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

#include <xs.h>

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

40 41
#define VIR_FROM_THIS VIR_FROM_XEN

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

47
struct xenUnifiedDriver xenStoreDriver = {
48 49 50
    xenStoreOpen, /* open */
    xenStoreClose, /* close */
    NULL, /* version */
51
    NULL, /* hostname */
52
    NULL, /* nodeGetInfo */
53
    NULL, /* getCapabilities */
54 55
    xenStoreListDomains, /* listDomains */
    NULL, /* numOfDomains */
56
    NULL, /* domainCreateXML */
57 58
    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
#define virXenStoreError(conn, code, fmt...)                                 \
91
        virReportErrorHelper(NULL, VIR_FROM_XENSTORE, code, __FILE__,      \
92
                               __FUNCTION__, __LINE__, fmt)
93 94 95 96 97 98

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

    if (conn == NULL)
        return NULL;

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

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

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

    if (!conn)
        return NULL;
146

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

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

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

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

    if (!VIR_IS_CONNECTED_DOMAIN(domain))
        return (-1);
178 179 180

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

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

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

    if (!VIR_IS_CONNECTED_DOMAIN(domain))
        return (NULL);
212 213 214

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

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

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

    return (vm);
}

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

    if (!VIR_IS_CONNECTED_DOMAIN(domain))
        return (NULL);
246 247 248

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

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

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

    return (ret);
}

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

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

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

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

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

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

    /* This will get called once at start */
    if ( xenStoreAddWatch(conn, "@releaseDomain",
                     "releaseDomain", xenStoreDomainReleased, priv) < 0 )
    {
        virXenStoreError(NULL, VIR_ERR_INTERNAL_ERROR,
                                 "%s", _("adding watch @releaseDomain"));
        return -1;
    }

    /* The initial call of this will fill domInfoList */
    if( xenStoreAddWatch(conn, "@introduceDomain",
                     "introduceDomain", xenStoreDomainIntroduced, priv) < 0 )
    {
        virXenStoreError(NULL, VIR_ERR_INTERNAL_ERROR,
                                 "%s", _("adding watch @introduceDomain"));
        return -1;
    }

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

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

/**
 * 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)
{
361 362
    xenUnifiedPrivatePtr priv;

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

368
    priv = (xenUnifiedPrivatePtr) conn->privateData;
369

D
Daniel P. Berrange 已提交
370
#ifndef PROXY
371 372 373 374 375 376 377 378 379 380 381
    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 已提交
382
    priv->xsWatchList = NULL;
383 384
    xenUnifiedDomainInfoListFree(priv->activeDomainList);
    priv->activeDomainList = NULL;
385
#endif
386 387 388
    if (priv->xshandle == NULL)
        return(-1);

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

396 397 398
    return (0);
}

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

417 418 419
    if (!VIR_IS_CONNECTED_DOMAIN(domain))
        return (-1);

420 421
    if ((domain == NULL) || (domain->conn == NULL) || (info == NULL)) {
        virXenStoreError(domain ? domain->conn : NULL, VIR_ERR_INVALID_ARG,
422 423
                         __FUNCTION__);
        return(-1);
424
    }
425 426 427

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

430
    if (domain->id == -1)
431
        return(-1);
432

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

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

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

518 519
    if (!VIR_IS_CONNECTED_DOMAIN(domain))
        return (ret);
520
    if (domain->id == -1)
521
        return(-1);
522

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

550
    if (conn == NULL) {
551
        virXenStoreError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
552
        return -1;
553
    }
554 555 556 557 558 559 560

    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);
561 562
    if (idlist) {
        free(idlist);
563
        ret = num;
564 565 566 567 568
    }
    return(ret);
}

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

587
    if (priv->xshandle == NULL)
588
        goto out;
589

590
    idlist = xs_directory (priv->xshandle, 0, "/local/domain", &num);
591
    if (idlist == NULL)
592
        goto out;
593 594

    for (ret = 0, i = 0; (i < num) && (ret < maxids); i++) {
595
        id = strtol(idlist[i], &endptr, 10);
596 597
        if ((endptr == idlist[i]) || (*endptr != 0))
            goto out;
598
        ids[ret++] = (int) id;
599
    }
600 601 602 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

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)) {
        virXenStoreError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return(-1);
    }

    priv = (xenUnifiedPrivatePtr) conn->privateData;

    xenUnifiedLock(priv);
    ret = xenStoreDoListDomains(priv, ids, maxids);
D
Daniel P. Berrange 已提交
631
    xenUnifiedUnlock(priv);
632 633

    return(ret);
634 635 636
}

/**
637
 * xenStoreLookupByName:
638 639 640 641 642 643 644 645
 * @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
646
xenStoreLookupByName(virConnectPtr conn, const char *name)
647 648 649 650 651
{
    virDomainPtr ret = NULL;
    unsigned int num, i, len;
    long id = -1;
    char **idlist = NULL, *endptr;
652
    char prop[200], *tmp;
653 654
    int found = 0;
    struct xend_domain *xenddomain = NULL;
655
    xenUnifiedPrivatePtr priv;
656 657 658

    if ((conn == NULL) || (name == NULL)) {
        virXenStoreError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
659
        return(NULL);
660
    }
661 662 663

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

666
    idlist = xs_directory(priv->xshandle, 0, "/local/domain", &num);
667
    if (idlist == NULL)
668
        goto done;
669 670

    for (i = 0; i < num; i++) {
671 672 673 674
        id = strtol(idlist[i], &endptr, 10);
        if ((endptr == idlist[i]) || (*endptr != 0)) {
            goto done;
        }
675
#if 0
676 677
        if (virConnectCheckStoreID(conn, (int) id) < 0)
            continue;
678
#endif
679 680 681 682 683 684 685 686 687
        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;
        }
688 689
    }
    if (!found)
690
        goto done;
691

692
    ret = virGetDomain(conn, name, NULL);
693
    if (ret == NULL)
694
        goto done;
695

696
    ret->id = id;
697 698

done:
699 700
        free(xenddomain);
        free(idlist);
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717

    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 已提交
718 719 720
    int ret;
    xenUnifiedPrivatePtr priv;

721 722
    if ((domain == NULL) || (domain->conn == NULL)) {
        virXenStoreError((domain ? domain->conn : NULL), VIR_ERR_INVALID_ARG,
723
                         __FUNCTION__);
724 725
        return(-1);
    }
726
    if (domain->id == -1 || domain->id == 0)
727
        return(-1);
728
    /*
729
     * this is very hackish, the domU kernel probes for a special
730 731
     * node in the xenstore and launch the shutdown command if found.
     */
D
Daniel P. Berrange 已提交
732 733 734 735 736
    priv = domain->conn->privateData;
    xenUnifiedLock(priv);
    ret = virDomainDoStoreWrite(domain, "control/shutdown", "poweroff");
    xenUnifiedUnlock(priv);
    return ret;
737 738
}

739 740 741 742 743 744 745 746 747 748 749 750 751 752
/**
 * 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 已提交
753 754 755
    int ret;
    xenUnifiedPrivatePtr priv;

756 757
    if ((domain == NULL) || (domain->conn == NULL)) {
        virXenStoreError((domain ? domain->conn : NULL), VIR_ERR_INVALID_ARG,
758
                         __FUNCTION__);
759 760
        return(-1);
    }
761
    if (domain->id == -1 || domain->id == 0)
762
        return(-1);
763
    /*
764
     * this is very hackish, the domU kernel probes for a special
765 766
     * node in the xenstore and launch the shutdown command if found.
     */
D
Daniel P. Berrange 已提交
767 768 769 770 771
    priv = domain->conn->privateData;
    xenUnifiedLock(priv);
    ret = virDomainDoStoreWrite(domain, "control/shutdown", "reboot");
    xenUnifiedUnlock(priv);
    return ret;
772
}
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788

/*
 * 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,
789
                         __FUNCTION__);
790 791 792 793 794
        return(NULL);
    }

    vm = virDomainGetVM(domain);
    if (vm) {
D
Daniel P. Berrange 已提交
795 796
        xenUnifiedPrivatePtr priv = domain->conn->privateData;
        xenUnifiedLock(priv);
797
        str = virDomainGetVMInfo(domain, vm, "image/ostype");
D
Daniel P. Berrange 已提交
798 799
        xenUnifiedUnlock(priv);
        VIR_FREE(vm);
800 801 802 803
    }

    return (str);
}
804
#endif /* ! PROXY */
805

806 807
/**
 * xenStoreDomainGetVNCPort:
808 809
 * @conn: the hypervisor connection
 * @domid: id of the domain
810 811
 *
 * Return the port number on which the domain is listening for VNC
812
 * connections.
813
 *
D
Daniel P. Berrange 已提交
814 815 816
 * The caller must hold the lock on the privateData
 * associated with the 'conn' parameter.
 *
817 818
 * Returns the port number, -1 in case of error
 */
819
int             xenStoreDomainGetVNCPort(virConnectPtr conn, int domid) {
820 821 822
    char *tmp;
    int ret = -1;

823
    tmp = virDomainDoStoreQuery(conn, domid, "console/vnc-port");
824 825 826 827 828 829 830 831 832 833 834 835
    if (tmp != NULL) {
        char *end;
        ret = strtol(tmp, &end, 10);
        if (ret == 0 && end == tmp)
            ret = -1;
        free(tmp);
    }
    return(ret);
}

/**
 * xenStoreDomainGetConsolePath:
836 837
 * @conn: the hypervisor connection
 * @domid: id of the domain
838 839 840 841 842 843 844
 *
 * 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
D
Daniel P. Berrange 已提交
845 846 847
 *
 * The caller must hold the lock on the privateData
 * associated with the 'conn' parameter.
848
 */
849 850
char *          xenStoreDomainGetConsolePath(virConnectPtr conn, int domid) {
  return virDomainDoStoreQuery(conn, domid, "console/tty");
851
}
852 853 854 855 856 857 858 859 860

#ifdef PROXY
/*
 * xenStoreDomainGetOSTypeID:
 * @conn: pointer to the connection.
 * @id: the domain id
 *
 * Get the type of domain operation system.
 *
D
Daniel P. Berrange 已提交
861 862 863
 * The caller must hold the lock on the privateData
 * associated with the 'conn' parameter.
 *
864 865 866 867 868 869 870 871
 * 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;
872
    xenUnifiedPrivatePtr priv;
873 874 875 876

    if (id < 0)
        return(NULL);

877 878
    priv = (xenUnifiedPrivatePtr) conn->privateData;
    if (priv->xshandle == NULL)
879 880 881 882 883
        return (NULL);

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

884
    vm = xs_read(priv->xshandle, 0, &query[0], &len);
885 886 887

    if (vm) {
        snprintf(query, 199, "%s/image/ostype", vm);
888
        str = xs_read(priv->xshandle, 0, &query[0], &len);
889 890
        free(vm);
    }
891 892
    if (str == NULL)
        str = strdup("linux");
893 894
    if (str == NULL)
        virReportOOMError(conn);
895 896 897 898

    return (str);
}
#endif /* PROXY */
899 900 901 902 903 904 905 906 907 908

/*
 * 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 已提交
909 910 911
 * The caller must hold the lock on the privateData
 * associated with the 'conn' parameter.
 *
912 913 914 915 916 917
 * 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;
918
    unsigned int len, i, num;
919
    char *ret = NULL;
920
    xenUnifiedPrivatePtr priv;
921 922 923

    if (id < 0)
        return(NULL);
924 925 926

    priv = (xenUnifiedPrivatePtr) conn->privateData;
    if (priv->xshandle == NULL)
927 928 929 930
        return (NULL);
    if (mac == NULL)
        return (NULL);

931
    snprintf(dir, sizeof(dir), "/local/domain/0/backend/vif/%d", id);
932
    list = xs_directory(priv->xshandle, 0, dir, &num);
933
    if (list == NULL)
934
        return(NULL);
935
    for (i = 0; i < num; i++) {
936
        snprintf(path, sizeof(path), "%s/%s/%s", dir, list[i], "mac");
937
        if ((val = xs_read(priv->xshandle, 0, path, &len)) == NULL)
938
            break;
939 940 941 942 943 944

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

        VIR_FREE(val);

        if (match) {
945
            ret = strdup(list[i]);
946 947 948 949

            if (ret == NULL)
                virReportOOMError(conn);

950 951
            break;
        }
952
    }
953 954

    VIR_FREE(list);
955 956
    return(ret);
}
957

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

                if (ret == NULL)
                    virReportOOMError(conn);

                VIR_FREE (val);
                VIR_FREE (list);
1010 1011
                return (ret);
            }
1012
        }
1013
        VIR_FREE (list);
1014
    }
1015 1016 1017 1018 1019 1020 1021 1022 1023
    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)) {
1024
                VIR_FREE (val);
1025 1026
            } else {
                ret = strdup(list[i]);
1027 1028 1029 1030 1031 1032

                if (ret == NULL)
                    virReportOOMError(conn);

                VIR_FREE (val);
                VIR_FREE (list);
1033 1034 1035
                return (ret);
            }
        }
1036
        VIR_FREE (list);
1037 1038
    }
    return (NULL);
1039 1040
}

D
Daniel P. Berrange 已提交
1041 1042 1043 1044
/*
 * The caller must hold the lock on the privateData
 * associated with the 'conn' parameter.
 */
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
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);
}

1060
#ifndef PROXY
D
Daniel P. Berrange 已提交
1061 1062 1063 1064
/*
 * The caller must hold the lock on the privateData
 * associated with the 'conn' parameter.
 */
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
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 已提交
1092 1093
static void
xenStoreWatchListFree(xenStoreWatchListPtr list)
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
{
    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 已提交
1104 1105 1106 1107
/*
 * The caller must hold the lock on the privateData
 * associated with the 'conn' parameter.
 */
1108 1109 1110 1111 1112 1113
int xenStoreAddWatch(virConnectPtr conn,
                     const char *path,
                     const char *token,
                     xenStoreWatchCallback cb,
                     void *opaque)
{
1114
    xenStoreWatchPtr watch = NULL;
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
    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)) {
            virXenStoreError(NULL, VIR_ERR_INTERNAL_ERROR,
                                 "%s", _("watch already tracked"));
            return -1;
        }
    }

    if (VIR_ALLOC(watch) < 0)
1137 1138
        goto no_memory;

1139 1140 1141 1142 1143
    watch->path   = strdup(path);
    watch->token  = strdup(token);
    watch->cb     = cb;
    watch->opaque = opaque;

1144 1145 1146 1147
    if (watch->path == NULL || watch->token == NULL) {
        goto no_memory;
    }

1148 1149 1150
    /* Make space on list */
    n = list->count;
    if (VIR_REALLOC_N(list->watches, n + 1) < 0) {
1151
        goto no_memory;
1152 1153 1154 1155 1156 1157
    }

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

    return xs_watch(priv->xshandle, watch->path, watch->token);
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168

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

    virReportOOMError(conn);

    return -1;
1169 1170
}

D
Daniel P. Berrange 已提交
1171 1172 1173 1174
/*
 * The caller must hold the lock on the privateData
 * associated with the 'conn' parameter.
 */
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
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,
1196
                       list->watches[i]->token))
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
            {
                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 已提交
1223 1224 1225 1226
static xenStoreWatchPtr
xenStoreFindWatch(xenStoreWatchListPtr list,
                  const char *path,
                  const char *token)
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
{
    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 已提交
1237 1238 1239
static void
xenStoreWatchEvent(int watch ATTRIBUTE_UNUSED,
                   int fd ATTRIBUTE_UNUSED,
1240
                   int events,
D
Daniel P. Berrange 已提交
1241
                   void *data)
1242 1243 1244 1245 1246 1247 1248 1249 1250
{
    char		 **event;
    char		 *path;
    char		 *token;
    unsigned int	 stringCount;
    xenStoreWatchPtr     sw;

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

1252
    if(!priv) return;
D
Daniel P. Berrange 已提交
1253

1254 1255 1256
    /* only set a watch on read and write events */
    if (events & (VIR_EVENT_HANDLE_ERROR | VIR_EVENT_HANDLE_HANGUP)) return;

D
Daniel P. Berrange 已提交
1257 1258 1259 1260
    xenUnifiedLock(priv);

    if(!priv->xshandle)
        goto cleanup;
1261 1262 1263

    event = xs_read_watch(priv->xshandle, &stringCount);
    if (!event)
D
Daniel P. Berrange 已提交
1264
        goto cleanup;
1265 1266 1267 1268 1269 1270 1271 1272

    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 已提交
1273 1274 1275

cleanup:
    xenUnifiedUnlock(priv);
1276 1277
}

D
Daniel P. Berrange 已提交
1278 1279 1280 1281 1282 1283

/*
 * The domain callback for the @introduceDomain watch
 *
 * The lock on 'priv' is held when calling this
 */
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
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;

1294
    xenUnifiedPrivatePtr priv = opaque;
1295 1296 1297 1298

retry:
    new_domain_cnt = xenStoreNumOfDomains(conn);
    if( VIR_ALLOC_N(new_domids,new_domain_cnt) < 0 ) {
1299
        virReportOOMError(NULL);
1300 1301
        return -1;
    }
1302
    nread = xenStoreDoListDomains(priv, new_domids, new_domain_cnt);
1303 1304 1305 1306 1307 1308 1309 1310 1311
    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;
1312 1313
        for (j = 0 ; j < priv->activeDomainList->count ; j++) {
            if (priv->activeDomainList->doms[j]->id == new_domids[i]) {
1314 1315 1316 1317 1318 1319
                found = 1;
                break;
            }
        }

        if (!found) {
1320
            virDomainEventPtr event;
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
            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;
            }

1334 1335 1336 1337 1338
            event = virDomainEventNew(new_domids[i], name, uuid,
                                      VIR_DOMAIN_EVENT_STARTED,
                                      VIR_DOMAIN_EVENT_STARTED_BOOTED);
            if (event)
                xenUnifiedDomainEventDispatch(priv, event);
1339

1340
            /* Add to the list */
1341
            xenUnifiedAddDomainInfo(priv->activeDomainList,
1342
                                    new_domids[i], name, uuid);
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356

            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 已提交
1357 1358 1359 1360 1361
/*
 * The domain callback for the @destroyDomain watch
 *
 * The lock on 'priv' is held when calling this
 */
1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
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;

1374
    if(!priv->activeDomainList->count) return 0;
1375 1376 1377 1378 1379

retry:
    new_domain_cnt = xenStoreNumOfDomains(conn);

    if( VIR_ALLOC_N(new_domids,new_domain_cnt) < 0 ) {
1380
        virReportOOMError(NULL);
1381 1382
        return -1;
    }
1383
    nread = xenStoreDoListDomains(priv, new_domids, new_domain_cnt);
1384 1385 1386 1387 1388 1389 1390
    if (nread != new_domain_cnt) {
        // mismatch. retry this read
        VIR_FREE(new_domids);
        goto retry;
    }

    removed = 0;
1391
    for (j=0 ; j < priv->activeDomainList->count ; j++) {
1392 1393
        found = 0;
        for (i=0 ; i < new_domain_cnt ; i++) {
1394
            if (priv->activeDomainList->doms[j]->id == new_domids[i]) {
1395 1396 1397 1398 1399 1400
                found = 1;
                break;
            }
        }

        if (!found) {
1401 1402
            virDomainEventPtr event =
                virDomainEventNew(-1,
1403 1404
                                  priv->activeDomainList->doms[j]->name,
                                  priv->activeDomainList->doms[j]->uuid,
1405 1406 1407 1408 1409 1410
                                  VIR_DOMAIN_EVENT_STOPPED,
                                  VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN);
            if (event)
                xenUnifiedDomainEventDispatch(priv, event);

            /* Remove from the list */
1411 1412 1413 1414
            xenUnifiedRemoveDomainInfo(priv->activeDomainList,
                                       priv->activeDomainList->doms[j]->id,
                                       priv->activeDomainList->doms[j]->name,
                                       priv->activeDomainList->doms[j]->uuid);
1415 1416

            removed = 1;
1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
        }
    }

    VIR_FREE(new_domids);

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

#endif //PROXY