From 978d57bbe488e38b7513c32c50b99cb8fb905222 Mon Sep 17 00:00:00 2001 From: Daniel Veillard Date: Wed, 30 Nov 2005 13:20:53 +0000 Subject: [PATCH] * src/Makefile.am src/hash.[ch]: added hash module based on libxml2 one. * include/libxen.h src/libxen.c src/libxen_sym.version: extend API start to access libxenctrl directly (need xen update to get includes) * src/xensh.c: access to both xenstore and hypervisor Daniel --- ChangeLog | 8 +++ include/libxen.h | 5 +- src/Makefile.am | 2 +- src/libxen.c | 133 +++++++++++++++++++++++++++++++++++------ src/libxen_sym.version | 4 +- src/xensh.c | 10 +++- 6 files changed, 141 insertions(+), 21 deletions(-) diff --git a/ChangeLog b/ChangeLog index a9c61f5c40..8c09f04bf5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +Wed Nov 30 14:18:19 CET 2005 Daniel Veillard + + * src/Makefile.am src/hash.[ch]: added hash module based on libxml2 + one. + * include/libxen.h src/libxen.c src/libxen_sym.version: extend API + start to access libxenctrl directly (need xen update to get includes) + * src/xensh.c: access to both xenstore and hypervisor + Tue Nov 22 17:09:11 CET 2005 Daniel Veillard * configure.in: checking xenstore library, error out on missing libs diff --git a/include/libxen.h b/include/libxen.h index 79514c6828..89f61db517 100644 --- a/include/libxen.h +++ b/include/libxen.h @@ -73,8 +73,10 @@ xenDomainPtr xenCreateLinuxDomain (xenConnectPtr conn, const char *cmdline, unsigned long memory, unsigned int flags); -xenDomainPtr xenLookupDomain (xenConnectPtr conn, +xenDomainPtr xenDomainByName (xenConnectPtr conn, const char *name); +xenDomainPtr xenDomainByID (xenConnectPtr conn, + int id); int xenDestroyDomain (xenDomainPtr domain); /* @@ -87,6 +89,7 @@ int xenResumeDomain (xenDomainPtr domain); * Dynamic control of domains */ const char * xenGetName (xenDomainPtr domain); +unsigned int xenGetID (xenDomainPtr domain); unsigned long xenGetMaxMemory (xenDomainPtr domain); int xenSetMaxMemory (xenDomainPtr domain, unsigned long memory); diff --git a/src/Makefile.am b/src/Makefile.am index 3ca31023c5..f21e663d0e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -10,7 +10,7 @@ lib_LTLIBRARIES = libxen.la libxen_la_LIBADD = libxen_la_LDFLAGS = -Wl,--version-script=$(srcdir)/libxen_sym.version \ -version-info @LIBXEN_VERSION_INFO@ -libxen_la_SOURCES = libxen.c internal.h +libxen_la_SOURCES = libxen.c internal.h hash.c hash.h noinst_PROGRAMS=xensh diff --git a/src/libxen.c b/src/libxen.c index 7566b454a5..270c71aad3 100644 --- a/src/libxen.c +++ b/src/libxen.c @@ -12,7 +12,12 @@ #include "libxen.h" #include +#include +#include +#include #include "internal.h" +#include "hash.h" + /* * TODO: @@ -23,6 +28,7 @@ */ #define XEN_CONNECT_MAGIC 0x4F23DEAD + /** * _xenConnect: * @@ -31,7 +37,22 @@ struct _xenConnect { unsigned int magic; /* specific value to check */ int handle; /* internal handle used for hypercall */ - int xshandle; /* handle to talk to the xenstore */ + struct xs_handle *xshandle; /* handle to talk to the xenstore */ + xenHashTablePtr domains; /* hash table for known domains */ +}; + +#define XEN_DOMAIN_MAGIC 0xDEAD4321 + +/** + * _xenDomain: + * + * Internal structure associated to a domain + */ +struct _xenDomain { + unsigned int magic; /* specific value to check */ + xenConnectPtr conn; /* pointer back to the connection */ + char *name; /* the domain external name */ + int handle; /* internal handle for the dmonain ID */ }; /** @@ -47,13 +68,17 @@ xenConnectPtr xenOpenConnect(const char *name) { xenConnectPtr ret; int handle = -1; - int xshandle = -1; + struct xs_handle *xshandle = NULL; + + /* we can only talk to the local Xen supervisor ATM */ + if (name != NULL) + return(NULL); handle = xc_interface_open(); if (handle == -1) goto failed; xshandle = xs_daemon_open(); - if (xshandle < 0) + if (xshandle == NULL) goto failed; ret = (xenConnectPtr) malloc(sizeof(xenConnect)); @@ -62,16 +87,32 @@ xenOpenConnect(const char *name) { ret->magic = XEN_CONNECT_MAGIC; ret->handle = handle; ret->xshandle = xshandle; + ret->domains = xenHashCreate(20); + if (ret->domains == NULL) + goto failed; return(ret); failed: if (handle >= 0) xc_interface_close(handle); - if (xshandle >= 0) + if (xshandle != NULL) xs_daemon_close(xshandle); return(NULL); } +/** + * xenDestroyDomainName: + * @domain: a domain object + * + * Destroy the domain object, this is just used by the domain hash callback. + * + * Returns 0 in case of success and -1 in case of failure. + */ +static int +xenDestroyDomainName(xenDomainPtr domain, const char *name ATTRIBUTE_UNUSED) { + return(xenDestroyDomain(domain)); +} + /** * xenCloseConnect: * @conn: pointer to the hypervisor connection @@ -88,9 +129,10 @@ xenCloseConnect(xenConnectPtr conn) { if ((conn == NULL) || (conn->magic != XEN_CONNECT_MAGIC)) return(-1); + xenHashFree(conn->domains, (xenHashDeallocator) xenDestroyDomainName); conn->magic = -1; xs_daemon_close(conn->xshandle); - conn->xshandle = -1; + conn->xshandle = NULL; xc_interface_close(conn->handle); conn->handle = -1; free(conn); @@ -128,14 +170,15 @@ xenDomainPtr xenCreateLinuxDomain(xenConnectPtr conn, const char *kernel_path, const char *initrd_path, const char *cmdline, unsigned long memory, unsigned int flags) { - if ((conn == NULL) || (kernel_path == NULL) || (memory < 4096)) + if ((conn == NULL) || (conn->magic != XEN_CONNECT_MAGIC) || + (kernel_path == NULL) || (memory < 4096)) return(NULL); TODO return(NULL); } /** - * xenLookupDomain: + * xenDomainByName: * @conn: pointer to the hypervisor connection * @name: name for the domain * @@ -144,13 +187,54 @@ xenCreateLinuxDomain(xenConnectPtr conn, const char *kernel_path, * Returns a new domain object or NULL in case of failure */ xenDomainPtr -xenLookupDomain(xenConnectPtr conn, const char *name) { - if ((conn == NULL) || (name == NULL)) +xenDomainByName(xenConnectPtr conn, const char *name) { + if ((conn == NULL) || (conn->magic != XEN_CONNECT_MAGIC) || (name == NULL)) return(NULL); TODO return(NULL); } +/** + * xenDomainByID: + * @conn: pointer to the hypervisor connection + * @id: the domain ID number + * + * Try to find a domain based on the hypervisor ID number + * + * Returns a new domain object or NULL in case of failure + */ +xenDomainPtr +xenDomainByID(xenConnectPtr conn, int id) { + char *path; + xenDomainPtr ret; + xc_dominfo_t info; + int res; + + if ((conn == NULL) || (conn->magic != XEN_CONNECT_MAGIC) || (id < 0)) + return(NULL); + + res = xc_domain_getinfo(conn->handle, (uint32_t) id, 1, &info); + if (res != 1) { + return(NULL); + } + + path = xs_get_domain_path(conn->xshandle, (unsigned int) id); + if (path == NULL) { + return(NULL); + } + ret = (xenDomainPtr) malloc(sizeof(xenDomain)); + if (ret == NULL) { + free(path); + return(NULL); + } + ret->magic = XEN_DOMAIN_MAGIC; + ret->conn = conn; + ret->handle = id; + ret->name = path; + + return(ret); +} + /** * xenDestroyDomain: * @domain: a domain object @@ -162,7 +246,7 @@ xenLookupDomain(xenConnectPtr conn, const char *name) { */ int xenDestroyDomain(xenDomainPtr domain) { - if (domain == NULL) + if ((domain == NULL) || (domain->magic != XEN_DOMAIN_MAGIC)) return(-1); TODO return(-1); @@ -181,7 +265,7 @@ xenDestroyDomain(xenDomainPtr domain) { */ int xenSuspendDomain(xenDomainPtr domain) { - if (domain == NULL) + if ((domain == NULL) || (domain->magic != XEN_DOMAIN_MAGIC)) return(-1); TODO return(-1); @@ -198,7 +282,7 @@ xenSuspendDomain(xenDomainPtr domain) { */ int xenResumeDomain(xenDomainPtr domain) { - if (domain == NULL) + if ((domain == NULL) || (domain->magic != XEN_DOMAIN_MAGIC)) return(-1); TODO return(-1); @@ -215,10 +299,24 @@ xenResumeDomain(xenDomainPtr domain) { */ const char * xenGetName(xenDomainPtr domain) { - if (domain == NULL) + if ((domain == NULL) || (domain->magic != XEN_DOMAIN_MAGIC)) return(NULL); - TODO - return(NULL); + return(domain->name); +} + +/** + * xenGetID: + * @domain: a domain object + * + * Get the hypervisor ID number for the domain + * + * Returns the domain ID number or (unsigned int) -1 in case of error + */ +unsigned int +xenGetID(xenDomainPtr domain) { + if ((domain == NULL) || (domain->magic != XEN_DOMAIN_MAGIC)) + return((unsigned int) -1); + return(domain->handle); } /** @@ -233,7 +331,7 @@ xenGetName(xenDomainPtr domain) { */ unsigned long xenGetMaxMemory(xenDomainPtr domain) { - if (domain == NULL) + if ((domain == NULL) || (domain->magic != XEN_DOMAIN_MAGIC)) return(0); TODO return(0); @@ -252,7 +350,8 @@ xenGetMaxMemory(xenDomainPtr domain) { */ int xenSetMaxMemory(xenDomainPtr domain, unsigned long memory) { - if ((domain == NULL) || (memory < 4096)) + if ((domain == NULL) || (domain->magic != XEN_DOMAIN_MAGIC) || + (memory < 4096)) return(-1); TODO return(-1); diff --git a/src/libxen_sym.version b/src/libxen_sym.version index fa49bf78d9..6b9adb4d4e 100644 --- a/src/libxen_sym.version +++ b/src/libxen_sym.version @@ -4,11 +4,13 @@ xenCloseConnect; xenGetVersion; xenCreateLinuxDomain; - xenLookupDomain; + xenDomainByName; + xenDomainByID; xenDestroyDomain; xenSuspendDomain; xenResumeDomain; xenGetName; + xenGetID; xenGetMaxMemory; xenSetMaxMemory; local: *; diff --git a/src/xensh.c b/src/xensh.c index 6299892c18..afca43e8fd 100644 --- a/src/xensh.c +++ b/src/xensh.c @@ -13,6 +13,7 @@ int errcode = 0; xenConnectPtr conn; +xenDomainPtr dom0; int main(int argc, char **argv) { int ret; @@ -23,6 +24,13 @@ int main(int argc, char **argv) { errcode = 1; goto done; } + dom0 = xenDomainByID(conn, 0); + if (dom0 == NULL) { + fprintf(stderr, "Failed to get domain 0 informations\n"); + errcode = 2; + goto done; + } + printf("Dom0: name %s, id %d\n", xenGetName(dom0), xenGetID(dom0)); done: if (conn != NULL) { @@ -33,5 +41,5 @@ done: errcode = 1; } } - exit(errcode); + return(errcode); }