mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-12-22 13:45:38 +00:00
* include/libxen.h src/libxen.c src/libxen_sym.version: add read-only
connections for normal users (but need /var/run/xenstored/socket_ro to be chmoded to 666) * src/xensh.c: if not root use the RO access Daniel
This commit is contained in:
parent
363bb8380f
commit
e6b590c8a9
@ -1,3 +1,10 @@
|
|||||||
|
Thu Dec 1 17:34:23 CET 2005 Daniel Veillard <veillard@redhat.com>
|
||||||
|
|
||||||
|
* include/libxen.h src/libxen.c src/libxen_sym.version: add read-only
|
||||||
|
connections for normal users (but need /var/run/xenstored/socket_ro
|
||||||
|
to be chmoded to 666)
|
||||||
|
* src/xensh.c: if not root use the RO access
|
||||||
|
|
||||||
Thu Dec 1 11:50:16 CET 2005 Daniel Veillard <veillard@redhat.com>
|
Thu Dec 1 11:50:16 CET 2005 Daniel Veillard <veillard@redhat.com>
|
||||||
|
|
||||||
* include/libxen.h src/libxen.c src/libxen_sym.version src/xensh.c:
|
* include/libxen.h src/libxen.c src/libxen_sym.version src/xensh.c:
|
||||||
|
@ -61,6 +61,7 @@ typedef enum {
|
|||||||
* Connection and disconnections to the Hypervisor
|
* Connection and disconnections to the Hypervisor
|
||||||
*/
|
*/
|
||||||
xenConnectPtr xenConnectOpen (const char *name);
|
xenConnectPtr xenConnectOpen (const char *name);
|
||||||
|
xenConnectPtr xenConnectOpenReadOnly (const char *name);
|
||||||
int xenConnectClose (xenConnectPtr conn);
|
int xenConnectClose (xenConnectPtr conn);
|
||||||
unsigned long xenConnectGetVersion (xenConnectPtr conn);
|
unsigned long xenConnectGetVersion (xenConnectPtr conn);
|
||||||
|
|
||||||
|
120
src/libxen.c
120
src/libxen.c
@ -29,6 +29,11 @@
|
|||||||
|
|
||||||
#define XEN_CONNECT_MAGIC 0x4F23DEAD
|
#define XEN_CONNECT_MAGIC 0x4F23DEAD
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Flags for Xen connections
|
||||||
|
*/
|
||||||
|
#define XEN_CONNECT_RO 1
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* _xenConnect:
|
* _xenConnect:
|
||||||
*
|
*
|
||||||
@ -39,6 +44,7 @@ struct _xenConnect {
|
|||||||
int handle; /* internal handle used for hypercall */
|
int handle; /* internal handle used for hypercall */
|
||||||
struct xs_handle *xshandle; /* handle to talk to the xenstore */
|
struct xs_handle *xshandle; /* handle to talk to the xenstore */
|
||||||
xenHashTablePtr domains; /* hash table for known domains */
|
xenHashTablePtr domains; /* hash table for known domains */
|
||||||
|
int flags; /* a set of connection flags */
|
||||||
};
|
};
|
||||||
|
|
||||||
#define XEN_DOMAIN_MAGIC 0xDEAD4321
|
#define XEN_DOMAIN_MAGIC 0xDEAD4321
|
||||||
@ -52,6 +58,7 @@ struct _xenDomain {
|
|||||||
unsigned int magic; /* specific value to check */
|
unsigned int magic; /* specific value to check */
|
||||||
xenConnectPtr conn; /* pointer back to the connection */
|
xenConnectPtr conn; /* pointer back to the connection */
|
||||||
char *name; /* the domain external name */
|
char *name; /* the domain external name */
|
||||||
|
char *path; /* the domain internal path */
|
||||||
int handle; /* internal handle for the dmonain ID */
|
int handle; /* internal handle for the dmonain ID */
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -66,7 +73,7 @@ struct _xenDomain {
|
|||||||
*/
|
*/
|
||||||
xenConnectPtr
|
xenConnectPtr
|
||||||
xenConnectOpen(const char *name) {
|
xenConnectOpen(const char *name) {
|
||||||
xenConnectPtr ret;
|
xenConnectPtr ret = NULL;
|
||||||
int handle = -1;
|
int handle = -1;
|
||||||
struct xs_handle *xshandle = NULL;
|
struct xs_handle *xshandle = NULL;
|
||||||
|
|
||||||
@ -88,6 +95,7 @@ xenConnectOpen(const char *name) {
|
|||||||
ret->handle = handle;
|
ret->handle = handle;
|
||||||
ret->xshandle = xshandle;
|
ret->xshandle = xshandle;
|
||||||
ret->domains = xenHashCreate(20);
|
ret->domains = xenHashCreate(20);
|
||||||
|
ret->flags = 0;
|
||||||
if (ret->domains == NULL)
|
if (ret->domains == NULL)
|
||||||
goto failed;
|
goto failed;
|
||||||
|
|
||||||
@ -97,6 +105,51 @@ failed:
|
|||||||
xc_interface_close(handle);
|
xc_interface_close(handle);
|
||||||
if (xshandle != NULL)
|
if (xshandle != NULL)
|
||||||
xs_daemon_close(xshandle);
|
xs_daemon_close(xshandle);
|
||||||
|
if (ret != NULL)
|
||||||
|
free(ret);
|
||||||
|
return(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* xenConnectOpenReadOnly:
|
||||||
|
* @name: optional argument currently unused, pass NULL
|
||||||
|
*
|
||||||
|
* This function should be called first to get a read-only connection to the
|
||||||
|
* xen store. The set of APIs usable are then restricted.
|
||||||
|
*
|
||||||
|
* Returns a pointer to the hypervisor connection or NULL in case of error
|
||||||
|
*/
|
||||||
|
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 */
|
||||||
|
if (name != NULL)
|
||||||
|
return(NULL);
|
||||||
|
|
||||||
|
xshandle = xs_daemon_open_readonly();
|
||||||
|
if (xshandle == NULL)
|
||||||
|
goto failed;
|
||||||
|
|
||||||
|
ret = (xenConnectPtr) malloc(sizeof(xenConnect));
|
||||||
|
if (ret == NULL)
|
||||||
|
goto failed;
|
||||||
|
ret->magic = XEN_CONNECT_MAGIC;
|
||||||
|
ret->handle = -1;
|
||||||
|
ret->xshandle = xshandle;
|
||||||
|
ret->domains = xenHashCreate(20);
|
||||||
|
ret->flags = XEN_CONNECT_RO;
|
||||||
|
if (ret->domains == NULL)
|
||||||
|
goto failed;
|
||||||
|
|
||||||
|
return(ret);
|
||||||
|
failed:
|
||||||
|
if (xshandle != NULL)
|
||||||
|
xs_daemon_close(xshandle);
|
||||||
|
if (ret != NULL)
|
||||||
|
free(ret);
|
||||||
return(NULL);
|
return(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -133,6 +186,7 @@ xenConnectClose(xenConnectPtr conn) {
|
|||||||
conn->magic = -1;
|
conn->magic = -1;
|
||||||
xs_daemon_close(conn->xshandle);
|
xs_daemon_close(conn->xshandle);
|
||||||
conn->xshandle = NULL;
|
conn->xshandle = NULL;
|
||||||
|
if (conn->handle != -1)
|
||||||
xc_interface_close(conn->handle);
|
xc_interface_close(conn->handle);
|
||||||
conn->handle = -1;
|
conn->handle = -1;
|
||||||
free(conn);
|
free(conn);
|
||||||
@ -194,6 +248,65 @@ xenDomainLookupByName(xenConnectPtr conn, const char *name) {
|
|||||||
return(NULL);
|
return(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* xenConnectDoStoreQuery:
|
||||||
|
* @conn: pointer to the hypervisor connection
|
||||||
|
* @path: the absolute 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 *
|
||||||
|
xenConnectDoStoreQuery(xenConnectPtr conn, const char *path) {
|
||||||
|
struct xs_transaction_handle* t;
|
||||||
|
char s[256];
|
||||||
|
char *ret = NULL;
|
||||||
|
unsigned int len = 0;
|
||||||
|
|
||||||
|
t = xs_transaction_start(conn->xshandle);
|
||||||
|
if (t == NULL)
|
||||||
|
goto done;
|
||||||
|
|
||||||
|
ret = xs_read(conn->xshandle, t, path, &len);
|
||||||
|
|
||||||
|
done:
|
||||||
|
if (t != NULL)
|
||||||
|
xs_transaction_end(conn->xshandle, t, 0);
|
||||||
|
return(ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* xenDomainDoStoreQuery:
|
||||||
|
* @domain: a domain object
|
||||||
|
* @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 *
|
||||||
|
xenDomainDoStoreQuery(xenDomainPtr domain, const char *path) {
|
||||||
|
struct xs_transaction_handle* t;
|
||||||
|
char s[256];
|
||||||
|
char *ret = NULL;
|
||||||
|
unsigned int len = 0;
|
||||||
|
|
||||||
|
snprintf(s, 255, "/local/domain/%d/%s", domain->handle, path);
|
||||||
|
s[255] = 0;
|
||||||
|
|
||||||
|
t = xs_transaction_start(domain->conn->xshandle);
|
||||||
|
if (t == NULL)
|
||||||
|
goto done;
|
||||||
|
|
||||||
|
ret = xs_read(domain->conn->xshandle, t, &s[0], &len);
|
||||||
|
|
||||||
|
done:
|
||||||
|
if (t != NULL)
|
||||||
|
xs_transaction_end(domain->conn->xshandle, t, 0);
|
||||||
|
return(ret);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* xenDomainLookupByID:
|
* xenDomainLookupByID:
|
||||||
* @conn: pointer to the hypervisor connection
|
* @conn: pointer to the hypervisor connection
|
||||||
@ -213,10 +326,12 @@ xenDomainLookupByID(xenConnectPtr conn, int id) {
|
|||||||
if ((conn == NULL) || (conn->magic != XEN_CONNECT_MAGIC) || (id < 0))
|
if ((conn == NULL) || (conn->magic != XEN_CONNECT_MAGIC) || (id < 0))
|
||||||
return(NULL);
|
return(NULL);
|
||||||
|
|
||||||
|
if ((conn->flags & XEN_CONNECT_RO) == 0) {
|
||||||
res = xc_domain_getinfo(conn->handle, (uint32_t) id, 1, &info);
|
res = xc_domain_getinfo(conn->handle, (uint32_t) id, 1, &info);
|
||||||
if (res != 1) {
|
if (res != 1) {
|
||||||
return(NULL);
|
return(NULL);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
path = xs_get_domain_path(conn->xshandle, (unsigned int) id);
|
path = xs_get_domain_path(conn->xshandle, (unsigned int) id);
|
||||||
if (path == NULL) {
|
if (path == NULL) {
|
||||||
@ -230,7 +345,8 @@ xenDomainLookupByID(xenConnectPtr conn, int id) {
|
|||||||
ret->magic = XEN_DOMAIN_MAGIC;
|
ret->magic = XEN_DOMAIN_MAGIC;
|
||||||
ret->conn = conn;
|
ret->conn = conn;
|
||||||
ret->handle = id;
|
ret->handle = id;
|
||||||
ret->name = path;
|
ret->path = path;
|
||||||
|
ret->name = xenDomainDoStoreQuery(ret, "name");
|
||||||
|
|
||||||
return(ret);
|
return(ret);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
{
|
{
|
||||||
global:
|
global:
|
||||||
xenConnectOpen;
|
xenConnectOpen;
|
||||||
|
xenConnectOpenReadOnly;
|
||||||
xenConnectClose;
|
xenConnectClose;
|
||||||
xenConnectGetVersion;
|
xenConnectGetVersion;
|
||||||
xenDomainCreateLinux;
|
xenDomainCreateLinux;
|
||||||
|
@ -10,6 +10,8 @@
|
|||||||
|
|
||||||
#include "libxen.h"
|
#include "libxen.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
int errcode = 0;
|
int errcode = 0;
|
||||||
xenConnectPtr conn;
|
xenConnectPtr conn;
|
||||||
@ -18,7 +20,11 @@ xenDomainPtr dom0;
|
|||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
|
if (getuid() == 0) {
|
||||||
conn = xenConnectOpen(NULL);
|
conn = xenConnectOpen(NULL);
|
||||||
|
} else {
|
||||||
|
conn = xenConnectOpenReadOnly(NULL);
|
||||||
|
}
|
||||||
if (conn == NULL) {
|
if (conn == NULL) {
|
||||||
fprintf(stderr, "Failed to connect to the hypervisor\n");
|
fprintf(stderr, "Failed to connect to the hypervisor\n");
|
||||||
errcode = 1;
|
errcode = 1;
|
||||||
|
Loading…
Reference in New Issue
Block a user