]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
* src/Makefile.am src/hash.[ch]: added hash module based on libxml2
authorDaniel Veillard <veillard@redhat.com>
Wed, 30 Nov 2005 13:20:53 +0000 (13:20 +0000)
committerDaniel Veillard <veillard@redhat.com>
Wed, 30 Nov 2005 13:20:53 +0000 (13:20 +0000)
  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
include/libxen.h
src/Makefile.am
src/libxen.c
src/libxen_sym.version
src/xensh.c

index a9c61f5c40607436a097a9a15f97185c6f64af06..8c09f04bf5661abdd1b4b848aa15974b7d7f9fc8 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+Wed Nov 30 14:18:19 CET 2005 Daniel Veillard <veillard@redhat.com>
+
+       * 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 <veillard@redhat.com>
 
        * configure.in: checking xenstore library, error out on missing libs
index 79514c6828e530eae51ed91709fc30844d6f2afd..89f61db5176486b67b8d7315ddd249d34eee3c10 100644 (file)
@@ -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);
index 3ca31023c5e5f41b2a062cc3279ec62a70bd9890..f21e663d0e71f0b31400a2ba7a380a51152fbd26 100644 (file)
@@ -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
 
index 7566b454a56075cccf05c98baa3f9df870590492..270c71aad329a20116865a27e3c74671c415c7d9 100644 (file)
 #include "libxen.h"
 
 #include <stdio.h>
+#include <stdlib.h>
+#include <xenctrl.h>
+#include <xs.h>
 #include "internal.h"
+#include "hash.h"
+
 
 /*
  * TODO:
@@ -23,6 +28,7 @@
  */
 
 #define XEN_CONNECT_MAGIC 0x4F23DEAD
+
 /**
  * _xenConnect:
  *
 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);
index fa49bf78d98d9bde20486b5d56f87c40b9bc9ff9..6b9adb4d4e94dcdf7c49166ca08136da4363590f 100644 (file)
@@ -4,11 +4,13 @@
        xenCloseConnect;
        xenGetVersion;
        xenCreateLinuxDomain;
-       xenLookupDomain;
+       xenDomainByName;
+       xenDomainByID;
        xenDestroyDomain;
        xenSuspendDomain;
        xenResumeDomain;
        xenGetName;
+       xenGetID;
        xenGetMaxMemory;
        xenSetMaxMemory;
     local: *;
index 6299892c180e87e99e0a68c1116983098616f14c..afca43e8fd372dce0ea1e6ed4cdd2252c4f9dc30 100644 (file)
@@ -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);
 }