diff --git a/ChangeLog b/ChangeLog index 72d45c31b6..83d1b7f4b7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Fri Dec 2 15:15:26 CET 2005 Daniel Veillard + + * include/libxen.h src/libxen.c src/libxen_sym.version: adding + xenConnectListDomains() to list active domains + * src/xensh.c: integrated a basic test for xenConnectListDomains() + Fri Dec 2 13:10:04 CET 2005 Daniel Veillard * configure.in src/Makefile.am: more warnings from compiler and diff --git a/include/libxen.h b/include/libxen.h index af0e6545dd..b30f3996b8 100644 --- a/include/libxen.h +++ b/include/libxen.h @@ -65,6 +65,13 @@ xenConnectPtr xenConnectOpenReadOnly (const char *name); int xenConnectClose (xenConnectPtr conn); unsigned long xenConnectGetVersion (xenConnectPtr conn); +/* + * Gather list of running domains + */ +int xenConnectListDomains (xenConnectPtr conn, + int *ids, + int maxids); + /* * Domain creation and destruction */ diff --git a/src/libxen.c b/src/libxen.c index 7e4de49acd..bd1eacaade 100644 --- a/src/libxen.c +++ b/src/libxen.c @@ -122,7 +122,6 @@ failed: xenConnectPtr xenConnectOpenReadOnly(const char *name) { xenConnectPtr ret = NULL; - int handle = -1; struct xs_handle *xshandle = NULL; /* we can only talk to the local Xen supervisor ATM */ @@ -205,6 +204,56 @@ unsigned long xenConnectGetVersion(xenConnectPtr conn) { if (conn == NULL) return(-1); + TODO + return(-1); +} + +/** + * xenConnectListDomains: + * @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 +xenConnectListDomains(xenConnectPtr conn, int *ids, int maxids) { + struct xs_transaction_handle* t; + int ret = -1; + unsigned int num, i; + long id; + char **idlist = NULL, *endptr; + + if ((conn == NULL) || (conn->magic != XEN_CONNECT_MAGIC) || + (ids == NULL) || (maxids <= 0)) + return(-1); + + t = xs_transaction_start(conn->xshandle); + if (t == NULL) + goto done; + + idlist = xs_directory(conn->xshandle, t, "/local/domain", &num); + if (idlist == NULL) + goto done; + + for (ret = 0,i = 0;(i < num) && (ret < maxids);i++) { + id = strtol(idlist[i], &endptr, 10); + if ((endptr == idlist[i]) || (*endptr != 0)) { + ret = -1; + goto done; + } + ids[ret++] = (int) id; + } + +done: + if (t != NULL) + xs_transaction_end(conn->xshandle, t, 0); + if (idlist != NULL) + free(idlist); + + return(ret); } /** @@ -260,7 +309,6 @@ xenDomainLookupByName(xenConnectPtr conn, const char *name) { static char * xenConnectDoStoreQuery(xenConnectPtr conn, const char *path) { struct xs_transaction_handle* t; - char s[256]; char *ret = NULL; unsigned int len = 0; diff --git a/src/libxen_sym.version b/src/libxen_sym.version index a015bb258f..7d36508c7e 100644 --- a/src/libxen_sym.version +++ b/src/libxen_sym.version @@ -5,6 +5,7 @@ xenConnectClose; xenConnectGetVersion; xenDomainCreateLinux; + xenConnectListDomains; xenDomainLookupByName; xenDomainLookupByID; xenDomainDestroy; diff --git a/src/xensh.c b/src/xensh.c index ea6f24ebb1..c6712663c9 100644 --- a/src/xensh.c +++ b/src/xensh.c @@ -13,12 +13,19 @@ #include #include +#define MAX_DOM 100 int errcode = 0; xenConnectPtr conn; xenDomainPtr dom0; +int ids[MAX_DOM]; + +static void printDomain(xenDomainPtr dom) { + printf("id %d: name %s\n", xenDomainGetID(dom), xenDomainGetName(dom)); +} int main(int argc, char **argv) { - int ret; + int ret, i; + xenDomainPtr dom; if (getuid() == 0) { conn = xenConnectOpen(NULL); @@ -36,9 +43,29 @@ int main(int argc, char **argv) { errcode = 2; goto done; } - printf("Dom0: name %s, id %d\n", xenDomainGetName(dom0), - xenDomainGetID(dom0)); + printf("Dom0: "); + printDomain(dom0); + + ret = xenConnectListDomains(conn, &ids[0], MAX_DOM); + if (ret < 0) { + fprintf(stderr, "Failed to list active domains\n"); + errcode = 3; + goto done; + } + printf("Found %d more active domains\n", ret - 1); + for (i = 0;i < ret;i++) { + if (ids[i] == 0) + continue; + printf(" "); + dom = xenDomainLookupByID(conn, ids[i]); + if (dom == NULL) { + printf("domain %d disapeared\n", ids[i]); + } else { + printDomain(dom); + } + } + done: if (conn != NULL) { ret = xenConnectClose(conn);