xs_internal.c 38.9 KB
Newer Older
1 2 3
/*
 * xs_internal.c: access to Xen Store
 *
4
 * Copyright (C) 2006, 2009-2010 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 <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 25 26 27
#include <xen/version.h>

#include <xs.h>

28
#include "virterror_internal.h"
29
#include "datatypes.h"
30
#include "driver.h"
31 32 33 34
#include "memory.h"
#include "event.h"
#include "logging.h"
#include "uuid.h"
35
#include "xen_driver.h"
36
#include "xs_internal.h"
37
#include "xen_hypervisor.h"
38

39 40
#define VIR_FROM_THIS VIR_FROM_XEN

41
#ifndef PROXY
42
static char *xenStoreDomainGetOSType(virDomainPtr domain);
D
Daniel P. Berrange 已提交
43 44
static void xenStoreWatchEvent(int watch, int fd, int events, void *data);
static void xenStoreWatchListFree(xenStoreWatchListPtr list);
45

46
struct xenUnifiedDriver xenStoreDriver = {
47 48 49
    xenStoreOpen, /* open */
    xenStoreClose, /* close */
    NULL, /* version */
50
    NULL, /* hostname */
51
    NULL, /* nodeGetInfo */
52
    NULL, /* getCapabilities */
53 54
    xenStoreListDomains, /* listDomains */
    NULL, /* numOfDomains */
55
    NULL, /* domainCreateXML */
56 57
    NULL, /* domainSuspend */
    NULL, /* domainResume */
58 59
    xenStoreDomainShutdown, /* domainShutdown */
    xenStoreDomainReboot, /* domainReboot */
60
    NULL, /* domainDestroy */
61
    xenStoreDomainGetOSType, /* domainGetOSType */
62
    xenStoreDomainGetMaxMemory, /* domainGetMaxMemory */
63 64
    NULL, /* domainSetMaxMemory */
    xenStoreDomainSetMemory, /* domainSetMemory */
65 66
    xenStoreGetDomainInfo, /* domainGetInfo */
    NULL, /* domainSave */
67
    NULL, /* domainRestore */
D
Daniel Veillard 已提交
68
    NULL, /* domainCoreDump */
69
    NULL, /* domainPinVcpu */
70
    NULL, /* domainGetVcpus */
71 72 73 74 75
    NULL, /* listDefinedDomains */
    NULL, /* numOfDefinedDomains */
    NULL, /* domainCreate */
    NULL, /* domainDefineXML */
    NULL, /* domainUndefine */
76 77
    NULL, /* domainAttachDeviceFlags */
    NULL, /* domainDetachDeviceFlags */
78
    NULL, /* domainUpdateDeviceFlags */
79 80
    NULL, /* domainGetAutostart */
    NULL, /* domainSetAutostart */
81 82 83
    NULL, /* domainGetSchedulerType */
    NULL, /* domainGetSchedulerParameters */
    NULL, /* domainSetSchedulerParameters */
84 85
};

86
#endif /* ! PROXY */
87

88
#define virXenStoreError(code, ...)                                  \
89
        virReportErrorHelper(NULL, VIR_FROM_XENSTORE, code, __FILE__,      \
90
                             __FUNCTION__, __LINE__, __VA_ARGS__)
91 92 93 94 95 96

/************************************************************************
 *									*
 *		Helper internal APIs					*
 *									*
 ************************************************************************/
97
#ifndef PROXY
98 99 100 101 102 103 104 105 106 107 108 109 110 111
/**
 * 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)
{
112 113 114 115 116 117 118
    xenUnifiedPrivatePtr priv;

    if (conn == NULL)
        return NULL;

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

121
    return xs_directory (priv->xshandle, 0, path, nb);
122
}
123
#endif /* ! PROXY */
124 125 126

/**
 * virDomainDoStoreQuery:
127 128
 * @conn: pointer to the hypervisor connection
 * @domid: id of the domain
129 130 131 132 133 134 135
 * @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 *
136
virDomainDoStoreQuery(virConnectPtr conn, int domid, const char *path)
137 138 139
{
    char s[256];
    unsigned int len = 0;
140 141 142 143
    xenUnifiedPrivatePtr priv;

    if (!conn)
        return NULL;
144

145 146
    priv = (xenUnifiedPrivatePtr) conn->privateData;
    if (priv->xshandle == NULL)
147 148
        return (NULL);

149
    snprintf(s, 255, "/local/domain/%d/%s", domid, path);
150 151
    s[255] = 0;

152
    return xs_read(priv->xshandle, 0, &s[0], &len);
153 154
}

155
#ifndef PROXY
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
/**
 * 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];
171
    xenUnifiedPrivatePtr priv;
172 173 174 175
    int ret = -1;

    if (!VIR_IS_CONNECTED_DOMAIN(domain))
        return (-1);
176 177 178

    priv = (xenUnifiedPrivatePtr) domain->conn->privateData;
    if (priv->xshandle == NULL)
179 180 181 182
        return (-1);
    if (domain->conn->flags & VIR_CONNECT_RO)
        return (-1);

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

186
    if (xs_write(priv->xshandle, 0, &s[0], value, strlen(value)))
187 188 189 190 191 192 193 194 195 196 197 198 199
        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
 */
200
static char *
201 202 203 204 205
virDomainGetVM(virDomainPtr domain)
{
    char *vm;
    char query[200];
    unsigned int len;
206
    xenUnifiedPrivatePtr priv;
207 208 209

    if (!VIR_IS_CONNECTED_DOMAIN(domain))
        return (NULL);
210 211 212

    priv = (xenUnifiedPrivatePtr) domain->conn->privateData;
    if (priv->xshandle == NULL)
213 214 215 216 217
        return (NULL);

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

218
    vm = xs_read(priv->xshandle, 0, &query[0], &len);
219 220 221 222 223 224 225 226 227 228

    return (vm);
}

/**
 * virDomainGetVMInfo:
 * @domain: a domain object
 * @vm: the xenstore vm path
 * @name: the value's path
 *
229
 * Internal API extracting one information the device used
230 231 232 233
 * by the domain from xensttore
 *
 * Returns the new string or NULL in case of error
 */
234
static char *
235 236 237 238 239
virDomainGetVMInfo(virDomainPtr domain, const char *vm, const char *name)
{
    char s[256];
    char *ret = NULL;
    unsigned int len = 0;
240
    xenUnifiedPrivatePtr priv;
241 242 243

    if (!VIR_IS_CONNECTED_DOMAIN(domain))
        return (NULL);
244 245 246

    priv = (xenUnifiedPrivatePtr) domain->conn->privateData;
    if (priv->xshandle == NULL)
247 248 249 250 251
        return (NULL);

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

252
    ret = xs_read(priv->xshandle, 0, &s[0], &len);
253 254 255 256

    return (ret);
}

257
#endif /* ! PROXY */
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273

/************************************************************************
 *									*
 *		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.
 */
274
virDrvOpenStatus
275
xenStoreOpen(virConnectPtr conn,
276
             virConnectAuthPtr auth ATTRIBUTE_UNUSED,
277
             int flags ATTRIBUTE_UNUSED)
278
{
279
    xenUnifiedPrivatePtr priv = (xenUnifiedPrivatePtr) conn->privateData;
280

281
#ifdef PROXY
282
    priv->xshandle = xs_daemon_open_readonly();
283
#else
284
    if (flags & VIR_CONNECT_RO)
285
        priv->xshandle = xs_daemon_open_readonly();
286
    else
287
        priv->xshandle = xs_daemon_open();
288
#endif /* ! PROXY */
289

290
    if (priv->xshandle == NULL) {
291
        /*
J
John Levon 已提交
292 293
         * not being able to connect via the socket as an unprivileged
         * user is rather normal, this should fallback to the proxy (or
294
         * remote) mechanism.
295
         */
J
John Levon 已提交
296
        if (xenHavePrivilege()) {
297 298
            virXenStoreError(VIR_ERR_NO_XEN,
                             "%s", _("failed to connect to Xen Store"));
299
        }
300 301
        return (-1);
    }
302 303 304

#ifndef PROXY
    /* Init activeDomainList */
305
    if (VIR_ALLOC(priv->activeDomainList) < 0) {
306
        virReportOOMError();
307 308 309 310 311 312
        return -1;
    }

    /* Init watch list before filling in domInfoList,
       so we can know if it is the first time through
       when the callback fires */
313
    if (VIR_ALLOC(priv->xsWatchList) < 0) {
314
        virReportOOMError();
315 316 317 318 319 320 321
        return -1;
    }

    /* This will get called once at start */
    if ( xenStoreAddWatch(conn, "@releaseDomain",
                     "releaseDomain", xenStoreDomainReleased, priv) < 0 )
    {
322 323
        virXenStoreError(VIR_ERR_INTERNAL_ERROR,
                         "%s", _("adding watch @releaseDomain"));
324 325 326 327 328 329 330
        return -1;
    }

    /* The initial call of this will fill domInfoList */
    if( xenStoreAddWatch(conn, "@introduceDomain",
                     "introduceDomain", xenStoreDomainIntroduced, priv) < 0 )
    {
331 332
        virXenStoreError(VIR_ERR_INTERNAL_ERROR,
                         "%s", _("adding watch @introduceDomain"));
333 334 335 336 337 338 339 340 341
        return -1;
    }

    /* Add an event handle */
    if ((priv->xsWatch = virEventAddHandle(xs_fileno(priv->xshandle),
                                           VIR_EVENT_HANDLE_READABLE,
                                           xenStoreWatchEvent,
                                           conn,
                                           NULL)) < 0)
342
        DEBUG0("Failed to add event handle, disabling events");
343 344 345

#endif //PROXY
    return 0;
346 347 348 349 350 351 352 353 354 355 356 357 358
}

/**
 * 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)
{
359 360
    xenUnifiedPrivatePtr priv;

361
    if (conn == NULL) {
362
        virXenStoreError(VIR_ERR_INVALID_ARG, __FUNCTION__);
363
        return(-1);
364 365
    }

366
    priv = (xenUnifiedPrivatePtr) conn->privateData;
367

D
Daniel P. Berrange 已提交
368
#ifndef PROXY
369 370 371 372 373 374 375 376 377 378 379
    if (xenStoreRemoveWatch(conn, "@introduceDomain", "introduceDomain") < 0) {
        DEBUG0("Warning, could not remove @introduceDomain watch");
        /* not fatal */
    }

    if (xenStoreRemoveWatch(conn, "@releaseDomain", "releaseDomain") < 0) {
        DEBUG0("Warning, could not remove @releaseDomain watch");
        /* not fatal */
    }

    xenStoreWatchListFree(priv->xsWatchList);
J
John Levon 已提交
380
    priv->xsWatchList = NULL;
381 382
    xenUnifiedDomainInfoListFree(priv->activeDomainList);
    priv->activeDomainList = NULL;
383
#endif
384 385 386
    if (priv->xshandle == NULL)
        return(-1);

387
#ifndef PROXY
388 389
    if (priv->xsWatch != -1)
        virEventRemoveHandle(priv->xsWatch);
390
#endif
391
    xs_daemon_close(priv->xshandle);
392 393
    priv->xshandle = NULL;

394 395 396
    return (0);
}

397
#ifndef PROXY
398 399 400
/**
 * xenStoreGetDomainInfo:
 * @domain: pointer to the domain block
401
 * @info: the place where information should be stored
402
 *
403
 * Do an hypervisor call to get the related set of domain information.
404 405 406 407 408 409 410 411 412
 *
 * 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];
413
    xenUnifiedPrivatePtr priv;
414

415 416 417
    if (!VIR_IS_CONNECTED_DOMAIN(domain))
        return (-1);

418
    if ((domain == NULL) || (domain->conn == NULL) || (info == NULL)) {
419
        virXenStoreError(VIR_ERR_INVALID_ARG, __FUNCTION__);
420
        return(-1);
421
    }
422 423 424

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

427
    if (domain->id == -1)
428
        return(-1);
429

430
    tmp = virDomainDoStoreQuery(domain->conn, domain->id, "running");
431 432 433
    if (tmp != NULL) {
        if (tmp[0] == '1')
            info->state = VIR_DOMAIN_RUNNING;
434
        VIR_FREE(tmp);
435
    } else {
436
        info->state = VIR_DOMAIN_NOSTATE;
437
    }
438
    tmp = virDomainDoStoreQuery(domain->conn, domain->id, "memory/target");
439 440 441
    if (tmp != NULL) {
        info->memory = atol(tmp);
        info->maxMem = atol(tmp);
442
        VIR_FREE(tmp);
443 444 445 446
    } else {
        info->memory = 0;
        info->maxMem = 0;
    }
447
# if 0
448
    /* doesn't seems to work */
449
    tmp = virDomainDoStoreQuery(domain->conn, domain->id, "cpu_time");
450 451
    if (tmp != NULL) {
        info->cpuTime = atol(tmp);
452
        VIR_FREE(tmp);
453 454 455
    } else {
        info->cpuTime = 0;
    }
456
# endif
457
    snprintf(request, 199, "/local/domain/%d/cpu", domain->id);
458 459 460 461
    request[199] = 0;
    tmp2 = virConnectDoStoreList(domain->conn, request, &nb_vcpus);
    if (tmp2 != NULL) {
        info->nrVirtCpu = nb_vcpus;
462
        VIR_FREE(tmp2);
463 464 465 466 467
    }
    return (0);
}

/**
468
 * xenStoreDomainSetMemory:
469 470 471 472 473 474 475 476
 * @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
477
xenStoreDomainSetMemory(virDomainPtr domain, unsigned long memory)
478 479 480 481
{
    int ret;
    char value[20];

D
Daniel Veillard 已提交
482 483
    if ((domain == NULL) || (domain->conn == NULL) ||
        (memory < 1024 * MIN_XEN_GUEST_SIZE)) {
484
        virXenStoreError(VIR_ERR_INVALID_ARG, __FUNCTION__);
485
        return(-1);
486
    }
487
    if (domain->id == -1)
488
        return(-1);
D
Daniel Veillard 已提交
489
    if ((domain->id == 0) && (memory < (2 * MIN_XEN_GUEST_SIZE * 1024)))
490
        return(-1);
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
    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;
D
Daniel P. Berrange 已提交
512
    xenUnifiedPrivatePtr priv;
513

514 515
    if (!VIR_IS_CONNECTED_DOMAIN(domain))
        return (ret);
516
    if (domain->id == -1)
517
        return(-1);
518

D
Daniel P. Berrange 已提交
519 520
    priv = domain->conn->privateData;
    xenUnifiedLock(priv);
521
    tmp = virDomainDoStoreQuery(domain->conn, domain->id, "memory/target");
522
    if (tmp != NULL) {
523
        ret = (unsigned long) atol(tmp);
D
Daniel P. Berrange 已提交
524
        VIR_FREE(tmp);
525
    }
D
Daniel P. Berrange 已提交
526
    xenUnifiedUnlock(priv);
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
    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;
542 543 544
    char **idlist = NULL, *endptr;
    int i, ret = -1, realnum = 0;
    long id;
545
    xenUnifiedPrivatePtr priv;
546

547
    if (conn == NULL) {
548
        virXenStoreError(VIR_ERR_INVALID_ARG, __FUNCTION__);
549
        return -1;
550
    }
551 552 553

    priv = (xenUnifiedPrivatePtr) conn->privateData;
    if (priv->xshandle == NULL) {
554
        virXenStoreError(VIR_ERR_INVALID_ARG, __FUNCTION__);
555 556
        return(-1);
    }
557

558
    idlist = xs_directory(priv->xshandle, 0, "/local/domain", &num);
559
    if (idlist) {
560 561 562 563 564 565 566 567 568 569 570 571 572
        for (i = 0; i < num; i++) {
            id = strtol(idlist[i], &endptr, 10);
            if ((endptr == idlist[i]) || (*endptr != 0))
                goto out;

            /* Sometimes xenstore has stale domain IDs, so filter
               against the hypervisor's info */
            if (xenHypervisorHasDomain(conn, (int)id))
                realnum++;
        }
out:
        VIR_FREE (idlist);
        ret = realnum;
573 574 575 576 577
    }
    return(ret);
}

/**
578
 * xenStoreDoListDomains:
579 580 581 582
 * @conn: pointer to the hypervisor connection
 * @ids: array to collect the list of IDs of active domains
 * @maxids: size of @ids
 *
583 584
 * Internal API: collect the list of active domains, and store
 * their ID in @maxids. The driver lock must be held.
585 586 587
 *
 * Returns the number of domain found or -1 in case of error
 */
588
static int
589
xenStoreDoListDomains(virConnectPtr conn, xenUnifiedPrivatePtr priv, int *ids, int maxids)
590 591 592
{
    char **idlist = NULL, *endptr;
    unsigned int num, i;
593
    int ret = -1;
594
    long id;
D
Daniel P. Berrange 已提交
595

596
    if (priv->xshandle == NULL)
597
        goto out;
598

599
    idlist = xs_directory (priv->xshandle, 0, "/local/domain", &num);
600
    if (idlist == NULL)
601
        goto out;
602 603

    for (ret = 0, i = 0; (i < num) && (ret < maxids); i++) {
604
        id = strtol(idlist[i], &endptr, 10);
605 606
        if ((endptr == idlist[i]) || (*endptr != 0))
            goto out;
607 608 609 610 611

        /* Sometimes xenstore has stale domain IDs, so filter
           against the hypervisor's info */
        if (xenHypervisorHasDomain(conn, (int)id))
            ids[ret++] = (int) id;
612
    }
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635

out:
    VIR_FREE (idlist);
    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)
{
    xenUnifiedPrivatePtr priv;
    int ret;

    if ((conn == NULL) || (ids == NULL)) {
636
        virXenStoreError(VIR_ERR_INVALID_ARG, __FUNCTION__);
637 638 639 640 641 642
        return(-1);
    }

    priv = (xenUnifiedPrivatePtr) conn->privateData;

    xenUnifiedLock(priv);
643
    ret = xenStoreDoListDomains(conn, priv, ids, maxids);
D
Daniel P. Berrange 已提交
644
    xenUnifiedUnlock(priv);
645 646

    return(ret);
647 648 649
}

/**
650
 * xenStoreLookupByName:
651 652 653 654 655 656 657 658
 * @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
659
xenStoreLookupByName(virConnectPtr conn, const char *name)
660 661 662 663 664
{
    virDomainPtr ret = NULL;
    unsigned int num, i, len;
    long id = -1;
    char **idlist = NULL, *endptr;
665
    char prop[200], *tmp;
666 667
    int found = 0;
    struct xend_domain *xenddomain = NULL;
668
    xenUnifiedPrivatePtr priv;
669 670

    if ((conn == NULL) || (name == NULL)) {
671
        virXenStoreError(VIR_ERR_INVALID_ARG, __FUNCTION__);
672
        return(NULL);
673
    }
674 675 676

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

679
    idlist = xs_directory(priv->xshandle, 0, "/local/domain", &num);
680
    if (idlist == NULL)
681
        goto done;
682 683

    for (i = 0; i < num; i++) {
684 685 686 687
        id = strtol(idlist[i], &endptr, 10);
        if ((endptr == idlist[i]) || (*endptr != 0)) {
            goto done;
        }
688
# if 0
689 690
        if (virConnectCheckStoreID(conn, (int) id) < 0)
            continue;
691
# endif
692 693 694 695 696
        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);
697
            VIR_FREE(tmp);
698 699 700
            if (found)
                break;
        }
701 702
    }
    if (!found)
703
        goto done;
704

705
    ret = virGetDomain(conn, name, NULL);
706
    if (ret == NULL)
707
        goto done;
708

709
    ret->id = id;
710 711

done:
712 713
    VIR_FREE(xenddomain);
    VIR_FREE(idlist);
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730

    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)
{
D
Daniel P. Berrange 已提交
731 732 733
    int ret;
    xenUnifiedPrivatePtr priv;

734
    if ((domain == NULL) || (domain->conn == NULL)) {
735
        virXenStoreError(VIR_ERR_INVALID_ARG, __FUNCTION__);
736 737
        return(-1);
    }
738
    if (domain->id == -1 || domain->id == 0)
739
        return(-1);
740
    /*
741
     * this is very hackish, the domU kernel probes for a special
742 743
     * node in the xenstore and launch the shutdown command if found.
     */
D
Daniel P. Berrange 已提交
744 745 746 747 748
    priv = domain->conn->privateData;
    xenUnifiedLock(priv);
    ret = virDomainDoStoreWrite(domain, "control/shutdown", "poweroff");
    xenUnifiedUnlock(priv);
    return ret;
749 750
}

751 752 753 754 755 756 757 758 759 760 761 762 763 764
/**
 * 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)
{
D
Daniel P. Berrange 已提交
765 766 767
    int ret;
    xenUnifiedPrivatePtr priv;

768
    if ((domain == NULL) || (domain->conn == NULL)) {
769
        virXenStoreError(VIR_ERR_INVALID_ARG, __FUNCTION__);
770 771
        return(-1);
    }
772
    if (domain->id == -1 || domain->id == 0)
773
        return(-1);
774
    /*
775
     * this is very hackish, the domU kernel probes for a special
776 777
     * node in the xenstore and launch the shutdown command if found.
     */
D
Daniel P. Berrange 已提交
778 779 780 781 782
    priv = domain->conn->privateData;
    xenUnifiedLock(priv);
    ret = virDomainDoStoreWrite(domain, "control/shutdown", "reboot");
    xenUnifiedUnlock(priv);
    return ret;
783
}
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798

/*
 * 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)) {
799
        virXenStoreError(VIR_ERR_INVALID_ARG, __FUNCTION__);
800 801 802 803 804
        return(NULL);
    }

    vm = virDomainGetVM(domain);
    if (vm) {
D
Daniel P. Berrange 已提交
805 806
        xenUnifiedPrivatePtr priv = domain->conn->privateData;
        xenUnifiedLock(priv);
807
        str = virDomainGetVMInfo(domain, vm, "image/ostype");
D
Daniel P. Berrange 已提交
808 809
        xenUnifiedUnlock(priv);
        VIR_FREE(vm);
810 811 812 813
    }

    return (str);
}
814
#endif /* ! PROXY */
815

816 817
/**
 * xenStoreDomainGetVNCPort:
818 819
 * @conn: the hypervisor connection
 * @domid: id of the domain
820 821
 *
 * Return the port number on which the domain is listening for VNC
822
 * connections.
823
 *
D
Daniel P. Berrange 已提交
824 825 826
 * The caller must hold the lock on the privateData
 * associated with the 'conn' parameter.
 *
827 828
 * Returns the port number, -1 in case of error
 */
829
int             xenStoreDomainGetVNCPort(virConnectPtr conn, int domid) {
830 831 832
    char *tmp;
    int ret = -1;

833
    tmp = virDomainDoStoreQuery(conn, domid, "console/vnc-port");
834 835 836 837 838
    if (tmp != NULL) {
        char *end;
        ret = strtol(tmp, &end, 10);
        if (ret == 0 && end == tmp)
            ret = -1;
839
        VIR_FREE(tmp);
840 841 842 843 844 845
    }
    return(ret);
}

/**
 * xenStoreDomainGetConsolePath:
846 847
 * @conn: the hypervisor connection
 * @domid: id of the domain
848
 *
849
 * Return the path to the pseudo TTY on which the guest domain's
850 851 852 853 854
 * 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
D
Daniel P. Berrange 已提交
855 856 857
 *
 * The caller must hold the lock on the privateData
 * associated with the 'conn' parameter.
858
 */
859 860
char *          xenStoreDomainGetConsolePath(virConnectPtr conn, int domid) {
  return virDomainDoStoreQuery(conn, domid, "console/tty");
861
}
862 863 864 865 866 867 868 869 870

#ifdef PROXY
/*
 * xenStoreDomainGetOSTypeID:
 * @conn: pointer to the connection.
 * @id: the domain id
 *
 * Get the type of domain operation system.
 *
D
Daniel P. Berrange 已提交
871 872 873
 * The caller must hold the lock on the privateData
 * associated with the 'conn' parameter.
 *
874 875 876 877 878 879 880 881
 * 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;
882
    xenUnifiedPrivatePtr priv;
883 884 885 886

    if (id < 0)
        return(NULL);

887 888
    priv = (xenUnifiedPrivatePtr) conn->privateData;
    if (priv->xshandle == NULL)
889 890 891 892 893
        return (NULL);

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

894
    vm = xs_read(priv->xshandle, 0, &query[0], &len);
895 896 897

    if (vm) {
        snprintf(query, 199, "%s/image/ostype", vm);
898
        str = xs_read(priv->xshandle, 0, &query[0], &len);
899
        VIR_FREE(vm);
900
    }
901 902
    if (str == NULL)
        str = strdup("linux");
903
    if (str == NULL)
904
        virReportOOMError();
905 906 907 908

    return (str);
}
#endif /* PROXY */
909 910 911 912 913 914 915 916 917 918

/*
 * 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
 *
D
Daniel P. Berrange 已提交
919 920 921
 * The caller must hold the lock on the privateData
 * associated with the 'conn' parameter.
 *
922 923 924 925 926 927
 * 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;
928
    unsigned int len, i, num;
929
    char *ret = NULL;
930
    xenUnifiedPrivatePtr priv;
931 932 933

    if (id < 0)
        return(NULL);
934 935 936

    priv = (xenUnifiedPrivatePtr) conn->privateData;
    if (priv->xshandle == NULL)
937 938 939 940
        return (NULL);
    if (mac == NULL)
        return (NULL);

941
    snprintf(dir, sizeof(dir), "/local/domain/0/backend/vif/%d", id);
942
    list = xs_directory(priv->xshandle, 0, dir, &num);
943
    if (list == NULL)
944
        return(NULL);
945
    for (i = 0; i < num; i++) {
946
        snprintf(path, sizeof(path), "%s/%s/%s", dir, list[i], "mac");
947
        if ((val = xs_read(priv->xshandle, 0, path, &len)) == NULL)
948
            break;
949 950 951 952 953 954

        bool match = (virMacAddrCompare(val, mac) == 0);

        VIR_FREE(val);

        if (match) {
955
            ret = strdup(list[i]);
956 957

            if (ret == NULL)
958
                virReportOOMError();
959

960 961
            break;
        }
962
    }
963 964

    VIR_FREE(list);
965 966
    return(ret);
}
967

968 969 970 971 972 973 974 975 976
/*
 * 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
 *
D
Daniel P. Berrange 已提交
977 978 979
 * The caller must hold the lock on the privateData
 * associated with the 'conn' parameter.
 *
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
 * 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);
1004 1005 1006 1007 1008 1009 1010
    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)) {
1011
                VIR_FREE (val);
1012 1013
            } else {
                ret = strdup(list[i]);
1014 1015

                if (ret == NULL)
1016
                    virReportOOMError();
1017 1018 1019

                VIR_FREE (val);
                VIR_FREE (list);
1020 1021
                return (ret);
            }
1022
        }
1023
        VIR_FREE (list);
1024
    }
1025 1026 1027 1028 1029 1030 1031 1032 1033
    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)) {
1034
                VIR_FREE (val);
1035 1036
            } else {
                ret = strdup(list[i]);
1037 1038

                if (ret == NULL)
1039
                    virReportOOMError();
1040 1041 1042

                VIR_FREE (val);
                VIR_FREE (list);
1043 1044 1045
                return (ret);
            }
        }
1046
        VIR_FREE (list);
1047 1048
    }
    return (NULL);
1049 1050
}

1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 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 1105
/*
 * xenStoreDomainGetPCIID:
 * @conn: pointer to the connection.
 * @id: the domain id
 * @bdf: the PCI BDF
 *
 * Get the reference (i.e. the string number) for the device on that domain
 * which uses the given PCI address
 *
 * The caller must hold the lock on the privateData
 * associated with the 'conn' parameter.
 *
 * Returns the new string or NULL in case of error, the string must be
 *         freed by the caller.
 */
char *
xenStoreDomainGetPCIID(virConnectPtr conn, int id, const char *bdf)
{
    char dir[80], path[128], **list = NULL, *val = NULL;
    unsigned int len, i, num;
    char *ret = NULL;
    xenUnifiedPrivatePtr priv;

    if (id < 0)
        return(NULL);

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

    snprintf(dir, sizeof(dir), "/local/domain/0/backend/pci/%d", id);
    list = xs_directory(priv->xshandle, 0, dir, &num);
    if (list == NULL)
        return(NULL);
    for (i = 0; i < num; i++) {
        snprintf(path, sizeof(path), "%s/%s/%s", dir, list[i], "dev-0");
        if ((val = xs_read(priv->xshandle, 0, path, &len)) == NULL)
            break;

        bool match = STREQ(val, bdf);

        VIR_FREE(val);

        if (match) {
            ret = strdup(list[i]);
            break;
        }
    }

    VIR_FREE(list);
    return(ret);
}

D
Daniel P. Berrange 已提交
1106 1107 1108 1109
/*
 * The caller must hold the lock on the privateData
 * associated with the 'conn' parameter.
 */
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
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);
}

1125
#ifndef PROXY
D
Daniel P. Berrange 已提交
1126 1127 1128 1129
/*
 * The caller must hold the lock on the privateData
 * associated with the 'conn' parameter.
 */
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
int xenStoreDomainGetUUID(virConnectPtr conn,
                          int id,
                          unsigned char *uuid) {
    char prop[200];
    xenUnifiedPrivatePtr priv;
    unsigned int len;
    char *uuidstr;
    int ret = 0;

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

    snprintf(prop, 199, "/local/domain/%d/vm", id);
    prop[199] = 0;
    // This will return something like
    // /vm/00000000-0000-0000-0000-000000000000
    uuidstr = xs_read(priv->xshandle, 0, prop, &len);

    // remove "/vm/"
    ret = virUUIDParse(uuidstr + 4, uuid);

    VIR_FREE(uuidstr);

    return ret;
}

D
Daniel P. Berrange 已提交
1157 1158
static void
xenStoreWatchListFree(xenStoreWatchListPtr list)
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
{
    int i;
    for (i=0; i<list->count; i++) {
        VIR_FREE(list->watches[i]->path);
        VIR_FREE(list->watches[i]->token);
        VIR_FREE(list->watches[i]);
    }
    VIR_FREE(list);
}

D
Daniel P. Berrange 已提交
1169 1170 1171 1172
/*
 * The caller must hold the lock on the privateData
 * associated with the 'conn' parameter.
 */
1173 1174 1175 1176 1177 1178
int xenStoreAddWatch(virConnectPtr conn,
                     const char *path,
                     const char *token,
                     xenStoreWatchCallback cb,
                     void *opaque)
{
1179
    xenStoreWatchPtr watch = NULL;
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
    int n;
    xenStoreWatchListPtr list;
    xenUnifiedPrivatePtr priv = (xenUnifiedPrivatePtr) conn->privateData;

    if (priv->xshandle == NULL)
        return -1;

    list = priv->xsWatchList;
    if(!list)
        return -1;

    /* check if we already have this callback on our list */
    for (n=0; n < list->count; n++) {
        if( STREQ(list->watches[n]->path, path) &&
            STREQ(list->watches[n]->token, token)) {
1195 1196
            virXenStoreError(VIR_ERR_INTERNAL_ERROR,
                             "%s", _("watch already tracked"));
1197 1198 1199 1200 1201
            return -1;
        }
    }

    if (VIR_ALLOC(watch) < 0)
1202 1203
        goto no_memory;

1204 1205 1206 1207 1208
    watch->path   = strdup(path);
    watch->token  = strdup(token);
    watch->cb     = cb;
    watch->opaque = opaque;

1209 1210 1211 1212
    if (watch->path == NULL || watch->token == NULL) {
        goto no_memory;
    }

1213 1214 1215
    /* Make space on list */
    n = list->count;
    if (VIR_REALLOC_N(list->watches, n + 1) < 0) {
1216
        goto no_memory;
1217 1218 1219 1220 1221 1222
    }

    list->watches[n] = watch;
    list->count++;

    return xs_watch(priv->xshandle, watch->path, watch->token);
1223 1224 1225 1226 1227 1228 1229 1230

  no_memory:
    if (watch) {
        VIR_FREE(watch->path);
        VIR_FREE(watch->token);
        VIR_FREE(watch);
    }

1231
    virReportOOMError();
1232 1233

    return -1;
1234 1235
}

D
Daniel P. Berrange 已提交
1236 1237 1238 1239
/*
 * The caller must hold the lock on the privateData
 * associated with the 'conn' parameter.
 */
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
int xenStoreRemoveWatch(virConnectPtr conn,
                        const char *path,
                        const char *token)
{
    int i;
    xenStoreWatchListPtr list;
    xenUnifiedPrivatePtr priv = (xenUnifiedPrivatePtr) conn->privateData;

    if (priv->xshandle == NULL)
        return -1;

    list = priv->xsWatchList;
    if(!list)
        return -1;

    for (i = 0 ; i < list->count ; i++) {
        if( STREQ(list->watches[i]->path, path) &&
            STREQ(list->watches[i]->token, token)) {

            if (!xs_unwatch(priv->xshandle,
                       list->watches[i]->path,
1261
                       list->watches[i]->token))
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
            {
                DEBUG0("WARNING: Could not remove watch");
                /* Not fatal, continue */
            }

            VIR_FREE(list->watches[i]->path);
            VIR_FREE(list->watches[i]->token);
            VIR_FREE(list->watches[i]);

            if (i < (list->count - 1))
                memmove(list->watches + i,
                        list->watches + i + 1,
                        sizeof(*(list->watches)) *
                                (list->count - (i + 1)));

            if (VIR_REALLOC_N(list->watches,
                              list->count - 1) < 0) {
                ; /* Failure to reduce memory allocation isn't fatal */
            }
            list->count--;
            return 0;
        }
    }
    return -1;
}

D
Daniel P. Berrange 已提交
1288 1289 1290 1291
static xenStoreWatchPtr
xenStoreFindWatch(xenStoreWatchListPtr list,
                  const char *path,
                  const char *token)
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
{
    int i;
    for (i = 0 ; i < list->count ; i++)
        if( STREQ(path, list->watches[i]->path) &&
            STREQ(token, list->watches[i]->token) )
            return list->watches[i];

    return NULL;
}

D
Daniel P. Berrange 已提交
1302 1303 1304
static void
xenStoreWatchEvent(int watch ATTRIBUTE_UNUSED,
                   int fd ATTRIBUTE_UNUSED,
1305
                   int events,
D
Daniel P. Berrange 已提交
1306
                   void *data)
1307 1308 1309 1310 1311 1312 1313 1314 1315
{
    char		 **event;
    char		 *path;
    char		 *token;
    unsigned int	 stringCount;
    xenStoreWatchPtr     sw;

    virConnectPtr        conn = data;
    xenUnifiedPrivatePtr priv = (xenUnifiedPrivatePtr) conn->privateData;
1316

1317
    if(!priv) return;
D
Daniel P. Berrange 已提交
1318

1319 1320 1321
    /* only set a watch on read and write events */
    if (events & (VIR_EVENT_HANDLE_ERROR | VIR_EVENT_HANDLE_HANGUP)) return;

D
Daniel P. Berrange 已提交
1322 1323 1324 1325
    xenUnifiedLock(priv);

    if(!priv->xshandle)
        goto cleanup;
1326 1327 1328

    event = xs_read_watch(priv->xshandle, &stringCount);
    if (!event)
D
Daniel P. Berrange 已提交
1329
        goto cleanup;
1330 1331 1332 1333 1334 1335 1336 1337

    path  = event[XS_WATCH_PATH];
    token = event[XS_WATCH_TOKEN];

    sw = xenStoreFindWatch(priv->xsWatchList, path, token);
    if( sw )
        sw->cb(conn, path, token, sw->opaque);
    VIR_FREE(event);
D
Daniel P. Berrange 已提交
1338 1339 1340

cleanup:
    xenUnifiedUnlock(priv);
1341 1342
}

D
Daniel P. Berrange 已提交
1343 1344 1345 1346 1347 1348

/*
 * The domain callback for the @introduceDomain watch
 *
 * The lock on 'priv' is held when calling this
 */
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
int xenStoreDomainIntroduced(virConnectPtr conn,
                             const char *path ATTRIBUTE_UNUSED,
                             const char *token ATTRIBUTE_UNUSED,
                             void *opaque)
{
    int i, j, found, missing = 0, retries = 20;
    int new_domain_cnt;
    int *new_domids;
    int nread;

1359
    xenUnifiedPrivatePtr priv = opaque;
1360 1361 1362

retry:
    new_domain_cnt = xenStoreNumOfDomains(conn);
1363 1364 1365
    if (new_domain_cnt < 0)
        return -1;

1366
    if( VIR_ALLOC_N(new_domids,new_domain_cnt) < 0 ) {
1367
        virReportOOMError();
1368 1369
        return -1;
    }
1370
    nread = xenStoreDoListDomains(conn, priv, new_domids, new_domain_cnt);
1371 1372 1373 1374 1375 1376 1377 1378 1379
    if (nread != new_domain_cnt) {
        // mismatch. retry this read
        VIR_FREE(new_domids);
        goto retry;
    }

    missing = 0;
    for (i=0 ; i < new_domain_cnt ; i++) {
        found = 0;
1380 1381
        for (j = 0 ; j < priv->activeDomainList->count ; j++) {
            if (priv->activeDomainList->doms[j]->id == new_domids[i]) {
1382 1383 1384 1385 1386 1387
                found = 1;
                break;
            }
        }

        if (!found) {
1388
            virDomainEventPtr event;
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
            char *name;
            unsigned char uuid[VIR_UUID_BUFLEN];

            if (!(name = xenStoreDomainGetName(conn, new_domids[i]))) {
                missing = 1;
                continue;
            }
            if (xenStoreDomainGetUUID(conn, new_domids[i], uuid) < 0) {
                missing = 1;
                VIR_FREE(name);
                continue;
            }

1402 1403 1404 1405 1406
            event = virDomainEventNew(new_domids[i], name, uuid,
                                      VIR_DOMAIN_EVENT_STARTED,
                                      VIR_DOMAIN_EVENT_STARTED_BOOTED);
            if (event)
                xenUnifiedDomainEventDispatch(priv, event);
1407

1408
            /* Add to the list */
1409
            xenUnifiedAddDomainInfo(priv->activeDomainList,
1410
                                    new_domids[i], name, uuid);
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424

            VIR_FREE(name);
        }
    }
    VIR_FREE(new_domids);

    if (missing && retries--) {
        DEBUG0("Some domains were missing, trying again");
        usleep(100 * 1000);
        goto retry;
    }
    return 0;
}

D
Daniel P. Berrange 已提交
1425 1426 1427 1428 1429
/*
 * The domain callback for the @destroyDomain watch
 *
 * The lock on 'priv' is held when calling this
 */
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
int xenStoreDomainReleased(virConnectPtr conn,
                            const char *path  ATTRIBUTE_UNUSED,
                            const char *token ATTRIBUTE_UNUSED,
                            void *opaque)
{
    int i, j, found, removed, retries = 20;
    int new_domain_cnt;
    int *new_domids;
    int nread;

    xenUnifiedPrivatePtr priv = (xenUnifiedPrivatePtr) opaque;

1442
    if(!priv->activeDomainList->count) return 0;
1443 1444 1445

retry:
    new_domain_cnt = xenStoreNumOfDomains(conn);
1446 1447
    if (new_domain_cnt < 0)
        return -1;
1448 1449

    if( VIR_ALLOC_N(new_domids,new_domain_cnt) < 0 ) {
1450
        virReportOOMError();
1451 1452
        return -1;
    }
1453
    nread = xenStoreDoListDomains(conn, priv, new_domids, new_domain_cnt);
1454 1455 1456 1457 1458 1459 1460
    if (nread != new_domain_cnt) {
        // mismatch. retry this read
        VIR_FREE(new_domids);
        goto retry;
    }

    removed = 0;
1461
    for (j=0 ; j < priv->activeDomainList->count ; j++) {
1462 1463
        found = 0;
        for (i=0 ; i < new_domain_cnt ; i++) {
1464
            if (priv->activeDomainList->doms[j]->id == new_domids[i]) {
1465 1466 1467 1468 1469 1470
                found = 1;
                break;
            }
        }

        if (!found) {
1471 1472
            virDomainEventPtr event =
                virDomainEventNew(-1,
1473 1474
                                  priv->activeDomainList->doms[j]->name,
                                  priv->activeDomainList->doms[j]->uuid,
1475 1476 1477 1478 1479 1480
                                  VIR_DOMAIN_EVENT_STOPPED,
                                  VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
            if (event)
                xenUnifiedDomainEventDispatch(priv, event);

            /* Remove from the list */
1481 1482 1483 1484
            xenUnifiedRemoveDomainInfo(priv->activeDomainList,
                                       priv->activeDomainList->doms[j]->id,
                                       priv->activeDomainList->doms[j]->name,
                                       priv->activeDomainList->doms[j]->uuid);
1485 1486

            removed = 1;
1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500
        }
    }

    VIR_FREE(new_domids);

    if (!removed && retries--) {
        DEBUG0("No domains removed, retrying");
        usleep(100 * 1000);
        goto retry;
    }
    return 0;
}

#endif //PROXY