+Wed Mar 15 13:10:25 CET 2006 Daniel Veillard <veillard@redhat.com>
+
+ * src/hash.c src/hash.h src/internal.h src/libvirt.c src/sexpr.c
+ src/sexpr.h src/virsh.c src/virterror.c src/xen_internal.c
+ src/xen_internal.h src/xend_internal.c src/xend_internal.h
+ src/xml.c src/xml.h: applied cb/indent to homogenize the source
+ style, as a first pass.
+
Fri Mar 10 11:07:58 CET 2006 Daniel Veillard <veillard@redhat.com>
* configure.in: applied patch for --with-xen-distdir option from
* Calculate the hash key
*/
static unsigned long
-virHashComputeKey(virHashTablePtr table, const char *name) {
+virHashComputeKey(virHashTablePtr table, const char *name)
+{
unsigned long value = 0L;
char ch;
-
+
if (name != NULL) {
- value += 30 * (*name);
- while ((ch = *name++) != 0) {
- value = value ^ ((value << 5) + (value >> 3) + (unsigned long)ch);
- }
+ value += 30 * (*name);
+ while ((ch = *name++) != 0) {
+ value =
+ value ^ ((value << 5) + (value >> 3) + (unsigned long) ch);
+ }
}
return (value % table->size);
}
* Returns the newly created object, or NULL if an error occured.
*/
virHashTablePtr
-virHashCreate(int size) {
+virHashCreate(int size)
+{
virHashTablePtr table;
-
+
if (size <= 0)
size = 256;
-
+
table = malloc(sizeof(virHashTable));
if (table) {
table->size = size;
- table->nbElems = 0;
+ table->nbElems = 0;
table->table = malloc(size * sizeof(virHashEntry));
if (table->table) {
- memset(table->table, 0, size * sizeof(virHashEntry));
- return(table);
+ memset(table->table, 0, size * sizeof(virHashEntry));
+ return (table);
}
free(table);
}
- return(NULL);
+ return (NULL);
}
/**
* Returns 0 in case of success, -1 in case of failure
*/
static int
-virHashGrow(virHashTablePtr table, int size) {
+virHashGrow(virHashTablePtr table, int size)
+{
unsigned long key;
int oldsize, i;
virHashEntryPtr iter, next;
struct _virHashEntry *oldtable;
+
#ifdef DEBUG_GROW
unsigned long nbElem = 0;
#endif
-
+
if (table == NULL)
- return(-1);
+ return (-1);
if (size < 8)
- return(-1);
+ return (-1);
if (size > 8 * 2048)
- return(-1);
+ return (-1);
oldsize = table->size;
oldtable = table->table;
if (oldtable == NULL)
- return(-1);
-
+ return (-1);
+
table->table = malloc(size * sizeof(virHashEntry));
if (table->table == NULL) {
- table->table = oldtable;
- return(-1);
+ table->table = oldtable;
+ return (-1);
}
memset(table->table, 0, size * sizeof(virHashEntry));
table->size = size;
- /* If the two loops are merged, there would be situations where
- a new entry needs to allocated and data copied into it from
- the main table. So instead, we run through the array twice, first
- copying all the elements in the main array (where we can't get
- conflicts) and then the rest, so we only free (and don't allocate)
- */
+ /* If the two loops are merged, there would be situations where
+ * a new entry needs to allocated and data copied into it from
+ * the main table. So instead, we run through the array twice, first
+ * copying all the elements in the main array (where we can't get
+ * conflicts) and then the rest, so we only free (and don't allocate)
+ */
for (i = 0; i < oldsize; i++) {
- if (oldtable[i].valid == 0)
- continue;
- key = virHashComputeKey(table, oldtable[i].name);
- memcpy(&(table->table[key]), &(oldtable[i]), sizeof(virHashEntry));
- table->table[key].next = NULL;
+ if (oldtable[i].valid == 0)
+ continue;
+ key = virHashComputeKey(table, oldtable[i].name);
+ memcpy(&(table->table[key]), &(oldtable[i]), sizeof(virHashEntry));
+ table->table[key].next = NULL;
}
for (i = 0; i < oldsize; i++) {
- iter = oldtable[i].next;
- while (iter) {
- next = iter->next;
-
- /*
- * put back the entry in the new table
- */
-
- key = virHashComputeKey(table, iter->name);
- if (table->table[key].valid == 0) {
- memcpy(&(table->table[key]), iter, sizeof(virHashEntry));
- table->table[key].next = NULL;
- free(iter);
- } else {
- iter->next = table->table[key].next;
- table->table[key].next = iter;
- }
+ iter = oldtable[i].next;
+ while (iter) {
+ next = iter->next;
+
+ /*
+ * put back the entry in the new table
+ */
+
+ key = virHashComputeKey(table, iter->name);
+ if (table->table[key].valid == 0) {
+ memcpy(&(table->table[key]), iter, sizeof(virHashEntry));
+ table->table[key].next = NULL;
+ free(iter);
+ } else {
+ iter->next = table->table[key].next;
+ table->table[key].next = iter;
+ }
#ifdef DEBUG_GROW
- nbElem++;
+ nbElem++;
#endif
- iter = next;
- }
+ iter = next;
+ }
}
free(oldtable);
#ifdef DEBUG_GROW
xmlGenericError(xmlGenericErrorContext,
- "virHashGrow : from %d to %d, %d elems\n", oldsize, size, nbElem);
+ "virHashGrow : from %d to %d, %d elems\n", oldsize,
+ size, nbElem);
#endif
- return(0);
+ return (0);
}
/**
* deallocated with @f if provided.
*/
void
-virHashFree(virHashTablePtr table, virHashDeallocator f) {
+virHashFree(virHashTablePtr table, virHashDeallocator f)
+{
int i;
virHashEntryPtr iter;
virHashEntryPtr next;
int nbElems;
if (table == NULL)
- return;
+ return;
if (table->table) {
- nbElems = table->nbElems;
- for(i = 0; (i < table->size) && (nbElems > 0); i++) {
- iter = &(table->table[i]);
- if (iter->valid == 0)
- continue;
- inside_table = 1;
- while (iter) {
- next = iter->next;
- if ((f != NULL) && (iter->payload != NULL))
- f(iter->payload, iter->name);
- if (iter->name)
- free(iter->name);
- iter->payload = NULL;
- if (!inside_table)
- free(iter);
- nbElems--;
- inside_table = 0;
- iter = next;
- }
- inside_table = 0;
- }
- free(table->table);
+ nbElems = table->nbElems;
+ for (i = 0; (i < table->size) && (nbElems > 0); i++) {
+ iter = &(table->table[i]);
+ if (iter->valid == 0)
+ continue;
+ inside_table = 1;
+ while (iter) {
+ next = iter->next;
+ if ((f != NULL) && (iter->payload != NULL))
+ f(iter->payload, iter->name);
+ if (iter->name)
+ free(iter->name);
+ iter->payload = NULL;
+ if (!inside_table)
+ free(iter);
+ nbElems--;
+ inside_table = 0;
+ iter = next;
+ }
+ inside_table = 0;
+ }
+ free(table->table);
}
free(table);
}
* Returns 0 the addition succeeded and -1 in case of error.
*/
int
-virHashAddEntry(virHashTablePtr table, const char *name,
- void *userdata) {
+virHashAddEntry(virHashTablePtr table, const char *name, void *userdata)
+{
unsigned long key, len = 0;
virHashEntryPtr entry;
virHashEntryPtr insert;
if ((table == NULL) || (name == NULL))
- return(-1);
+ return (-1);
/*
* Check for duplicate and insertion location.
*/
key = virHashComputeKey(table, name);
if (table->table[key].valid == 0) {
- insert = NULL;
+ insert = NULL;
} else {
- for (insert = &(table->table[key]); insert->next != NULL;
- insert = insert->next) {
- if (!strcmp(insert->name, name))
- return(-1);
- len++;
- }
- if (!strcmp(insert->name, name))
- return(-1);
+ for (insert = &(table->table[key]); insert->next != NULL;
+ insert = insert->next) {
+ if (!strcmp(insert->name, name))
+ return (-1);
+ len++;
+ }
+ if (!strcmp(insert->name, name))
+ return (-1);
}
if (insert == NULL) {
- entry = &(table->table[key]);
+ entry = &(table->table[key]);
} else {
- entry = malloc(sizeof(virHashEntry));
- if (entry == NULL)
- return(-1);
+ entry = malloc(sizeof(virHashEntry));
+ if (entry == NULL)
+ return (-1);
}
entry->name = strdup(name);
entry->valid = 1;
- if (insert != NULL)
- insert->next = entry;
+ if (insert != NULL)
+ insert->next = entry;
table->nbElems++;
if (len > MAX_HASH_LEN)
- virHashGrow(table, MAX_HASH_LEN * table->size);
+ virHashGrow(table, MAX_HASH_LEN * table->size);
- return(0);
+ return (0);
}
/**
*/
int
virHashUpdateEntry(virHashTablePtr table, const char *name,
- void *userdata, virHashDeallocator f) {
+ void *userdata, virHashDeallocator f)
+{
unsigned long key;
virHashEntryPtr entry;
virHashEntryPtr insert;
if ((table == NULL) || name == NULL)
- return(-1);
+ return (-1);
/*
* Check for duplicate and insertion location.
*/
key = virHashComputeKey(table, name);
if (table->table[key].valid == 0) {
- insert = NULL;
+ insert = NULL;
} else {
- for (insert = &(table->table[key]); insert->next != NULL;
- insert = insert->next) {
- if (!strcmp(insert->name, name)) {
- if (f)
- f(insert->payload, insert->name);
- insert->payload = userdata;
- return(0);
- }
- }
- if (!strcmp(insert->name, name)) {
- if (f)
- f(insert->payload, insert->name);
- insert->payload = userdata;
- return(0);
- }
+ for (insert = &(table->table[key]); insert->next != NULL;
+ insert = insert->next) {
+ if (!strcmp(insert->name, name)) {
+ if (f)
+ f(insert->payload, insert->name);
+ insert->payload = userdata;
+ return (0);
+ }
+ }
+ if (!strcmp(insert->name, name)) {
+ if (f)
+ f(insert->payload, insert->name);
+ insert->payload = userdata;
+ return (0);
+ }
}
if (insert == NULL) {
- entry = &(table->table[key]);
+ entry = &(table->table[key]);
} else {
- entry = malloc(sizeof(virHashEntry));
- if (entry == NULL)
- return(-1);
+ entry = malloc(sizeof(virHashEntry));
+ if (entry == NULL)
+ return (-1);
}
entry->name = strdup(name);
if (insert != NULL) {
- insert->next = entry;
+ insert->next = entry;
}
- return(0);
+ return (0);
}
/**
* Returns the a pointer to the userdata
*/
void *
-virHashLookup(virHashTablePtr table, const char *name) {
+virHashLookup(virHashTablePtr table, const char *name)
+{
unsigned long key;
virHashEntryPtr entry;
if (table == NULL)
- return(NULL);
+ return (NULL);
if (name == NULL)
- return(NULL);
+ return (NULL);
key = virHashComputeKey(table, name);
if (table->table[key].valid == 0)
- return(NULL);
+ return (NULL);
for (entry = &(table->table[key]); entry != NULL; entry = entry->next) {
- if (!strcmp(entry->name, name))
- return(entry->payload);
+ if (!strcmp(entry->name, name))
+ return (entry->payload);
}
- return(NULL);
+ return (NULL);
}
/**
* -1 in case of error
*/
int
-virHashSize(virHashTablePtr table) {
+virHashSize(virHashTablePtr table)
+{
if (table == NULL)
- return(-1);
- return(table->nbElems);
+ return (-1);
+ return (table->nbElems);
}
/**
*/
int
virHashRemoveEntry(virHashTablePtr table, const char *name,
- virHashDeallocator f) {
+ virHashDeallocator f)
+{
unsigned long key;
virHashEntryPtr entry;
virHashEntryPtr prev = NULL;
if (table == NULL || name == NULL)
- return(-1);
+ return (-1);
key = virHashComputeKey(table, name);
if (table->table[key].valid == 0) {
- return(-1);
+ return (-1);
} else {
- for (entry = &(table->table[key]); entry != NULL; entry = entry->next) {
+ for (entry = &(table->table[key]); entry != NULL;
+ entry = entry->next) {
if (!strcmp(entry->name, name)) {
if ((f != NULL) && (entry->payload != NULL))
f(entry->payload, entry->name);
entry->payload = NULL;
- if(entry->name)
- free(entry->name);
- if(prev) {
+ if (entry->name)
+ free(entry->name);
+ if (prev) {
prev->next = entry->next;
- free(entry);
- } else {
- if (entry->next == NULL) {
- entry->valid = 0;
- } else {
- entry = entry->next;
- memcpy(&(table->table[key]), entry, sizeof(virHashEntry));
- free(entry);
- }
- }
+ free(entry);
+ } else {
+ if (entry->next == NULL) {
+ entry->valid = 0;
+ } else {
+ entry = entry->next;
+ memcpy(&(table->table[key]), entry,
+ sizeof(virHashEntry));
+ free(entry);
+ }
+ }
table->nbElems--;
- return(0);
+ return (0);
}
prev = entry;
}
- return(-1);
+ return (-1);
}
}
-
/*
* The hash table.
*/
-typedef struct _virHashTable virHashTable;
-typedef virHashTable *virHashTablePtr;
+ typedef struct _virHashTable virHashTable;
+ typedef virHashTable *virHashTablePtr;
/*
* function types:
*/
+
/**
* virHashDeallocator:
* @payload: the data in the hash
*
* Callback to free data from a hash.
*/
-typedef void (*virHashDeallocator)(void *payload, char *name);
+ typedef void (*virHashDeallocator) (void *payload, char *name);
/*
* Constructor and destructor.
*/
-virHashTablePtr virHashCreate (int size);
-void
- virHashFree (virHashTablePtr table,
- virHashDeallocator f);
-int virHashSize (virHashTablePtr table);
+ virHashTablePtr virHashCreate(int size);
+ void
+ virHashFree(virHashTablePtr table, virHashDeallocator f);
+ int virHashSize(virHashTablePtr table);
/*
* Add a new entry to the hash table.
*/
-int virHashAddEntry (virHashTablePtr table,
- const char *name,
- void *userdata);
-int virHashUpdateEntry(virHashTablePtr table,
- const char *name,
- void *userdata,
- virHashDeallocator f);
+ int virHashAddEntry(virHashTablePtr table,
+ const char *name, void *userdata);
+ int virHashUpdateEntry(virHashTablePtr table,
+ const char *name,
+ void *userdata, virHashDeallocator f);
/*
* Remove an entry from the hash table.
*/
-int virHashRemoveEntry(virHashTablePtr table,
- const char *name,
- virHashDeallocator f);
+ int virHashRemoveEntry(virHashTablePtr table,
+ const char *name, virHashDeallocator f);
+
/*
* Retrieve the userdata.
*/
-void * virHashLookup (virHashTablePtr table,
- const char *name);
+ void *virHashLookup(virHashTablePtr table, const char *name);
#ifdef __cplusplus
}
#endif
-#endif /* ! __VIR_HASH_H__ */
+#endif /* ! __VIR_HASH_H__ */
*
* Internal structure associated to a connection
*/
-struct _virConnect {
- unsigned int magic; /* specific value to check */
- int handle; /* internal handle used for hypercall */
- struct xs_handle *xshandle; /* handle to talk to the xenstore */
-
- /* connection to xend */
- int type; /* PF_UNIX or PF_INET */
- int len; /* lenght of addr */
- struct sockaddr *addr; /* type of address used */
- struct sockaddr_un addr_un; /* the unix address */
- struct sockaddr_in addr_in; /* the inet address */
-
- /* error stuff */
- virError err; /* the last error */
- virErrorFunc handler; /* associated handlet */
- void *userData; /* the user data */
-
- /* misc */
- virHashTablePtr domains; /* hash table for known domains */
- int flags; /* a set of connection flags */
-};
+ struct _virConnect {
+ unsigned int magic; /* specific value to check */
+ int handle; /* internal handle used for hypercall */
+ struct xs_handle *xshandle; /* handle to talk to the xenstore */
+
+ /* connection to xend */
+ int type; /* PF_UNIX or PF_INET */
+ int len; /* lenght of addr */
+ struct sockaddr *addr; /* type of address used */
+ struct sockaddr_un addr_un; /* the unix address */
+ struct sockaddr_in addr_in; /* the inet address */
+
+ /* error stuff */
+ virError err; /* the last error */
+ virErrorFunc handler; /* associated handlet */
+ void *userData; /* the user data */
+
+ /* misc */
+ virHashTablePtr domains; /* hash table for known domains */
+ int flags; /* a set of connection flags */
+ };
/**
* virDomainFlags:
* a set of special flag values associated to the domain
*/
-enum {
- DOMAIN_IS_SHUTDOWN = (1 << 0) /* the domain is being shutdown */
-} virDomainFlags;
+ enum {
+ DOMAIN_IS_SHUTDOWN = (1 << 0) /* the domain is being shutdown */
+ } virDomainFlags;
/**
* _virDomain:
*
* Internal structure associated to a domain
*/
-struct _virDomain {
- unsigned int magic; /* specific value to check */
- virConnectPtr conn; /* pointer back to the connection */
- char *name; /* the domain external name */
- char *path; /* the domain internal path */
- int handle; /* internal handle for the dmonain ID */
- int flags; /* extra flags */
- unsigned char uuid[16]; /* the domain unique identifier */
-};
+ struct _virDomain {
+ unsigned int magic; /* specific value to check */
+ virConnectPtr conn; /* pointer back to the connection */
+ char *name; /* the domain external name */
+ char *path; /* the domain internal path */
+ int handle; /* internal handle for the dmonain ID */
+ int flags; /* extra flags */
+ unsigned char uuid[16]; /* the domain unique identifier */
+ };
/*
* Internal routines
*/
-char * virDomainGetVM (virDomainPtr domain);
-char * virDomainGetVMInfo (virDomainPtr domain,
- const char *vm,
- const char *name);
-
-void __virRaiseError (virConnectPtr conn,
- virDomainPtr dom,
- int domain,
- int code,
- virErrorLevel level,
- const char *str1,
- const char *str2,
- const char *str3,
- int int1,
- int int2,
- const char *msg,
- ...);
-const char * __virErrorMsg (virErrorNumber error,
- const char *info);
+ char *virDomainGetVM(virDomainPtr domain);
+ char *virDomainGetVMInfo(virDomainPtr domain,
+ const char *vm, const char *name);
+
+ void __virRaiseError(virConnectPtr conn,
+ virDomainPtr dom,
+ int domain,
+ int code,
+ virErrorLevel level,
+ const char *str1,
+ const char *str2,
+ const char *str3,
+ int int1, int int2, const char *msg, ...);
+ const char *__virErrorMsg(virErrorNumber error, const char *info);
#ifdef __cplusplus
}
-#endif /* __cplusplus */
-
-#endif /* __VIR_INTERNAL_H__ */
+#endif /* __cplusplus */
+#endif /* __VIR_INTERNAL_H__ */
* Handle an error at the connection level
*/
static void
-virLibConnError(virConnectPtr conn, virErrorNumber error, const char *info) {
+virLibConnError(virConnectPtr conn, virErrorNumber error, const char *info)
+{
const char *errmsg;
-
+
if (error == VIR_ERR_OK)
return;
* Handle an error at the connection level
*/
static void
-virLibDomainError(virDomainPtr domain, virErrorNumber error, const char *info) {
+virLibDomainError(virDomainPtr domain, virErrorNumber error,
+ const char *info)
+{
virConnectPtr conn = NULL;
const char *errmsg;
-
+
if (error == VIR_ERR_OK)
return;
* @typeVer have the format major * 1,000,000 + minor * 1,000 + release.
*/
int
-virGetVersion(unsigned long *libVer, const char *type, unsigned long *typeVer) {
+virGetVersion(unsigned long *libVer, const char *type,
+ unsigned long *typeVer)
+{
if (libVer == NULL)
- return(-1);
+ return (-1);
*libVer = LIBVIR_VERSION_NUMBER;
if (typeVer != NULL) {
- if ((type == NULL) || (!strcasecmp(type, "Xen"))) {
- if ((DOM0_INTERFACE_VERSION & 0xFFFF0000) == (0xAAAA0000)) {
- /* one time glitch hopefully ! */
+ if ((type == NULL) || (!strcasecmp(type, "Xen"))) {
+ if ((DOM0_INTERFACE_VERSION & 0xFFFF0000) == (0xAAAA0000)) {
+ /* one time glitch hopefully ! */
*typeVer = 2 * 1000000 +
- ((DOM0_INTERFACE_VERSION >> 8) & 0xFF) * 1000 +
- (DOM0_INTERFACE_VERSION & 0xFF);
- } else {
- *typeVer = (DOM0_INTERFACE_VERSION >> 24) * 1000000 +
- ((DOM0_INTERFACE_VERSION >> 16) & 0xFF) * 1000 +
- (DOM0_INTERFACE_VERSION & 0xFFFF);
- }
- } else {
- *typeVer = 0;
- virLibConnError(NULL, VIR_ERR_NO_SUPPORT, "type");
- return(-1);
- }
- }
- return(0);
+ ((DOM0_INTERFACE_VERSION >> 8) & 0xFF) * 1000 +
+ (DOM0_INTERFACE_VERSION & 0xFF);
+ } else {
+ *typeVer = (DOM0_INTERFACE_VERSION >> 24) * 1000000 +
+ ((DOM0_INTERFACE_VERSION >> 16) & 0xFF) * 1000 +
+ (DOM0_INTERFACE_VERSION & 0xFFFF);
+ }
+ } else {
+ *typeVer = 0;
+ virLibConnError(NULL, VIR_ERR_NO_SUPPORT, "type");
+ return (-1);
+ }
+ }
+ return (0);
}
/**
* Returns a pointer to the hypervisor connection or NULL in case of error
*/
virConnectPtr
-virConnectOpen(const char *name) {
+virConnectOpen(const char *name)
+{
virConnectPtr ret = NULL;
int handle = -1;
struct xs_handle *xshandle = NULL;
/* we can only talk to the local Xen supervisor ATM */
if (name != NULL) {
- virLibConnError(NULL, VIR_ERR_NO_SUPPORT, name);
- return(NULL);
+ virLibConnError(NULL, VIR_ERR_NO_SUPPORT, name);
+ return (NULL);
}
handle = xenHypervisorOpen(0);
}
xshandle = xs_daemon_open();
if (xshandle == NULL) {
- virLibConnError(NULL, VIR_ERR_NO_CONNECT, "XenStore");
+ virLibConnError(NULL, VIR_ERR_NO_CONNECT, "XenStore");
goto failed;
}
if (ret->domains == NULL)
goto failed;
- return(ret);
-failed:
+ return (ret);
+ failed:
if (handle >= 0)
xenHypervisorClose(handle);
if (xshandle != NULL)
xs_daemon_close(xshandle);
if (ret != NULL)
free(ret);
- return(NULL);
+ return (NULL);
}
/**
* Returns a pointer to the hypervisor connection or NULL in case of error
*/
virConnectPtr
-virConnectOpenReadOnly(const char *name) {
+virConnectOpenReadOnly(const char *name)
+{
int method = 0;
int handle;
virConnectPtr ret = NULL;
/* we can only talk to the local Xen supervisor ATM */
if (name != NULL) {
- virLibConnError(NULL, VIR_ERR_NO_SUPPORT, name);
- return(NULL);
+ virLibConnError(NULL, VIR_ERR_NO_SUPPORT, name);
+ return (NULL);
}
handle = xenHypervisorOpen(1);
ret->flags = VIR_CONNECT_RO;
if (method == 0) {
virLibConnError(NULL, VIR_ERR_NO_CONNECT,
- "could not connect to Xen Daemon nor Xen Store");
+ "could not connect to Xen Daemon nor Xen Store");
goto failed;
}
- return(ret);
-failed:
+ return (ret);
+ failed:
if (handle >= 0)
xenHypervisorClose(handle);
if (xshandle != NULL)
xs_daemon_close(xshandle);
if (ret != NULL) {
if (ret->domains != NULL)
- virHashFree(ret->domains, NULL);
+ virHashFree(ret->domains, NULL);
free(ret);
}
- return(NULL);
+ return (NULL);
}
/**
* Returns -1 if the check failed, 0 if successful or not possible to check
*/
static int
-virConnectCheckStoreID(virConnectPtr conn, int id) {
+virConnectCheckStoreID(virConnectPtr conn, int id)
+{
if (conn->handle >= 0) {
- dom0_getdomaininfo_t dominfo;
- int tmp;
+ dom0_getdomaininfo_t dominfo;
+ int tmp;
- dominfo.domain = id;
- tmp = xenHypervisorGetDomainInfo(conn->handle, id, &dominfo);
- if (tmp < 0)
- return(-1);
+ dominfo.domain = id;
+ tmp = xenHypervisorGetDomainInfo(conn->handle, id, &dominfo);
+ if (tmp < 0)
+ return (-1);
}
- return(0);
+ return (0);
}
/**
* Returns 0 in case of success and -1 in case of failure.
*/
static int
-virDomainFreeName(virDomainPtr domain, const char *name ATTRIBUTE_UNUSED) {
- return(virDomainFree(domain));
+virDomainFreeName(virDomainPtr domain, const char *name ATTRIBUTE_UNUSED)
+{
+ return (virDomainFree(domain));
}
/**
* Returns 0 in case of success or -1 in case of error.
*/
int
-virConnectClose(virConnectPtr conn) {
+virConnectClose(virConnectPtr conn)
+{
xend_cleanup(conn);
if (!VIR_IS_CONNECT(conn))
- return(-1);
+ return (-1);
virHashFree(conn->domains, (virHashDeallocator) virDomainFreeName);
conn->magic = -1;
if (conn->xshandle != NULL)
- xs_daemon_close(conn->xshandle);
+ xs_daemon_close(conn->xshandle);
conn->xshandle = NULL;
if (conn->handle != -1)
- xenHypervisorClose(conn->handle);
+ xenHypervisorClose(conn->handle);
conn->handle = -1;
free(conn);
- return(0);
+ return (0);
}
/**
* Returns NULL in case of error, a static zero terminated string otherwise.
*/
const char *
-virConnectGetType(virConnectPtr conn) {
+virConnectGetType(virConnectPtr conn)
+{
if (!VIR_IS_CONNECT(conn)) {
- virLibConnError(conn, VIR_ERR_INVALID_CONN, "in virConnectGetType");
- return(NULL);
+ virLibConnError(conn, VIR_ERR_INVALID_CONN,
+ "in virConnectGetType");
+ return (NULL);
}
- return("Xen");
+ return ("Xen");
}
/**
* @hvVer value is major * 1,000,000 + minor * 1,000 + release
*/
int
-virConnectGetVersion(virConnectPtr conn, unsigned long *hvVer) {
+virConnectGetVersion(virConnectPtr conn, unsigned long *hvVer)
+{
unsigned long ver;
if (!VIR_IS_CONNECT(conn)) {
virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
- return(-1);
+ return (-1);
}
-
+
if (hvVer == NULL) {
virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
- return(-1);
+ return (-1);
}
-
+
/* this can't be extracted from the Xenstore */
if (conn->handle < 0) {
*hvVer = 0;
- return(0);
+ return (0);
}
ver = xenHypervisorGetVersion(conn->handle);
*hvVer = (ver >> 16) * 1000000 + (ver & 0xFFFF) * 1000;
- return(0);
+ return (0);
}
/**
* Returns the number of domain found or -1 in case of error
*/
int
-virConnectListDomains(virConnectPtr conn, int *ids, int maxids) {
+virConnectListDomains(virConnectPtr conn, int *ids, int maxids)
+{
int ret = -1;
unsigned int num, i;
long id;
if (!VIR_IS_CONNECT(conn)) {
virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
- return(-1);
+ return (-1);
}
-
+
if ((ids == NULL) || (maxids <= 0)) {
virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
- return(-1);
+ return (-1);
}
-
+
idlist = xend_get_domains(conn);
if (idlist != NULL) {
- for (ret = 0,i = 0;(idlist[i] != NULL) && (ret < maxids);i++) {
- id = xend_get_domain_ids(conn, idlist[i], NULL);
- if (id >= 0)
- ids[ret++] = (int) id;
- }
- goto done;
+ for (ret = 0, i = 0; (idlist[i] != NULL) && (ret < maxids); i++) {
+ id = xend_get_domain_ids(conn, idlist[i], NULL);
+ if (id >= 0)
+ ids[ret++] = (int) id;
+ }
+ goto done;
}
if (conn->xshandle != NULL) {
- idlist = xs_directory(conn->xshandle, 0, "/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;
- }
- if (virConnectCheckStoreID(conn, (int) id) < 0)
- continue;
- ids[ret++] = (int) id;
- }
- }
-
-done:
+ idlist = xs_directory(conn->xshandle, 0, "/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;
+ }
+ if (virConnectCheckStoreID(conn, (int) id) < 0)
+ continue;
+ ids[ret++] = (int) id;
+ }
+ }
+
+ done:
if (idlist != NULL)
free(idlist);
- return(ret);
+ return (ret);
}
/**
* Returns the number of domain found or -1 in case of error
*/
int
-virConnectNumOfDomains(virConnectPtr conn) {
+virConnectNumOfDomains(virConnectPtr conn)
+{
int ret = -1;
unsigned int num;
char **idlist = NULL;
if (!VIR_IS_CONNECT(conn)) {
virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
- return(-1);
+ return (-1);
}
/*
ret = 0;
while (*tmp != NULL) {
- tmp++;
- ret++;
- }
-
+ tmp++;
+ ret++;
+ }
+
} else if (conn->xshandle != NULL) {
idlist = xs_directory(conn->xshandle, 0, "/local/domain", &num);
- if (idlist) {
- free(idlist);
- ret = num;
- }
+ if (idlist) {
+ free(idlist);
+ ret = num;
+ }
}
- return(ret);
+ return (ret);
}
/**
* Returns a new domain object or NULL in case of failure
*/
virDomainPtr
-virDomainCreateLinux(virConnectPtr conn,
+virDomainCreateLinux(virConnectPtr conn,
const char *xmlDesc,
- unsigned int flags ATTRIBUTE_UNUSED) {
+ unsigned int flags ATTRIBUTE_UNUSED)
+{
int ret;
char *sexpr;
char *name = NULL;
if (!VIR_IS_CONNECT(conn)) {
virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
- return(NULL);
+ return (NULL);
}
if (xmlDesc == NULL) {
virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
- return(NULL);
+ return (NULL);
}
sexpr = virDomainParseXMLDesc(xmlDesc, &name);
if ((sexpr == NULL) || (name == NULL)) {
if (sexpr != NULL)
- free(sexpr);
+ free(sexpr);
if (name != NULL)
- free(name);
+ free(name);
- return(NULL);
+ return (NULL);
}
ret = xend_create_sexpr(conn, sexpr);
free(sexpr);
if (ret != 0) {
fprintf(stderr, "Failed to create domain %s\n", name);
- goto error;
+ goto error;
}
ret = xend_wait_for_devices(conn, name);
if (ret != 0) {
fprintf(stderr, "Failed to get devices for domain %s\n", name);
- xend_destroy(conn, name);
- goto error;
+ xend_destroy(conn, name);
+ goto error;
}
ret = xend_unpause(conn, name);
if (ret != 0) {
fprintf(stderr, "Failed to resume new domain %s\n", name);
- xend_destroy(conn, name);
- goto error;
+ xend_destroy(conn, name);
+ goto error;
}
dom = virDomainLookupByName(conn, name);
free(name);
- return(dom);
-error:
+ return (dom);
+ error:
if (name != NULL)
free(name);
- return(NULL);
+ return (NULL);
}
/**
* 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) {
+virConnectDoStoreList(virConnectPtr conn, const char *path,
+ unsigned int *nb)
+{
if ((conn == NULL) || (conn->xshandle == NULL) || (path == NULL) ||
(nb == NULL))
- return(NULL);
+ return (NULL);
return xs_directory(conn->xshandle, 0, path, nb);
}
* Returns a string which must be freed by the caller or NULL in case of error
*/
static char *
-virDomainDoStoreQuery(virDomainPtr domain, const char *path) {
+virDomainDoStoreQuery(virDomainPtr domain, const char *path)
+{
char s[256];
unsigned int len = 0;
-
+
if (!VIR_IS_CONNECTED_DOMAIN(domain))
- return(NULL);
+ return (NULL);
if (domain->conn->xshandle == NULL)
- return(NULL);
+ return (NULL);
snprintf(s, 255, "/local/domain/%d/%s", domain->handle, path);
s[255] = 0;
*/
static int
virDomainDoStoreWrite(virDomainPtr domain, const char *path,
- const char *value) {
+ const char *value)
+{
char s[256];
int ret = -1;
if (!VIR_IS_CONNECTED_DOMAIN(domain))
- return(-1);
+ return (-1);
if (domain->conn->xshandle == NULL)
- return(-1);
+ return (-1);
if (domain->conn->flags & VIR_CONNECT_RO)
- return(-1);
+ return (-1);
snprintf(s, 255, "/local/domain/%d/%s", domain->handle, path);
s[255] = 0;
if (xs_write(domain->conn->xshandle, 0, &s[0], value, strlen(value)))
ret = 0;
- return(ret);
+ return (ret);
}
/**
char *vm;
char query[200];
unsigned int len;
-
+
if (!VIR_IS_CONNECTED_DOMAIN(domain))
- return(NULL);
+ return (NULL);
if (domain->conn->xshandle == NULL)
- return(NULL);
-
- snprintf(query, 199, "/local/domain/%d/vm",
- virDomainGetID(domain));
+ return (NULL);
+
+ snprintf(query, 199, "/local/domain/%d/vm", virDomainGetID(domain));
query[199] = 0;
vm = xs_read(domain->conn->xshandle, 0, &query[0], &len);
- return(vm);
+ return (vm);
}
/**
* Returns the new string or NULL in case of error
*/
char *
-virDomainGetVMInfo(virDomainPtr domain, const char *vm,
- const char *name) {
+virDomainGetVMInfo(virDomainPtr domain, const char *vm, const char *name)
+{
char s[256];
char *ret = NULL;
unsigned int len = 0;
-
+
if (!VIR_IS_CONNECTED_DOMAIN(domain))
- return(NULL);
- if (domain->conn->xshandle==NULL)
- return(NULL);
-
+ return (NULL);
+ if (domain->conn->xshandle == NULL)
+ return (NULL);
+
snprintf(s, 255, "%s/%s", vm, name);
s[255] = 0;
ret = xs_read(domain->conn->xshandle, 0, &s[0], &len);
- return(ret);
+ return (ret);
}
/**
* Returns a new domain object or NULL in case of failure
*/
virDomainPtr
-virDomainLookupByID(virConnectPtr conn, int id) {
+virDomainLookupByID(virConnectPtr conn, int id)
+{
char *path = NULL;
virDomainPtr ret;
char *name = NULL;
if (!VIR_IS_CONNECT(conn)) {
virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
- return(NULL);
+ return (NULL);
}
if (id < 0) {
virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
- return(NULL);
+ return (NULL);
}
/* lookup is easier with the Xen store so try it first */
if (conn->xshandle != NULL) {
- path = xs_get_domain_path(conn->xshandle, (unsigned int) id);
+ path = xs_get_domain_path(conn->xshandle, (unsigned int) id);
}
/* fallback to xend API then */
if (path == NULL) {
char **names = xend_get_domains(conn);
- char **tmp = names;
- int ident;
-
- if (names != NULL) {
- while (*tmp != NULL) {
- ident = xend_get_domain_ids(conn, *tmp, &uuid[0]);
- if (ident == id) {
- name = strdup(*tmp);
- break;
- }
- tmp++;
- }
- free(names);
- }
+ char **tmp = names;
+ int ident;
+
+ if (names != NULL) {
+ while (*tmp != NULL) {
+ ident = xend_get_domain_ids(conn, *tmp, &uuid[0]);
+ if (ident == id) {
+ name = strdup(*tmp);
+ break;
+ }
+ tmp++;
+ }
+ free(names);
+ }
}
ret = (virDomainPtr) malloc(sizeof(virDomain));
ret->handle = id;
ret->path = path;
if (name == NULL) {
- ret->name = virDomainDoStoreQuery(ret, "name");
+ ret->name = virDomainDoStoreQuery(ret, "name");
} else {
ret->name = name;
memcpy(&ret->uuid[0], uuid, 16);
goto error;
}
- return(ret);
-error:
+ return (ret);
+ error:
if (ret != NULL)
- free(path);
+ free(path);
if (path != NULL)
- free(path);
+ free(path);
if (path != NULL)
- free(path);
- return(NULL);
+ free(path);
+ return (NULL);
}
/**
* Returns a new domain object or NULL in case of failure
*/
virDomainPtr
-virDomainLookupByUUID(virConnectPtr conn, const unsigned char *uuid) {
+virDomainLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
+{
virDomainPtr ret;
char *name = NULL;
char **names;
if (!VIR_IS_CONNECT(conn)) {
virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
- return(NULL);
+ return (NULL);
}
if (uuid == NULL) {
virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
- return(NULL);
+ return (NULL);
}
names = xend_get_domains(conn);
tmp = names;
if (names == NULL) {
- TODO /* try to fallback to xenstore lookup */
- return(NULL);
+ TODO /* try to fallback to xenstore lookup */
+ return (NULL);
}
while (*tmp != NULL) {
- id = xend_get_domain_ids(conn, *tmp, &ident[0]);
- if (id >= 0) {
- if (!memcmp(uuid, ident, 16)) {
- name = strdup(*tmp);
- break;
- }
- }
- tmp++;
+ id = xend_get_domain_ids(conn, *tmp, &ident[0]);
+ if (id >= 0) {
+ if (!memcmp(uuid, ident, 16)) {
+ name = strdup(*tmp);
+ break;
+ }
+ }
+ tmp++;
}
free(names);
if (name == NULL)
- return(NULL);
+ return (NULL);
ret = (virDomainPtr) malloc(sizeof(virDomain));
if (ret == NULL) {
if (name != NULL)
- free(name);
- return(NULL);
+ free(name);
+ return (NULL);
}
memset(ret, 0, sizeof(virDomain));
ret->magic = VIR_DOMAIN_MAGIC;
ret->path = 0;
memcpy(ret->uuid, uuid, 16);
- return(ret);
+ return (ret);
}
/**
* Returns a new domain object or NULL in case of failure
*/
virDomainPtr
-virDomainLookupByName(virConnectPtr conn, const char *name) {
+virDomainLookupByName(virConnectPtr conn, const char *name)
+{
virDomainPtr ret = NULL;
unsigned int num, i, len;
long id = -1;
if (!VIR_IS_CONNECT(conn)) {
virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
- return(NULL);
+ return (NULL);
}
if (name == NULL) {
virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
- return(NULL);
+ return (NULL);
}
/* try first though Xend */
xenddomain = xend_get_domain(conn, name);
if (xenddomain != NULL) {
id = xenddomain->live->id;
- uuid = xenddomain->uuid;
- found = 1;
- goto do_found;
+ uuid = xenddomain->uuid;
+ found = 1;
+ goto do_found;
}
/* then though the XenStore */
if (idlist == NULL)
goto done;
- for (i = 0;i < num;i++) {
- id = strtol(idlist[i], &endptr, 10);
- if ((endptr == idlist[i]) || (*endptr != 0)) {
- goto done;
- }
- if (virConnectCheckStoreID(conn, (int) id) < 0)
- continue;
- snprintf(prop, 199, "/local/domain/%s/name", idlist[i]);
- prop[199] = 0;
- tmp = xs_read(conn->xshandle, 0, prop, &len);
- if (tmp != NULL) {
- found = !strcmp(name, tmp);
- free(tmp);
- if (found)
- break;
- }
+ for (i = 0; i < num; i++) {
+ id = strtol(idlist[i], &endptr, 10);
+ if ((endptr == idlist[i]) || (*endptr != 0)) {
+ goto done;
+ }
+ if (virConnectCheckStoreID(conn, (int) id) < 0)
+ continue;
+ snprintf(prop, 199, "/local/domain/%s/name", idlist[i]);
+ prop[199] = 0;
+ tmp = xs_read(conn->xshandle, 0, prop, &len);
+ if (tmp != NULL) {
+ found = !strcmp(name, tmp);
+ free(tmp);
+ if (found)
+ break;
+ }
}
path = xs_get_domain_path(conn->xshandle, (unsigned int) id);
}
-do_found:
+ do_found:
if (found) {
- ret = (virDomainPtr) malloc(sizeof(virDomain));
- if (ret == NULL)
- goto done;
- memset(ret, 0, sizeof(virDomain));
- ret->magic = VIR_DOMAIN_MAGIC;
- ret->conn = conn;
- ret->handle = id;
- ret->path = path;
- if (uuid != NULL)
- memcpy(ret->uuid, uuid, 16);
- ret->name = strdup(name);
- }
-
-done:
+ ret = (virDomainPtr) malloc(sizeof(virDomain));
+ if (ret == NULL)
+ goto done;
+ memset(ret, 0, sizeof(virDomain));
+ ret->magic = VIR_DOMAIN_MAGIC;
+ ret->conn = conn;
+ ret->handle = id;
+ ret->path = path;
+ if (uuid != NULL)
+ memcpy(ret->uuid, uuid, 16);
+ ret->name = strdup(name);
+ }
+
+ done:
if (xenddomain != NULL)
- free(xenddomain);
+ free(xenddomain);
if (idlist != NULL)
free(idlist);
- return(ret);
+ return (ret);
}
/**
* Returns 0 in case of success and -1 in case of failure.
*/
int
-virDomainDestroy(virDomainPtr domain) {
+virDomainDestroy(virDomainPtr domain)
+{
int ret;
if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
- return(-1);
+ return (-1);
}
/*
ret = xend_destroy(domain->conn, domain->name);
if (ret == 0) {
virDomainFree(domain);
- return(0);
+ return (0);
}
ret = xenHypervisorDestroyDomain(domain->conn->handle, domain->handle);
if (ret < 0)
- return(-1);
-
+ return (-1);
+
virDomainFree(domain);
- return(0);
+ return (0);
}
/**
* Returns 0 in case of success and -1 in case of failure.
*/
int
-virDomainFree(virDomainPtr domain) {
+virDomainFree(virDomainPtr domain)
+{
if (!VIR_IS_DOMAIN(domain)) {
virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
- return(-1);
+ return (-1);
}
domain->magic = -1;
domain->handle = -1;
if (domain->name)
free(domain->name);
free(domain);
- return(0);
+ return (0);
}
/**
* Returns 0 in case of success and -1 in case of failure.
*/
int
-virDomainSuspend(virDomainPtr domain) {
+virDomainSuspend(virDomainPtr domain)
+{
int ret;
if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
- return(-1);
+ return (-1);
}
/* first try though the Xen daemon */
ret = xend_pause(domain->conn, domain->name);
if (ret == 0)
- return(0);
+ return (0);
/* then try a direct hypervisor access */
- return(xenHypervisorPauseDomain(domain->conn->handle, domain->handle));
+ return (xenHypervisorPauseDomain
+ (domain->conn->handle, domain->handle));
}
/**
* Returns 0 in case of success and -1 in case of failure.
*/
int
-virDomainResume(virDomainPtr domain) {
+virDomainResume(virDomainPtr domain)
+{
int ret;
if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
- return(-1);
+ return (-1);
}
/* first try though the Xen daemon */
ret = xend_unpause(domain->conn, domain->name);
if (ret == 0)
- return(0);
+ return (0);
/* then try a direct hypervisor access */
- return(xenHypervisorResumeDomain(domain->conn->handle, domain->handle));
+ return (xenHypervisorResumeDomain
+ (domain->conn->handle, domain->handle));
}
/**
* Returns 0 in case of success and -1 in case of failure.
*/
int
-virDomainSave(virDomainPtr domain, const char *to) {
+virDomainSave(virDomainPtr domain, const char *to)
+{
int ret;
char filepath[4096];
if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
- return(-1);
+ return (-1);
}
if (to == NULL) {
virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
- return(-1);
+ return (-1);
}
/*
* TODO: check for URI when libxml2 is linked in.
*/
if (to[0] != '/') {
- unsigned int len, t;
+ unsigned int len, t;
- t = strlen(to);
- if (getcwd(filepath, sizeof(filepath) - (t + 3)) == NULL)
- return(-1);
- len = strlen(filepath);
- /* that should be covered by getcwd() semantic, but be 100% sure */
- if (len > sizeof(filepath) - (t + 3))
- return(-1);
- filepath[len] = '/';
- strcpy(&filepath[len + 1], to);
- to = &filepath[0];
+ t = strlen(to);
+ if (getcwd(filepath, sizeof(filepath) - (t + 3)) == NULL)
+ return (-1);
+ len = strlen(filepath);
+ /* that should be covered by getcwd() semantic, but be 100% sure */
+ if (len > sizeof(filepath) - (t + 3))
+ return (-1);
+ filepath[len] = '/';
+ strcpy(&filepath[len + 1], to);
+ to = &filepath[0];
}
ret = xend_save(domain->conn, domain->name, to);
- return(ret);
+ return (ret);
}
/**
* Returns 0 in case of success and -1 in case of failure.
*/
int
-virDomainRestore(virConnectPtr conn, const char *from) {
+virDomainRestore(virConnectPtr conn, const char *from)
+{
int ret;
char filepath[4096];
if (!VIR_IS_CONNECT(conn)) {
virLibConnError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
- return(-1);
+ return (-1);
}
if (from == NULL) {
virLibConnError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
- return(-1);
+ return (-1);
}
/*
* TODO: check for URI when libxml2 is linked in.
*/
if (from[0] != '/') {
- unsigned int len, t;
-
- t = strlen(from);
- if (getcwd(filepath, sizeof(filepath) - (t + 3)) == NULL)
- return(-1);
- len = strlen(filepath);
- /* that should be covered by getcwd() semantic, but be 100% sure */
- if (len > sizeof(filepath) - (t + 3))
- return(-1);
- filepath[len] = '/';
- strcpy(&filepath[len + 1], from);
- from = &filepath[0];
- }
-
+ unsigned int len, t;
+
+ t = strlen(from);
+ if (getcwd(filepath, sizeof(filepath) - (t + 3)) == NULL)
+ return (-1);
+ len = strlen(filepath);
+ /* that should be covered by getcwd() semantic, but be 100% sure */
+ if (len > sizeof(filepath) - (t + 3))
+ return (-1);
+ filepath[len] = '/';
+ strcpy(&filepath[len + 1], from);
+ from = &filepath[0];
+ }
+
ret = xend_restore(conn, from);
- return(ret);
+ return (ret);
}
/**
* Returns 0 in case of success and -1 in case of failure.
*/
int
-virDomainShutdown(virDomainPtr domain) {
+virDomainShutdown(virDomainPtr domain)
+{
int ret;
if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
- return(-1);
+ return (-1);
}
-
+
/*
* try first with the xend daemon
*/
ret = xend_shutdown(domain->conn, domain->name);
if (ret == 0)
- return(0);
+ return (0);
/*
* this is very hackish, the domU kernel probes for a special
if (ret == 0) {
domain->flags |= DOMAIN_IS_SHUTDOWN;
}
- return(ret);
+ return (ret);
}
/**
* its lifetime will be the same as the domain object.
*/
const char *
-virDomainGetName(virDomainPtr domain) {
+virDomainGetName(virDomainPtr domain)
+{
if (!VIR_IS_DOMAIN(domain)) {
virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
- return(NULL);
+ return (NULL);
}
- return(domain->name);
+ return (domain->name);
}
/**
* Returns -1 in case of error, 0 in case of success
*/
int
-virDomainGetUUID(virDomainPtr domain, unsigned char *uuid) {
+virDomainGetUUID(virDomainPtr domain, unsigned char *uuid)
+{
if (!VIR_IS_DOMAIN(domain)) {
virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
- return(-1);
+ return (-1);
}
if (uuid == NULL) {
virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
- return(-1);
+ return (-1);
}
if (domain->handle == 0) {
memset(uuid, 0, 16);
} else {
- if ((domain->uuid[0] == 0) && (domain->uuid[1] == 0) &&
- (domain->uuid[2] == 0) && (domain->uuid[3] == 0) &&
- (domain->uuid[4] == 0) && (domain->uuid[5] == 0) &&
- (domain->uuid[6] == 0) && (domain->uuid[7] == 0) &&
- (domain->uuid[8] == 0) && (domain->uuid[9] == 0) &&
- (domain->uuid[10] == 0) && (domain->uuid[11] == 0) &&
- (domain->uuid[12] == 0) && (domain->uuid[13] == 0) &&
- (domain->uuid[14] == 0) && (domain->uuid[15] == 0))
- xend_get_domain_ids(domain->conn, domain->name, &domain->uuid[0]);
- memcpy(uuid, &domain->uuid[0], 16);
- }
- return(0);
+ if ((domain->uuid[0] == 0) && (domain->uuid[1] == 0) &&
+ (domain->uuid[2] == 0) && (domain->uuid[3] == 0) &&
+ (domain->uuid[4] == 0) && (domain->uuid[5] == 0) &&
+ (domain->uuid[6] == 0) && (domain->uuid[7] == 0) &&
+ (domain->uuid[8] == 0) && (domain->uuid[9] == 0) &&
+ (domain->uuid[10] == 0) && (domain->uuid[11] == 0) &&
+ (domain->uuid[12] == 0) && (domain->uuid[13] == 0) &&
+ (domain->uuid[14] == 0) && (domain->uuid[15] == 0))
+ xend_get_domain_ids(domain->conn, domain->name,
+ &domain->uuid[0]);
+ memcpy(uuid, &domain->uuid[0], 16);
+ }
+ return (0);
}
/**
* Returns the domain ID number or (unsigned int) -1 in case of error
*/
unsigned int
-virDomainGetID(virDomainPtr domain) {
+virDomainGetID(virDomainPtr domain)
+{
if (!VIR_IS_DOMAIN(domain)) {
virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
- return((unsigned int) -1);
+ return ((unsigned int) -1);
}
- return(domain->handle);
+ return (domain->handle);
}
/**
* Returns the new string or NULL in case of error
*/
char *
-virDomainGetOSType(virDomainPtr domain) {
+virDomainGetOSType(virDomainPtr domain)
+{
char *vm, *str = NULL;
-
+
if (!VIR_IS_DOMAIN(domain)) {
virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
- return(NULL);
+ return (NULL);
}
-
+
vm = virDomainGetVM(domain);
if (vm) {
- str = virDomainGetVMInfo(domain, vm, "image/ostype");
- free(vm);
+ str = virDomainGetVMInfo(domain, vm, "image/ostype");
+ free(vm);
}
if (str == NULL)
str = strdup("linux");
- return(str);
+ return (str);
}
/**
* Returns the memory size in kilobytes or 0 in case of error.
*/
unsigned long
-virDomainGetMaxMemory(virDomainPtr domain) {
+virDomainGetMaxMemory(virDomainPtr domain)
+{
unsigned long ret = 0;
if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
- return(0);
+ return (0);
}
-
+
if (domain->conn->flags & VIR_CONNECT_RO) {
char *tmp;
- tmp = virDomainDoStoreQuery(domain, "memory/target");
- if (tmp != NULL) {
- ret = (unsigned long) atol(tmp);
- free(tmp);
- }
+ tmp = virDomainDoStoreQuery(domain, "memory/target");
+ if (tmp != NULL) {
+ ret = (unsigned long) atol(tmp);
+ free(tmp);
+ }
} else {
dom0_getdomaininfo_t dominfo;
- int tmp;
+ int tmp;
- dominfo.domain = domain->handle;
- tmp = xenHypervisorGetDomainInfo(domain->conn->handle, domain->handle,
- &dominfo);
- if (tmp >= 0)
- ret = dominfo.max_pages * 4;
+ dominfo.domain = domain->handle;
+ tmp =
+ xenHypervisorGetDomainInfo(domain->conn->handle,
+ domain->handle, &dominfo);
+ if (tmp >= 0)
+ ret = dominfo.max_pages * 4;
}
- return(ret);
+ return (ret);
}
/**
* Returns 0 in case of success and -1 in case of failure.
*/
int
-virDomainSetMaxMemory(virDomainPtr domain, unsigned long memory) {
+virDomainSetMaxMemory(virDomainPtr domain, unsigned long memory)
+{
int ret;
char s[256], v[30];
-
+
if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
- return(-1);
+ return (-1);
}
if (memory < 4096) {
virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
- return(-1);
+ return (-1);
}
if (domain->conn->flags & VIR_CONNECT_RO)
- return(-1);
- if (domain->conn->xshandle==NULL)
- return(-1);
+ return (-1);
+ if (domain->conn->xshandle == NULL)
+ return (-1);
ret = xenHypervisorSetMaxMemory(domain->conn->handle, domain->handle,
memory);
if (ret < 0)
- return(-1);
+ return (-1);
/*
* try to update at the Xenstore level too
if (!xs_write(domain->conn->xshandle, 0, &s[0], &v[0], strlen(v)))
ret = -1;
- return(ret);
+ return (ret);
}
/**
* Returns 0 in case of success and -1 in case of failure.
*/
int
-virDomainGetInfo(virDomainPtr domain, virDomainInfoPtr info) {
+virDomainGetInfo(virDomainPtr domain, virDomainInfoPtr info)
+{
int ret;
char *tmp, **tmp2;
unsigned int nb_vcpus;
if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
- return(-1);
+ return (-1);
}
if (info == NULL) {
virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
- return(-1);
+ return (-1);
}
-
+
memset(info, 0, sizeof(virDomainInfo));
-
+
/*
* if we have direct access though the hypervisor do a direct call
*/
if (domain->conn->handle >= 0) {
dom0_getdomaininfo_t dominfo;
- dominfo.domain = domain->handle;
- ret = xenHypervisorGetDomainInfo(domain->conn->handle, domain->handle,
- &dominfo);
+ dominfo.domain = domain->handle;
+ ret =
+ xenHypervisorGetDomainInfo(domain->conn->handle,
+ domain->handle, &dominfo);
if (ret < 0)
- goto xend_info;
-
- switch (dominfo.flags & 0xFF) {
- case DOMFLAGS_DYING:
- info->state = VIR_DOMAIN_SHUTDOWN;
- break;
- case DOMFLAGS_SHUTDOWN:
- info->state = VIR_DOMAIN_SHUTOFF;
- break;
- case DOMFLAGS_PAUSED:
- info->state = VIR_DOMAIN_PAUSED;
- break;
- case DOMFLAGS_BLOCKED:
- info->state = VIR_DOMAIN_BLOCKED;
- break;
- case DOMFLAGS_RUNNING:
- info->state = VIR_DOMAIN_RUNNING;
- break;
- default:
- info->state = VIR_DOMAIN_NONE;
- }
-
- /*
- * the API brings back the cpu time in nanoseconds,
- * convert to microseconds, same thing convert to
+ goto xend_info;
+
+ switch (dominfo.flags & 0xFF) {
+ case DOMFLAGS_DYING:
+ info->state = VIR_DOMAIN_SHUTDOWN;
+ break;
+ case DOMFLAGS_SHUTDOWN:
+ info->state = VIR_DOMAIN_SHUTOFF;
+ break;
+ case DOMFLAGS_PAUSED:
+ info->state = VIR_DOMAIN_PAUSED;
+ break;
+ case DOMFLAGS_BLOCKED:
+ info->state = VIR_DOMAIN_BLOCKED;
+ break;
+ case DOMFLAGS_RUNNING:
+ info->state = VIR_DOMAIN_RUNNING;
+ break;
+ default:
+ info->state = VIR_DOMAIN_NONE;
+ }
+
+ /*
+ * the API brings back the cpu time in nanoseconds,
+ * convert to microseconds, same thing convert to
* kilobytes from page counts
- */
- info->cpuTime = dominfo.cpu_time;
- info->memory = dominfo.tot_pages * 4;
- info->maxMem = dominfo.max_pages * 4;
- info->nrVirtCpu = dominfo.nr_online_vcpus;
- return(0);
+ */
+ info->cpuTime = dominfo.cpu_time;
+ info->memory = dominfo.tot_pages * 4;
+ info->maxMem = dominfo.max_pages * 4;
+ info->nrVirtCpu = dominfo.nr_online_vcpus;
+ return (0);
}
-xend_info:
+ xend_info:
/*
* try to extract the informations though access to the Xen Daemon
*/
if (xend_get_domain_info(domain, info) == 0)
- return(0);
+ return (0);
/*
* last fallback, try to get the inforamtions from the Xen store
tmp = virDomainDoStoreQuery(domain, "running");
if (tmp != NULL) {
- if (tmp[0] == '1')
- info->state = VIR_DOMAIN_RUNNING;
- free(tmp);
+ if (tmp[0] == '1')
+ info->state = VIR_DOMAIN_RUNNING;
+ free(tmp);
} else {
- info->state = VIR_DOMAIN_NONE;
+ info->state = VIR_DOMAIN_NONE;
}
tmp = virDomainDoStoreQuery(domain, "memory/target");
if (tmp != NULL) {
- info->memory = atol(tmp);
- info->maxMem = atol(tmp);
- free(tmp);
+ info->memory = atol(tmp);
+ info->maxMem = atol(tmp);
+ free(tmp);
} else {
- info->memory = 0;
- info->maxMem = 0;
+ info->memory = 0;
+ info->maxMem = 0;
}
#if 0
/* doesn't seems to work */
tmp = virDomainDoStoreQuery(domain, "cpu_time");
if (tmp != NULL) {
- info->cpuTime = atol(tmp);
- free(tmp);
+ info->cpuTime = atol(tmp);
+ free(tmp);
} else {
- info->cpuTime = 0;
+ info->cpuTime = 0;
}
#endif
snprintf(request, 199, "/local/domain/%d/cpu", domain->handle);
request[199] = 0;
tmp2 = virConnectDoStoreList(domain->conn, request, &nb_vcpus);
if (tmp2 != NULL) {
- info->nrVirtCpu = nb_vcpus;
- free(tmp2);
+ info->nrVirtCpu = nb_vcpus;
+ free(tmp2);
}
- return(0);
+ return (0);
}
/**
* the caller must free() the returned value.
*/
char *
-virDomainGetXMLDesc(virDomainPtr domain, int flags) {
+virDomainGetXMLDesc(virDomainPtr domain, int flags)
+{
if (!VIR_IS_DOMAIN(domain)) {
virLibDomainError(domain, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
- return(NULL);
+ return (NULL);
}
if (flags != 0) {
virLibDomainError(domain, VIR_ERR_INVALID_ARG, __FUNCTION__);
- return(NULL);
+ return (NULL);
}
- return(xend_get_domain_xml(domain));
+ return (xend_get_domain_xml(domain));
}
* Handle an error in the S-Expression code
*/
static void
-virSexprError(virErrorNumber error, const char *info) {
+virSexprError(virErrorNumber error, const char *info)
+{
const char *errmsg;
-
+
if (error == VIR_ERR_OK)
return;
ret = (struct sexpr *) malloc(sizeof(*ret));
if (ret == NULL) {
virSexprError(VIR_ERR_NO_MEMORY, "failed to allocate a node");
- return(NULL);
+ return (NULL);
}
ret->kind = SEXPR_NIL;
return ret;
sexpr_append(struct sexpr *lst, struct sexpr *value)
{
if (lst == NULL)
- return(NULL);
+ return (NULL);
if (value == NULL)
- return(lst);
+ return (lst);
append(lst, value);
- return(lst);
+ return (lst);
}
/**
size_t ret = 0, tmp;
if ((sexpr == NULL) || (buffer == NULL) || (n_buffer <= 0))
- return(0);
+ return (0);
switch (sexpr->kind) {
case SEXPR_CONS:
tmp = snprintf(buffer + ret, n_buffer - ret, "(");
- if (tmp == 0)
- goto error;
- ret += tmp;
- tmp = sexpr2string(sexpr->car, buffer + ret, n_buffer - ret);
- if (tmp == 0)
- goto error;
- ret += tmp;
+ if (tmp == 0)
+ goto error;
+ ret += tmp;
+ tmp = sexpr2string(sexpr->car, buffer + ret, n_buffer - ret);
+ if (tmp == 0)
+ goto error;
+ ret += tmp;
while (sexpr->cdr->kind != SEXPR_NIL) {
sexpr = sexpr->cdr;
tmp = snprintf(buffer + ret, n_buffer - ret, " ");
- if (tmp == 0)
- goto error;
- ret += tmp;
- tmp = sexpr2string(sexpr->car, buffer + ret, n_buffer - ret);
- if (tmp == 0)
- goto error;
- ret += tmp;
+ if (tmp == 0)
+ goto error;
+ ret += tmp;
+ tmp =
+ sexpr2string(sexpr->car, buffer + ret, n_buffer - ret);
+ if (tmp == 0)
+ goto error;
+ ret += tmp;
}
tmp = snprintf(buffer + ret, n_buffer - ret, ")");
- if (tmp == 0)
- goto error;
- ret += tmp;
+ if (tmp == 0)
+ goto error;
+ ret += tmp;
break;
case SEXPR_VALUE:
if (strchr(sexpr->value, ' '))
else
tmp = snprintf(buffer + ret, n_buffer - ret, "%s",
sexpr->value);
- if (tmp == 0)
- goto error;
- ret += tmp;
+ if (tmp == 0)
+ goto error;
+ ret += tmp;
break;
case SEXPR_NIL:
break;
- default:
- goto error;
+ default:
+ goto error;
}
- return(ret);
-error:
+ return (ret);
+ error:
buffer[n_buffer - 1] = 0;
virSexprError(VIR_ERR_SEXPR_SERIAL, buffer);
- return(0);
+ return (0);
}
#define IS_SPACE(c) ((c == 0x20) || (c == 0x9) || (c == 0xD) || (c == 0xA))
{
while (IS_SPACE(*string))
string++;
- return(string);
+ return (string);
}
/**
}
ret->value = strndup(start, ptr - start);
- if (ret->value == NULL) {
- virSexprError(VIR_ERR_NO_MEMORY, "failed to copy a string");
- }
+ if (ret->value == NULL) {
+ virSexprError(VIR_ERR_NO_MEMORY,
+ "failed to copy a string");
+ }
if (*ptr == '\'')
ptr++;
}
ret->value = strndup(start, ptr - start);
- if (ret->value == NULL) {
- virSexprError(VIR_ERR_NO_MEMORY, "failed to copy a string");
- }
+ if (ret->value == NULL) {
+ virSexprError(VIR_ERR_NO_MEMORY,
+ "failed to copy a string");
+ }
}
ret->kind = SEXPR_VALUE;
- if (ret->value == NULL)
- goto error;
+ if (ret->value == NULL)
+ goto error;
}
*end = ptr - buffer;
return ret;
-error:
+ error:
sexpr_free(ret);
- return(NULL);
+ return (NULL);
}
/**
char buffer[4096], *ptr, *token;
if ((node == NULL) || (sexpr == NULL))
- return(NULL);
+ return (NULL);
snprintf(buffer, sizeof(buffer), "%s", node);
continue;
sexpr = sexpr->cdr;
- for (i=sexpr; i->kind != SEXPR_NIL; i=i->cdr) {
+ for (i = sexpr; i->kind != SEXPR_NIL; i = i->cdr) {
if (i->kind != SEXPR_CONS ||
i->car->kind != SEXPR_CONS ||
i->car->car->kind != SEXPR_VALUE) {
};
/* conversion to/from strings */
-size_t sexpr2string (struct sexpr *sexpr,
- char *buffer,
- size_t n_buffer);
-struct sexpr * string2sexpr (const char *buffer);
+size_t sexpr2string(struct sexpr *sexpr, char *buffer, size_t n_buffer);
+struct sexpr *string2sexpr(const char *buffer);
/* constructors and destructors */
-struct sexpr * sexpr_nil (void);
-struct sexpr * sexpr_string (const char *str,
- ssize_t len);
-struct sexpr * sexpr_cons (struct sexpr *car,
- struct sexpr *cdr);
-struct sexpr * sexpr_append (struct sexpr *lst,
- struct sexpr *item);
-void sexpr_free (struct sexpr *sexpr);
+struct sexpr *sexpr_nil(void);
+struct sexpr *sexpr_string(const char *str, ssize_t len);
+struct sexpr *sexpr_cons(struct sexpr *car, struct sexpr *cdr);
+struct sexpr *sexpr_append(struct sexpr *lst, struct sexpr *item);
+void sexpr_free(struct sexpr *sexpr);
/* lookup in S-Expressions */
-const char * sexpr_node (struct sexpr *sexpr,
- const char *node);
-struct sexpr * sexpr_lookup (struct sexpr *sexpr,
- const char *node);
+const char *sexpr_node(struct sexpr *sexpr, const char *node);
+struct sexpr *sexpr_lookup(struct sexpr *sexpr, const char *node);
#endif
* $Id$
*/
-#define _GNU_SOURCE /* isblank() */
+#define _GNU_SOURCE /* isblank() */
#include "libvirt.h"
#include "virterror.h"
((int) ((T)->tv_usec - (U)->tv_usec))) / 1000.0)
typedef enum {
- VSH_MESG, /* standard output */
- VSH_HEADER, /* header for standard output */
- VSH_FOOTER, /* timing, last command state, or whatever */
- VSH_DEBUG1, /* debugN where 'N' = level */
+ VSH_MESG, /* standard output */
+ VSH_HEADER, /* header for standard output */
+ VSH_FOOTER, /* timing, last command state, or whatever */
+ VSH_DEBUG1, /* debugN where 'N' = level */
VSH_DEBUG2,
VSH_DEBUG3,
VSH_DEBUG4,
* The error handler for virtsh
*/
static void
-virshErrorHandler(void *unused, virErrorPtr error) {
+virshErrorHandler(void *unused, virErrorPtr error)
+{
if ((unused != NULL) || (error == NULL))
return;
/*
* vshCmdOptType - command option type
- */
+ */
typedef enum {
- VSH_OT_NONE = 0, /* none */
- VSH_OT_BOOL, /* boolean option */
- VSH_OT_STRING, /* string option */
- VSH_OT_INT, /* int option */
- VSH_OT_DATA /* string data (as non-option) */
+ VSH_OT_NONE = 0, /* none */
+ VSH_OT_BOOL, /* boolean option */
+ VSH_OT_STRING, /* string option */
+ VSH_OT_INT, /* int option */
+ VSH_OT_DATA /* string data (as non-option) */
} vshCmdOptType;
/*
* Command Option Flags
*/
-#define VSH_OFLAG_NONE 0 /* without flags */
-#define VSH_OFLAG_REQ (1 << 1) /* option required */
+#define VSH_OFLAG_NONE 0 /* without flags */
+#define VSH_OFLAG_REQ (1 << 1) /* option required */
/* dummy */
typedef struct __vshControl vshControl;
/*
* vshCmdInfo -- information about command
*/
-typedef struct {
- const char *name; /* name of information */
- const char *data; /* information */
+typedef struct {
+ const char *name; /* name of information */
+ const char *data; /* information */
} vshCmdInfo;
/*
* vshCmdOptDef - command option definition
*/
-typedef struct {
- const char *name; /* the name of option */
- vshCmdOptType type; /* option type */
- int flag; /* flags */
- const char *help; /* help string */
+typedef struct {
+ const char *name; /* the name of option */
+ vshCmdOptType type; /* option type */
+ int flag; /* flags */
+ const char *help; /* help string */
} vshCmdOptDef;
/*
* vshCmdOpt - command options
*/
typedef struct vshCmdOpt {
- vshCmdOptDef *def; /* pointer to relevant option */
- char *data; /* allocated data */
- struct vshCmdOpt *next;
+ vshCmdOptDef *def; /* pointer to relevant option */
+ char *data; /* allocated data */
+ struct vshCmdOpt *next;
} vshCmdOpt;
/*
* vshCmdDef - command definition
*/
-typedef struct {
- const char *name;
- int (*handler)(vshControl *, vshCmd *); /* command handler */
- vshCmdOptDef *opts; /* definition of command options */
- vshCmdInfo *info; /* details about command */
+typedef struct {
+ const char *name;
+ int (*handler) (vshControl *, vshCmd *); /* command handler */
+ vshCmdOptDef *opts; /* definition of command options */
+ vshCmdInfo *info; /* details about command */
} vshCmdDef;
/*
* vshCmd - parsed command
*/
typedef struct __vshCmd {
- vshCmdDef *def; /* command definition */
- vshCmdOpt *opts; /* list of command arguments */
- struct __vshCmd *next; /* next command */
+ vshCmdDef *def; /* command definition */
+ vshCmdOpt *opts; /* list of command arguments */
+ struct __vshCmd *next; /* next command */
} __vshCmd;
/*
* vshControl
*/
typedef struct __vshControl {
- virConnectPtr conn; /* connection to hypervisor */
- vshCmd *cmd; /* the current command */
- char *cmdstr; /* string with command */
- uid_t uid; /* process owner */
- int imode; /* interactive mode? */
- int quiet; /* quiet mode */
- int debug; /* print debug messages? */
- int timing; /* print timing info? */
+ virConnectPtr conn; /* connection to hypervisor */
+ vshCmd *cmd; /* the current command */
+ char *cmdstr; /* string with command */
+ uid_t uid; /* process owner */
+ int imode; /* interactive mode? */
+ int quiet; /* quiet mode */
+ int debug; /* print debug messages? */
+ int timing; /* print timing info? */
} __vshControl;
static vshCmdDef commands[];
-static void vshError(vshControl *ctl, int doexit, const char *format, ...);
-static int vshInit(vshControl *ctl);
-static int vshDeinit(vshControl *ctl);
-static void vshUsage(vshControl *ctl, const char *cmdname);
+static void vshError(vshControl * ctl, int doexit, const char *format,
+ ...);
+static int vshInit(vshControl * ctl);
+static int vshDeinit(vshControl * ctl);
+static void vshUsage(vshControl * ctl, const char *cmdname);
-static int vshParseArgv(vshControl *ctl, int argc, char **argv);
+static int vshParseArgv(vshControl * ctl, int argc, char **argv);
-static const char *vshCmddefGetInfo(vshCmdDef *cmd, const char *info);
+static const char *vshCmddefGetInfo(vshCmdDef * cmd, const char *info);
static vshCmdDef *vshCmddefSearch(const char *cmdname);
-static int vshCmddefHelp(vshControl *ctl, const char *name, int withprog);
+static int vshCmddefHelp(vshControl * ctl, const char *name, int withprog);
-static vshCmdOpt *vshCommandOpt(vshCmd *cmd, const char *name);
-static int vshCommandOptInt(vshCmd *cmd, const char *name, int *found);
-static char *vshCommandOptString(vshCmd *cmd, const char *name, int *found);
-static int vshCommandOptBool(vshCmd *cmd, const char *name);
-static virDomainPtr vshCommandOptDomain(vshControl *ctl, vshCmd *cmd, const char *optname, char **name);
+static vshCmdOpt *vshCommandOpt(vshCmd * cmd, const char *name);
+static int vshCommandOptInt(vshCmd * cmd, const char *name, int *found);
+static char *vshCommandOptString(vshCmd * cmd, const char *name,
+ int *found);
+static int vshCommandOptBool(vshCmd * cmd, const char *name);
+static virDomainPtr vshCommandOptDomain(vshControl * ctl, vshCmd * cmd,
+ const char *optname, char **name);
-static void vshPrint(vshControl *ctl, vshOutType out, const char *format, ...);
+static void vshPrint(vshControl * ctl, vshOutType out, const char *format,
+ ...);
static const char *vshDomainStateToString(int state);
-static int vshConnectionUsability(vshControl *ctl, virConnectPtr conn, int showerror);
+static int vshConnectionUsability(vshControl * ctl, virConnectPtr conn,
+ int showerror);
/* ---------------
* Commands
* "help" command
*/
static vshCmdInfo info_help[] = {
- { "syntax", "help [<command>]" },
- { "help", "print help" },
- { "desc", "Prints global help or command specific help." },
- { "version", "Prints versionning informations." },
- { NULL, NULL }
+ {"syntax", "help [<command>]"},
+ {"help", "print help"},
+ {"desc", "Prints global help or command specific help."},
+ {"version", "Prints versionning informations."},
+ {NULL, NULL}
};
static vshCmdOptDef opts_help[] = {
- { "command", VSH_OT_DATA, 0, "name of command" },
- { NULL, 0, 0, NULL }
+ {"command", VSH_OT_DATA, 0, "name of command"},
+ {NULL, 0, 0, NULL}
};
static int
-cmdHelp(vshControl *ctl, vshCmd *cmd) {
+cmdHelp(vshControl * ctl, vshCmd * cmd)
+{
const char *cmdname = vshCommandOptString(cmd, "command", NULL);
if (!cmdname) {
vshCmdDef *def;
-
+
vshPrint(ctl, VSH_HEADER, "Commands:\n\n");
- for(def = commands; def->name; def++)
- vshPrint(ctl, VSH_MESG, " %-15s %s\n", def->name,
- vshCmddefGetInfo(def, "help"));
+ for (def = commands; def->name; def++)
+ vshPrint(ctl, VSH_MESG, " %-15s %s\n", def->name,
+ vshCmddefGetInfo(def, "help"));
return TRUE;
}
return vshCmddefHelp(ctl, cmdname, FALSE);
* "connect" command
*/
static vshCmdInfo info_connect[] = {
- { "syntax", "connect [--readonly]" },
- { "help", "(re)connect to hypervisor" },
- { "desc", "Connect to local hypervisor. This is build-in command after shell start up." },
- { NULL, NULL }
+ {"syntax", "connect [--readonly]"},
+ {"help", "(re)connect to hypervisor"},
+ {"desc",
+ "Connect to local hypervisor. This is build-in command after shell start up."},
+ {NULL, NULL}
};
static vshCmdOptDef opts_connect[] = {
- { "readonly", VSH_OT_BOOL, 0, "read-only connection" },
- { NULL, 0, 0, NULL }
+ {"readonly", VSH_OT_BOOL, 0, "read-only connection"},
+ {NULL, 0, 0, NULL}
};
static int
-cmdConnect(vshControl *ctl, vshCmd *cmd) {
+cmdConnect(vshControl * ctl, vshCmd * cmd)
+{
int ro = vshCommandOptBool(cmd, "readonly");
-
+
if (ctl->conn) {
- if (virConnectClose(ctl->conn)!=0) {
- vshError(ctl, FALSE, "failed to disconnect from the hypervisor");
+ if (virConnectClose(ctl->conn) != 0) {
+ vshError(ctl, FALSE,
+ "failed to disconnect from the hypervisor");
return FALSE;
}
ctl->conn = NULL;
if (!ctl->conn)
vshError(ctl, FALSE, "failed to connect to the hypervisor");
-
+
return ctl->conn ? TRUE : FALSE;
}
* "list" command
*/
static vshCmdInfo info_list[] = {
- { "syntax", "list" },
- { "help", "list domains" },
- { "desc", "Returns list of domains." },
- { NULL, NULL }
+ {"syntax", "list"},
+ {"help", "list domains"},
+ {"desc", "Returns list of domains."},
+ {NULL, NULL}
};
static int
-cmdList(vshControl *ctl, vshCmd *cmd ATTRIBUTE_UNUSED) {
+cmdList(vshControl * ctl, vshCmd * cmd ATTRIBUTE_UNUSED)
+{
int *ids, maxid, i;
if (!vshConnectionUsability(ctl, ctl->conn, TRUE))
return FALSE;
-
+
maxid = virConnectNumOfDomains(ctl->conn);
if (maxid <= 0) {
/* strange, there should be at least dom0... */
}
ids = malloc(sizeof(int) * maxid);
virConnectListDomains(ctl->conn, &ids[0], maxid);
-
+
vshPrint(ctl, VSH_HEADER, "%3s %-20s %s\n", "Id", "Name", "State");
vshPrint(ctl, VSH_HEADER, "----------------------------------\n");
-
- for(i=0; i < maxid; i++) {
+
+ for (i = 0; i < maxid; i++) {
int ret;
virDomainInfo info;
virDomainPtr dom = virDomainLookupByID(ctl->conn, ids[i]);
-
- /* this kind of work with domains is not atomic operation */
+
+ /* this kind of work with domains is not atomic operation */
if (!dom)
continue;
ret = virDomainGetInfo(dom, &info);
-
- vshPrint(ctl, VSH_MESG, "%3d %-20s %s\n",
- virDomainGetID(dom),
- virDomainGetName(dom),
- ret < 0 ? "no state" : vshDomainStateToString(info.state));
+
+ vshPrint(ctl, VSH_MESG, "%3d %-20s %s\n",
+ virDomainGetID(dom),
+ virDomainGetName(dom),
+ ret <
+ 0 ? "no state" : vshDomainStateToString(info.state));
virDomainFree(dom);
}
free(ids);
* "dstate" command
*/
static vshCmdInfo info_dstate[] = {
- { "syntax", "dstate <domain>" },
- { "help", "domain state" },
- { "desc", "Returns state about a running domain." },
- { NULL, NULL }
+ {"syntax", "dstate <domain>"},
+ {"help", "domain state"},
+ {"desc", "Returns state about a running domain."},
+ {NULL, NULL}
};
static vshCmdOptDef opts_dstate[] = {
- { "domain", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name or id" },
- { NULL, 0, 0, NULL }
+ {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name or id"},
+ {NULL, 0, 0, NULL}
};
static int
-cmdDstate(vshControl *ctl, vshCmd *cmd) {
+cmdDstate(vshControl * ctl, vshCmd * cmd)
+{
virDomainInfo info;
virDomainPtr dom;
int ret = TRUE;
-
+
if (!vshConnectionUsability(ctl, ctl->conn, TRUE))
return FALSE;
if (!(dom = vshCommandOptDomain(ctl, cmd, "domain", NULL)))
return FALSE;
-
- if (virDomainGetInfo(dom, &info)==0)
- vshPrint(ctl, VSH_MESG, "%s\n", vshDomainStateToString(info.state));
+
+ if (virDomainGetInfo(dom, &info) == 0)
+ vshPrint(ctl, VSH_MESG, "%s\n",
+ vshDomainStateToString(info.state));
else
ret = FALSE;
-
+
virDomainFree(dom);
return ret;
}
* "suspend" command
*/
static vshCmdInfo info_suspend[] = {
- { "syntax", "suspend <domain>" },
- { "help", "suspend a domain" },
- { "desc", "Suspend a running domain." },
- { NULL, NULL }
+ {"syntax", "suspend <domain>"},
+ {"help", "suspend a domain"},
+ {"desc", "Suspend a running domain."},
+ {NULL, NULL}
};
static vshCmdOptDef opts_suspend[] = {
- { "domain", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name or id" },
- { NULL, 0, 0, NULL }
+ {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name or id"},
+ {NULL, 0, 0, NULL}
};
static int
-cmdSuspend(vshControl *ctl, vshCmd *cmd) {
+cmdSuspend(vshControl * ctl, vshCmd * cmd)
+{
virDomainPtr dom;
char *name;
int ret = TRUE;
-
+
if (!vshConnectionUsability(ctl, ctl->conn, TRUE))
return FALSE;
if (!(dom = vshCommandOptDomain(ctl, cmd, "domain", &name)))
return FALSE;
-
- if (virDomainSuspend(dom)==0) {
+
+ if (virDomainSuspend(dom) == 0) {
vshPrint(ctl, VSH_MESG, "Domain %s suspended\n", name);
} else {
vshError(ctl, FALSE, "Failed to suspend domain\n");
ret = FALSE;
}
-
+
virDomainFree(dom);
return ret;
}
* "save" command
*/
static vshCmdInfo info_save[] = {
- { "syntax", "save <domain> <file>" },
- { "help", "save a domain state to a file" },
- { "desc", "Save a running domain." },
- { NULL, NULL }
+ {"syntax", "save <domain> <file>"},
+ {"help", "save a domain state to a file"},
+ {"desc", "Save a running domain."},
+ {NULL, NULL}
};
static vshCmdOptDef opts_save[] = {
- { "domain", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name or id" },
- { "file", VSH_OT_DATA, VSH_OFLAG_REQ, "where to save the data" },
- { NULL, 0, 0, NULL }
+ {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name or id"},
+ {"file", VSH_OT_DATA, VSH_OFLAG_REQ, "where to save the data"},
+ {NULL, 0, 0, NULL}
};
static int
-cmdSave(vshControl *ctl, vshCmd *cmd) {
+cmdSave(vshControl * ctl, vshCmd * cmd)
+{
virDomainPtr dom;
char *name;
char *to;
int ret = TRUE;
-
+
if (!vshConnectionUsability(ctl, ctl->conn, TRUE))
return FALSE;
if (!(to = vshCommandOptString(cmd, "file", NULL)))
return FALSE;
-
+
if (!(dom = vshCommandOptDomain(ctl, cmd, "domain", &name)))
return FALSE;
-
- if (virDomainSave(dom, to)==0) {
+
+ if (virDomainSave(dom, to) == 0) {
vshPrint(ctl, VSH_MESG, "Domain %s saved\n", name);
} else {
vshError(ctl, FALSE, "Failed to save domain\n");
ret = FALSE;
}
-
+
virDomainFree(dom);
return ret;
}
* "restore" command
*/
static vshCmdInfo info_restore[] = {
- { "syntax", "restore a domain from <file>" },
- { "help", "restore a domain from a saved state in a file" },
- { "desc", "Restore a domain." },
- { NULL, NULL }
+ {"syntax", "restore a domain from <file>"},
+ {"help", "restore a domain from a saved state in a file"},
+ {"desc", "Restore a domain."},
+ {NULL, NULL}
};
static vshCmdOptDef opts_restore[] = {
- { "file", VSH_OT_DATA, VSH_OFLAG_REQ, "the state to restore" },
- { NULL, 0, 0, NULL }
+ {"file", VSH_OT_DATA, VSH_OFLAG_REQ, "the state to restore"},
+ {NULL, 0, 0, NULL}
};
static int
-cmdRestore(vshControl *ctl, vshCmd *cmd) {
+cmdRestore(vshControl * ctl, vshCmd * cmd)
+{
char *from;
int found;
int ret = TRUE;
-
+
if (!vshConnectionUsability(ctl, ctl->conn, TRUE))
return FALSE;
from = vshCommandOptString(cmd, "file", &found);
if (!found)
return FALSE;
-
- if (virDomainRestore(ctl->conn, from)==0) {
+
+ if (virDomainRestore(ctl->conn, from) == 0) {
vshPrint(ctl, VSH_MESG, "Domain restored from %s\n", from);
} else {
vshError(ctl, FALSE, "Failed to restore domain\n");
* "resume" command
*/
static vshCmdInfo info_resume[] = {
- { "syntax", "resume <domain>" },
- { "help", "resume a domain" },
- { "desc", "Resume a previously suspended domain." },
- { NULL, NULL }
+ {"syntax", "resume <domain>"},
+ {"help", "resume a domain"},
+ {"desc", "Resume a previously suspended domain."},
+ {NULL, NULL}
};
static vshCmdOptDef opts_resume[] = {
- { "domain", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name or id" },
- { NULL, 0, 0, NULL }
+ {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name or id"},
+ {NULL, 0, 0, NULL}
};
static int
-cmdResume(vshControl *ctl, vshCmd *cmd) {
+cmdResume(vshControl * ctl, vshCmd * cmd)
+{
virDomainPtr dom;
int ret = TRUE;
char *name;
-
+
if (!vshConnectionUsability(ctl, ctl->conn, TRUE))
return FALSE;
if (!(dom = vshCommandOptDomain(ctl, cmd, "domain", &name)))
return FALSE;
-
- if (virDomainResume(dom)==0) {
+
+ if (virDomainResume(dom) == 0) {
vshPrint(ctl, VSH_MESG, "Domain %s resumed\n", name);
} else {
vshError(ctl, FALSE, "Failed to resume domain\n");
ret = FALSE;
}
-
+
virDomainFree(dom);
return ret;
}
* "shutdown" command
*/
static vshCmdInfo info_shutdown[] = {
- { "syntax", "shutdown <domain>" },
- { "help", "gracefully shutdown a domain" },
- { "desc", "Run shutdown in the targetted domain" },
- { NULL, NULL }
+ {"syntax", "shutdown <domain>"},
+ {"help", "gracefully shutdown a domain"},
+ {"desc", "Run shutdown in the targetted domain"},
+ {NULL, NULL}
};
static vshCmdOptDef opts_shutdown[] = {
- { "domain", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name or id" },
- { NULL, 0, 0, NULL }
+ {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name or id"},
+ {NULL, 0, 0, NULL}
};
static int
-cmdShutdown(vshControl *ctl, vshCmd *cmd) {
+cmdShutdown(vshControl * ctl, vshCmd * cmd)
+{
virDomainPtr dom;
int ret = TRUE;
char *name;
-
+
if (!vshConnectionUsability(ctl, ctl->conn, TRUE))
return FALSE;
if (!(dom = vshCommandOptDomain(ctl, cmd, "domain", &name)))
return FALSE;
-
- if (virDomainShutdown(dom)==0) {
+
+ if (virDomainShutdown(dom) == 0) {
vshPrint(ctl, VSH_MESG, "Domain %s is being shutdown\n", name);
} else {
vshError(ctl, FALSE, "Failed to shutdown domain\n");
ret = FALSE;
}
-
+
virDomainFree(dom);
return ret;
}
* "destroy" command
*/
static vshCmdInfo info_destroy[] = {
- { "syntax", "destroy <domain>" },
- { "help", "destroy a domain" },
- { "desc", "Destroy a given domain." },
- { NULL, NULL }
+ {"syntax", "destroy <domain>"},
+ {"help", "destroy a domain"},
+ {"desc", "Destroy a given domain."},
+ {NULL, NULL}
};
static vshCmdOptDef opts_destroy[] = {
- { "domain", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name or id" },
- { NULL, 0, 0, NULL }
+ {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name or id"},
+ {NULL, 0, 0, NULL}
};
static int
-cmdDestroy(vshControl *ctl, vshCmd *cmd) {
+cmdDestroy(vshControl * ctl, vshCmd * cmd)
+{
virDomainPtr dom;
int ret = TRUE;
char *name;
-
+
if (!vshConnectionUsability(ctl, ctl->conn, TRUE))
return FALSE;
if (!(dom = vshCommandOptDomain(ctl, cmd, "domain", &name)))
return FALSE;
-
- if (virDomainDestroy(dom)==0) {
+
+ if (virDomainDestroy(dom) == 0) {
vshPrint(ctl, VSH_MESG, "Domain %s destroyed\n", name);
} else {
vshError(ctl, FALSE, "Failed to destroy domain\n");
ret = FALSE;
virDomainFree(dom);
}
-
+
return ret;
}
* "dinfo" command
*/
static vshCmdInfo info_dinfo[] = {
- { "syntax", "dinfo <domain>" },
- { "help", "domain information" },
- { "desc", "Returns basic information about the domain." },
- { NULL, NULL }
+ {"syntax", "dinfo <domain>"},
+ {"help", "domain information"},
+ {"desc", "Returns basic information about the domain."},
+ {NULL, NULL}
};
static vshCmdOptDef opts_dinfo[] = {
- { "domain", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name or id" },
- { NULL, 0, 0, NULL }
+ {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name or id"},
+ {NULL, 0, 0, NULL}
};
static int
-cmdDinfo(vshControl *ctl, vshCmd *cmd) {
+cmdDinfo(vshControl * ctl, vshCmd * cmd)
+{
virDomainInfo info;
virDomainPtr dom;
int ret = TRUE;
char *str;
-
+
if (!vshConnectionUsability(ctl, ctl->conn, TRUE))
return FALSE;
if (!(dom = vshCommandOptDomain(ctl, cmd, "domain", NULL)))
return FALSE;
-
- vshPrint(ctl, VSH_MESG, "%-15s %d\n", "Id:",
- virDomainGetID(dom));
- vshPrint(ctl, VSH_MESG, "%-15s %s\n", "Name:",
- virDomainGetName(dom));
-
- if ((str=virDomainGetOSType(dom))) {
- vshPrint(ctl, VSH_MESG, "%-15s %s\n", "OS Type:", str);
- free(str);
- }
-
- if (virDomainGetInfo(dom, &info)==0) {
- vshPrint(ctl, VSH_MESG, "%-15s %s\n", "State:",
- vshDomainStateToString(info.state));
-
- vshPrint(ctl, VSH_MESG, "%-15s %d\n", "CPU(s):",
- info.nrVirtCpu);
-
- if (info.cpuTime != 0)
- {
+
+ vshPrint(ctl, VSH_MESG, "%-15s %d\n", "Id:", virDomainGetID(dom));
+ vshPrint(ctl, VSH_MESG, "%-15s %s\n", "Name:", virDomainGetName(dom));
+
+ if ((str = virDomainGetOSType(dom))) {
+ vshPrint(ctl, VSH_MESG, "%-15s %s\n", "OS Type:", str);
+ free(str);
+ }
+
+ if (virDomainGetInfo(dom, &info) == 0) {
+ vshPrint(ctl, VSH_MESG, "%-15s %s\n", "State:",
+ vshDomainStateToString(info.state));
+
+ vshPrint(ctl, VSH_MESG, "%-15s %d\n", "CPU(s):", info.nrVirtCpu);
+
+ if (info.cpuTime != 0) {
float cpuUsed = info.cpuTime;
+
cpuUsed /= 1000000000;
-
+
vshPrint(ctl, VSH_MESG, "%-15s %.1fs\n", "CPU time:", cpuUsed);
}
-
+
vshPrint(ctl, VSH_MESG, "%-15s %lu kB\n", "Max memory:",
- info.maxMem);
+ info.maxMem);
vshPrint(ctl, VSH_MESG, "%-15s %lu kB\n", "Used memory:",
- info.memory);
-
+ info.memory);
+
} else {
ret = FALSE;
}
-
+
virDomainFree(dom);
return ret;
}
* "dumpxml" command
*/
static vshCmdInfo info_dumpxml[] = {
- { "syntax", "dumpxml <name>" },
- { "help", "domain information in XML" },
- { "desc", "Ouput the domain informations as an XML dump to stdout" },
- { NULL, NULL }
+ {"syntax", "dumpxml <name>"},
+ {"help", "domain information in XML"},
+ {"desc", "Ouput the domain informations as an XML dump to stdout"},
+ {NULL, NULL}
};
static vshCmdOptDef opts_dumpxml[] = {
- { "domain", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name or id" },
- { NULL, 0, 0, NULL }
+ {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name or id"},
+ {NULL, 0, 0, NULL}
};
static int
-cmdDumpXML(vshControl *ctl, vshCmd *cmd) {
+cmdDumpXML(vshControl * ctl, vshCmd * cmd)
+{
virDomainPtr dom;
int ret = TRUE;
char *dump;
-
+
if (!vshConnectionUsability(ctl, ctl->conn, TRUE))
return FALSE;
if (!(dom = vshCommandOptDomain(ctl, cmd, "domain", NULL)))
return FALSE;
-
+
dump = virDomainGetXMLDesc(dom, 0);
if (dump != NULL) {
printf("%s", dump);
} else {
ret = FALSE;
}
-
+
virDomainFree(dom);
return ret;
}
* "nameof" command
*/
static vshCmdInfo info_nameof[] = {
- { "syntax", "nameof <id>" },
- { "help", "convert a domain Id to domain name" },
- { NULL, NULL }
+ {"syntax", "nameof <id>"},
+ {"help", "convert a domain Id to domain name"},
+ {NULL, NULL}
};
static vshCmdOptDef opts_nameof[] = {
- { "id", VSH_OT_DATA, VSH_OFLAG_REQ, "domain Id" },
- { NULL, 0, 0, NULL }
+ {"id", VSH_OT_DATA, VSH_OFLAG_REQ, "domain Id"},
+ {NULL, 0, 0, NULL}
};
static int
-cmdNameof(vshControl *ctl, vshCmd *cmd) {
+cmdNameof(vshControl * ctl, vshCmd * cmd)
+{
int found;
int id = vshCommandOptInt(cmd, "id", &found);
virDomainPtr dom;
return FALSE;
if (!found)
return FALSE;
-
+
dom = virDomainLookupByID(ctl->conn, id);
if (dom) {
vshPrint(ctl, VSH_MESG, "%s\n", virDomainGetName(dom));
* "idof" command
*/
static vshCmdInfo info_idof[] = {
- { "syntax", "idof <name>" },
- { "help", "convert a domain name to domain Id" },
- { NULL, NULL }
+ {"syntax", "idof <name>"},
+ {"help", "convert a domain name to domain Id"},
+ {NULL, NULL}
};
static vshCmdOptDef opts_idof[] = {
- { "name", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name" },
- { NULL, 0, 0, NULL }
+ {"name", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name"},
+ {NULL, 0, 0, NULL}
};
static int
-cmdIdof(vshControl *ctl, vshCmd *cmd) {
+cmdIdof(vshControl * ctl, vshCmd * cmd)
+{
char *name = vshCommandOptString(cmd, "name", NULL);
virDomainPtr dom;
return FALSE;
if (!name)
return FALSE;
-
+
dom = virDomainLookupByName(ctl->conn, name);
if (dom) {
vshPrint(ctl, VSH_MESG, "%d\n", virDomainGetID(dom));
* "version" command
*/
static vshCmdInfo info_version[] = {
- { "syntax", "version" },
- { "help", "show versions" },
- { "desc", "Display the version informations available" },
- { NULL, NULL }
+ {"syntax", "version"},
+ {"help", "show versions"},
+ {"desc", "Display the version informations available"},
+ {NULL, NULL}
};
static int
-cmdVersion(vshControl *ctl, vshCmd *cmd ATTRIBUTE_UNUSED) {
+cmdVersion(vshControl * ctl, vshCmd * cmd ATTRIBUTE_UNUSED)
+{
unsigned long hvVersion;
const char *hvType;
unsigned long libVersion;
if (!vshConnectionUsability(ctl, ctl->conn, TRUE))
return FALSE;
-
+
hvType = virConnectGetType(ctl->conn);
if (hvType == NULL) {
vshError(ctl, FALSE, "failed to get hypervisor type\n");
rel = libVersion % 1000;
vshPrint(ctl, VSH_MESG, "Using library: libvir %d.%d.%d\n",
major, minor, rel);
-
+
major = apiVersion / 1000000;
apiVersion %= 1000000;
minor = apiVersion / 1000;
vshPrint(ctl, VSH_MESG, "Using API: %s %d.%d.%d\n", hvType,
major, minor, rel);
- ret = virConnectGetVersion(ctl->conn, &hvVersion);
+ ret = virConnectGetVersion(ctl->conn, &hvVersion);
if (ret < 0) {
vshError(ctl, FALSE, "failed to get the hypervisor version");
return FALSE;
}
if (hvVersion == 0) {
vshPrint(ctl, VSH_MESG,
- "cannot extract running %s hypervisor version\n",
- hvType);
+ "cannot extract running %s hypervisor version\n", hvType);
} else {
major = hvVersion / 1000000;
hvVersion %= 1000000;
minor = hvVersion / 1000;
rel = hvVersion % 1000;
- vshPrint(ctl, VSH_MESG, "Running hypervisor: %s %d.%d.%d\n", hvType,
- major, minor, rel);
+ vshPrint(ctl, VSH_MESG, "Running hypervisor: %s %d.%d.%d\n",
+ hvType, major, minor, rel);
}
return TRUE;
}
* "quit" command
*/
static vshCmdInfo info_quit[] = {
- { "syntax", "quit" },
- { "help", "quit this interactive terminal" },
- { NULL, NULL }
+ {"syntax", "quit"},
+ {"help", "quit this interactive terminal"},
+ {NULL, NULL}
};
static int
-cmdQuit(vshControl *ctl, vshCmd *cmd ATTRIBUTE_UNUSED) {
+cmdQuit(vshControl * ctl, vshCmd * cmd ATTRIBUTE_UNUSED)
+{
ctl->imode = FALSE;
return TRUE;
}
* Commands
*/
static vshCmdDef commands[] = {
- { "connect", cmdConnect, opts_connect, info_connect },
- { "dinfo", cmdDinfo, opts_dinfo, info_dinfo },
- { "dumpxml", cmdDumpXML, opts_dumpxml, info_dumpxml },
- { "dstate", cmdDstate, opts_dstate, info_dstate },
- { "suspend", cmdSuspend, opts_suspend, info_suspend },
- { "resume", cmdResume, opts_resume, info_resume },
- { "save", cmdSave, opts_save, info_save },
- { "restore", cmdRestore, opts_restore, info_restore },
- { "shutdown", cmdShutdown, opts_shutdown, info_shutdown },
- { "destroy", cmdDestroy, opts_destroy, info_destroy },
- { "help", cmdHelp, opts_help, info_help },
- { "idof", cmdIdof, opts_idof, info_idof },
- { "list", cmdList, NULL, info_list },
- { "nameof", cmdNameof, opts_nameof, info_nameof },
- { "version", cmdVersion, NULL, info_version },
- { "quit", cmdQuit, NULL, info_quit },
- { NULL, NULL, NULL, NULL }
+ {"connect", cmdConnect, opts_connect, info_connect},
+ {"dinfo", cmdDinfo, opts_dinfo, info_dinfo},
+ {"dumpxml", cmdDumpXML, opts_dumpxml, info_dumpxml},
+ {"dstate", cmdDstate, opts_dstate, info_dstate},
+ {"suspend", cmdSuspend, opts_suspend, info_suspend},
+ {"resume", cmdResume, opts_resume, info_resume},
+ {"save", cmdSave, opts_save, info_save},
+ {"restore", cmdRestore, opts_restore, info_restore},
+ {"shutdown", cmdShutdown, opts_shutdown, info_shutdown},
+ {"destroy", cmdDestroy, opts_destroy, info_destroy},
+ {"help", cmdHelp, opts_help, info_help},
+ {"idof", cmdIdof, opts_idof, info_idof},
+ {"list", cmdList, NULL, info_list},
+ {"nameof", cmdNameof, opts_nameof, info_nameof},
+ {"version", cmdVersion, NULL, info_version},
+ {"quit", cmdQuit, NULL, info_quit},
+ {NULL, NULL, NULL, NULL}
};
/* ---------------
* ---------------
*/
static const char *
-vshCmddefGetInfo(vshCmdDef *cmd, const char *name) {
+vshCmddefGetInfo(vshCmdDef * cmd, const char *name)
+{
vshCmdInfo *info;
-
+
for (info = cmd->info; info && info->name; info++) {
- if (strcmp(info->name, name)==0)
+ if (strcmp(info->name, name) == 0)
return info->data;
}
return NULL;
}
static vshCmdOptDef *
-vshCmddefGetOption(vshCmdDef *cmd, const char *name) {
+vshCmddefGetOption(vshCmdDef * cmd, const char *name)
+{
vshCmdOptDef *opt;
-
+
for (opt = cmd->opts; opt && opt->name; opt++)
- if (strcmp(opt->name, name)==0)
+ if (strcmp(opt->name, name) == 0)
return opt;
return NULL;
}
static vshCmdOptDef *
-vshCmddefGetData(vshCmdDef *cmd, int data_ct) {
+vshCmddefGetData(vshCmdDef * cmd, int data_ct)
+{
vshCmdOptDef *opt;
for (opt = cmd->opts; opt && opt->name; opt++) {
- if (opt->type==VSH_OT_DATA) {
- if (data_ct==0)
+ if (opt->type == VSH_OT_DATA) {
+ if (data_ct == 0)
return opt;
else
data_ct--;
/*
* Checks for required options
*/
-static int
-vshCommandCheckOpts(vshControl *ctl, vshCmd *cmd)
+static int
+vshCommandCheckOpts(vshControl * ctl, vshCmd * cmd)
{
vshCmdDef *def = cmd->def;
vshCmdOptDef *d;
- int err=0;
+ int err = 0;
for (d = def->opts; d && d->name; d++) {
if (d->flag & VSH_OFLAG_REQ) {
vshCmdOpt *o = cmd->opts;
- int ok=0;
-
- while(o && ok==0) {
+ int ok = 0;
+
+ while (o && ok == 0) {
if (o->def == d)
- ok=1;
+ ok = 1;
o = o->next;
}
if (!ok) {
- vshError(ctl, FALSE,
- d->type == VSH_OT_DATA ?
- "command '%s' requires <%s> option" :
- "command '%s' requires --%s option",
- def->name, d->name);
+ vshError(ctl, FALSE,
+ d->type == VSH_OT_DATA ?
+ "command '%s' requires <%s> option" :
+ "command '%s' requires --%s option",
+ def->name, d->name);
err = 1;
}
-
+
}
}
return !err;
}
static vshCmdDef *
-vshCmddefSearch(const char *cmdname) {
+vshCmddefSearch(const char *cmdname)
+{
vshCmdDef *c;
-
+
for (c = commands; c->name; c++)
- if (strcmp(c->name, cmdname)==0)
+ if (strcmp(c->name, cmdname) == 0)
return c;
return NULL;
}
static int
-vshCmddefHelp(vshControl *ctl, const char *cmdname, int withprog) {
+vshCmddefHelp(vshControl * ctl, const char *cmdname, int withprog)
+{
vshCmdDef *def = vshCmddefSearch(cmdname);
-
+
if (!def) {
- vshError(ctl, FALSE, "command '%s' doesn't exist", cmdname);
- return FALSE;
- } else {
+ vshError(ctl, FALSE, "command '%s' doesn't exist", cmdname);
+ return FALSE;
+ } else {
vshCmdOptDef *opt;
const char *desc = vshCmddefGetInfo(def, "desc");
const char *help = vshCmddefGetInfo(def, "help");
const char *syntax = vshCmddefGetInfo(def, "syntax");
fputs(" NAME\n", stdout);
- fprintf(stdout, " %s - %s\n", def->name, help);
-
+ fprintf(stdout, " %s - %s\n", def->name, help);
+
if (syntax) {
fputs("\n SYNOPSIS\n", stdout);
if (!withprog)
}
if (def->opts) {
fputs("\n OPTIONS\n", stdout);
- for (opt=def->opts; opt->name; opt++) {
+ for (opt = def->opts; opt->name; opt++) {
char buf[256];
-
- if (opt->type==VSH_OT_BOOL)
+
+ if (opt->type == VSH_OT_BOOL)
snprintf(buf, sizeof(buf), "--%s", opt->name);
- else if (opt->type==VSH_OT_INT)
+ else if (opt->type == VSH_OT_INT)
snprintf(buf, sizeof(buf), "--%s <number>", opt->name);
- else if (opt->type==VSH_OT_STRING)
+ else if (opt->type == VSH_OT_STRING)
snprintf(buf, sizeof(buf), "--%s <string>", opt->name);
- else if (opt->type==VSH_OT_DATA)
+ else if (opt->type == VSH_OT_DATA)
snprintf(buf, sizeof(buf), "<%s>", opt->name);
-
+
fprintf(stdout, " %-15s %s\n", buf, opt->help);
- }
+ }
}
fputc('\n', stdout);
}
* Utils for work with runtime commands data
* ---------------
*/
-static void
-vshCommandOptFree(vshCmdOpt *arg) {
+static void
+vshCommandOptFree(vshCmdOpt * arg)
+{
vshCmdOpt *a = arg;
- while(a) {
+ while (a) {
vshCmdOpt *tmp = a;
+
a = a->next;
if (tmp->data)
}
static void
-vshCommandFree(vshCmd *cmd) {
+vshCommandFree(vshCmd * cmd)
+{
vshCmd *c = cmd;
- while(c) {
+ while (c) {
vshCmd *tmp = c;
+
c = c->next;
if (tmp->opts)
* Returns option by name
*/
static vshCmdOpt *
-vshCommandOpt(vshCmd *cmd, const char *name) {
+vshCommandOpt(vshCmd * cmd, const char *name)
+{
vshCmdOpt *opt = cmd->opts;
-
- while(opt) {
- if (opt->def && strcmp(opt->def->name, name)==0)
+
+ while (opt) {
+ if (opt->def && strcmp(opt->def->name, name) == 0)
return opt;
opt = opt->next;
}
* Returns option as INT
*/
static int
-vshCommandOptInt(vshCmd *cmd, const char *name, int *found) {
+vshCommandOptInt(vshCmd * cmd, const char *name, int *found)
+{
vshCmdOpt *arg = vshCommandOpt(cmd, name);
int res = 0;
-
+
if (arg)
res = atoi(arg->data);
if (found)
* Returns option as STRING
*/
static char *
-vshCommandOptString(vshCmd *cmd, const char *name, int *found) {
+vshCommandOptString(vshCmd * cmd, const char *name, int *found)
+{
vshCmdOpt *arg = vshCommandOpt(cmd, name);
+
if (found)
*found = arg ? TRUE : FALSE;
* Returns TRUE/FALSE if the option exists
*/
static int
-vshCommandOptBool(vshCmd *cmd, const char *name) {
+vshCommandOptBool(vshCmd * cmd, const char *name)
+{
return vshCommandOpt(cmd, name) ? TRUE : FALSE;
}
static virDomainPtr
-vshCommandOptDomain(vshControl *ctl, vshCmd *cmd, const char *optname, char **name) {
+vshCommandOptDomain(vshControl * ctl, vshCmd * cmd, const char *optname,
+ char **name)
+{
virDomainPtr dom = NULL;
char *n, *end = NULL;
int id;
-
+
if (!(n = vshCommandOptString(cmd, optname, NULL))) {
vshError(ctl, FALSE, "undefined domain name or id");
- return NULL;
+ return NULL;
}
-
- vshPrint(ctl, VSH_DEBUG5, "%s: found option <%s>: %s\n", cmd->def->name, optname, n);
-
+
+ vshPrint(ctl, VSH_DEBUG5, "%s: found option <%s>: %s\n",
+ cmd->def->name, optname, n);
+
if (name)
*name = n;
-
+
/* try it by ID */
id = (int) strtol(n, &end, 10);
- if (id >= 0 && end && *end=='\0') {
- vshPrint(ctl, VSH_DEBUG5, "%s: <%s> seems like domain ID\n", cmd->def->name, optname);
+ if (id >= 0 && end && *end == '\0') {
+ vshPrint(ctl, VSH_DEBUG5, "%s: <%s> seems like domain ID\n",
+ cmd->def->name, optname);
dom = virDomainLookupByID(ctl->conn, id);
}
-
+
/* try it by NAME */
if (!dom) {
- vshPrint(ctl, VSH_DEBUG5, "%s: <%s> tring as domain NAME\n", cmd->def->name, optname);
+ vshPrint(ctl, VSH_DEBUG5, "%s: <%s> tring as domain NAME\n",
+ cmd->def->name, optname);
dom = virDomainLookupByName(ctl->conn, n);
}
- if (!dom)
+ if (!dom)
vshError(ctl, FALSE, "failed to get domain '%s'", n);
-
+
return dom;
}
* Executes command(s) and returns return code from last command
*/
static int
-vshCommandRun(vshControl *ctl, vshCmd *cmd) {
+vshCommandRun(vshControl * ctl, vshCmd * cmd)
+{
int ret = TRUE;
-
- while(cmd) {
+
+ while (cmd) {
struct timeval before, after;
-
+
if (ctl->timing)
GETTIMEOFDAY(&before);
-
+
ret = cmd->def->handler(ctl, cmd);
if (ctl->timing)
GETTIMEOFDAY(&after);
-
- if (strcmp(cmd->def->name, "quit")==0) /* hack ... */
+
+ if (strcmp(cmd->def->name, "quit") == 0) /* hack ... */
return ret;
if (ctl->timing)
- vshPrint(ctl, VSH_MESG, "\n(Time: %.3f ms)\n\n", DIFF_MSEC(&after, &before));
- else
+ vshPrint(ctl, VSH_MESG, "\n(Time: %.3f ms)\n\n",
+ DIFF_MSEC(&after, &before));
+ else
vshPrint(ctl, VSH_FOOTER, "\n");
cmd = cmd->next;
}
#define VSH_TK_DATA 2
#define VSH_TK_END 3
-static int
-vshCommandGetToken(vshControl *ctl, char *str, char **end, char **res) {
+static int
+vshCommandGetToken(vshControl * ctl, char *str, char **end, char **res)
+{
int tk = VSH_TK_NONE;
int quote = FALSE;
int sz = 0;
char *p = str;
char *tkstr = NULL;
-
+
*end = NULL;
-
- while(p && *p && isblank((unsigned char) *p))
+
+ while (p && *p && isblank((unsigned char) *p))
p++;
-
- if (p==NULL || *p=='\0')
+
+ if (p == NULL || *p == '\0')
return VSH_TK_END;
- if (*p==';') {
- *end = ++p; /* = \0 or begi of next command */
+ if (*p == ';') {
+ *end = ++p; /* = \0 or begi of next command */
return VSH_TK_END;
}
- while(*p) {
+ while (*p) {
/* end of token is blank space or ';' */
- if ((quote==FALSE && isblank((unsigned char) *p)) || *p==';')
+ if ((quote == FALSE && isblank((unsigned char) *p)) || *p == ';')
break;
-
+
/* end of option name could be '=' */
- if (tk==VSH_TK_OPTION && *p=='=') {
- p++; /* skip '=' */
+ if (tk == VSH_TK_OPTION && *p == '=') {
+ p++; /* skip '=' */
break;
}
-
- if (tk==VSH_TK_NONE) {
- if (*p=='-' && *(p+1)=='-' && *(p+2) && isalnum((unsigned char) *(p+2))) {
+
+ if (tk == VSH_TK_NONE) {
+ if (*p == '-' && *(p + 1) == '-' && *(p + 2)
+ && isalnum((unsigned char) *(p + 2))) {
tk = VSH_TK_OPTION;
- p+=2;
+ p += 2;
} else {
tk = VSH_TK_DATA;
- if (*p=='"') {
- quote = TRUE;
+ if (*p == '"') {
+ quote = TRUE;
p++;
} else {
quote = FALSE;
}
}
- tkstr = p; /* begin of token */
- } else if (quote && *p=='"') {
+ tkstr = p; /* begin of token */
+ } else if (quote && *p == '"') {
quote = FALSE;
p++;
- break; /* end of "..." token */
+ break; /* end of "..." token */
}
p++;
sz++;
vshError(ctl, FALSE, "missing \"");
return VSH_TK_ERROR;
}
- if (tkstr==NULL || *tkstr=='\0' || p==NULL)
+ if (tkstr == NULL || *tkstr == '\0' || p == NULL)
return VSH_TK_END;
- if (sz==0)
+ if (sz == 0)
return VSH_TK_END;
-
- *res = malloc(sz+1);
+
+ *res = malloc(sz + 1);
memcpy(*res, tkstr, sz);
- *(*res+sz) = '\0';
+ *(*res + sz) = '\0';
*end = p;
return tk;
}
static int
-vshCommandParse(vshControl *ctl, char *cmdstr) {
+vshCommandParse(vshControl * ctl, char *cmdstr)
+{
char *str;
char *tkdata = NULL;
vshCmd *clast = NULL;
vshCmdOpt *first = NULL;
-
+
if (ctl->cmd) {
vshCommandFree(ctl->cmd);
ctl->cmd = NULL;
}
-
- if (cmdstr==NULL || *cmdstr=='\0')
+
+ if (cmdstr == NULL || *cmdstr == '\0')
return FALSE;
-
+
str = cmdstr;
- while(str && *str)
- {
+ while (str && *str) {
vshCmdOpt *last = NULL;
vshCmdDef *cmd = NULL;
int tk = VSH_TK_NONE;
int data_ct = 0;
-
+
first = NULL;
-
- while (tk!=VSH_TK_END) {
+
+ while (tk != VSH_TK_END) {
char *end = NULL;
vshCmdOptDef *opt = NULL;
-
+
tkdata = NULL;
-
+
/* get token */
tk = vshCommandGetToken(ctl, str, &end, &tkdata);
-
+
str = end;
-
- if (tk==VSH_TK_END)
+
+ if (tk == VSH_TK_END)
break;
- if (tk==VSH_TK_ERROR)
+ if (tk == VSH_TK_ERROR)
goto syntaxError;
-
- if (cmd==NULL) {
+
+ if (cmd == NULL) {
/* first token must be command name */
- if (tk!=VSH_TK_DATA) {
- vshError(ctl, FALSE,
- "unexpected token (command name): '%s'",
- tkdata);
+ if (tk != VSH_TK_DATA) {
+ vshError(ctl, FALSE,
+ "unexpected token (command name): '%s'",
+ tkdata);
goto syntaxError;
}
if (!(cmd = vshCmddefSearch(tkdata))) {
- vshError(ctl, FALSE,
- "unknown command: '%s'", tkdata);
- goto syntaxError; /* ... or ignore this command only? */
+ vshError(ctl, FALSE, "unknown command: '%s'", tkdata);
+ goto syntaxError; /* ... or ignore this command only? */
}
free(tkdata);
- } else if (tk==VSH_TK_OPTION) {
+ } else if (tk == VSH_TK_OPTION) {
if (!(opt = vshCmddefGetOption(cmd, tkdata))) {
vshError(ctl, FALSE,
- "command '%s' doesn't support option --%s",
- cmd->name, tkdata);
+ "command '%s' doesn't support option --%s",
+ cmd->name, tkdata);
goto syntaxError;
}
- free(tkdata); /* option name */
+ free(tkdata); /* option name */
tkdata = NULL;
if (opt->type != VSH_OT_BOOL) {
/* option data */
tk = vshCommandGetToken(ctl, str, &end, &tkdata);
- str = end;
- if (tk==VSH_TK_ERROR)
+ str = end;
+ if (tk == VSH_TK_ERROR)
goto syntaxError;
- if (tk!=VSH_TK_DATA) {
+ if (tk != VSH_TK_DATA) {
vshError(ctl, FALSE,
- "expected syntax: --%s <%s>",
- opt->name,
- opt->type==VSH_OT_INT ? "number" : "string");
+ "expected syntax: --%s <%s>",
+ opt->name,
+ opt->type ==
+ VSH_OT_INT ? "number" : "string");
goto syntaxError;
}
}
- } else if (tk==VSH_TK_DATA) {
+ } else if (tk == VSH_TK_DATA) {
if (!(opt = vshCmddefGetData(cmd, data_ct++))) {
- vshError(ctl, FALSE,
- "unexpected data '%s'",
- tkdata);
+ vshError(ctl, FALSE, "unexpected data '%s'", tkdata);
goto syntaxError;
}
}
if (opt) {
/* save option */
vshCmdOpt *arg = malloc(sizeof(vshCmdOpt));
-
+
arg->def = opt;
arg->data = tkdata;
arg->next = NULL;
tkdata = NULL;
-
+
if (!first)
first = arg;
if (last)
last->next = arg;
last = arg;
-
+
vshPrint(ctl, VSH_DEBUG4, "%s: %s(%s): %s\n",
- cmd->name,
- opt->name,
- tk==VSH_TK_OPTION ? "OPTION" : "DATA",
- arg->data);
+ cmd->name,
+ opt->name,
+ tk == VSH_TK_OPTION ? "OPTION" : "DATA",
+ arg->data);
}
if (!str)
break;
}
-
+
/* commad parsed -- allocate new struct for the command */
if (cmd) {
- vshCmd *c = malloc(sizeof(vshCmd));
+ vshCmd *c = malloc(sizeof(vshCmd));
+
c->opts = first;
c->def = cmd;
c->next = NULL;
if (!vshCommandCheckOpts(ctl, c))
goto syntaxError;
-
+
if (!ctl->cmd)
ctl->cmd = c;
if (clast)
clast = c;
}
}
-
+
return TRUE;
-syntaxError:
+ syntaxError:
if (ctl->cmd)
vshCommandFree(ctl->cmd);
if (first)
vshCommandOptFree(first);
if (tkdata)
free(tkdata);
- return FALSE;
+ return FALSE;
}
* ---------------
*/
static const char *
-vshDomainStateToString(int state) {
+vshDomainStateToString(int state)
+{
switch (state) {
case VIR_DOMAIN_RUNNING:
- return "running ";
+ return "running ";
case VIR_DOMAIN_BLOCKED:
return "blocked ";
case VIR_DOMAIN_PAUSED:
case VIR_DOMAIN_SHUTOFF:
return "shut off";
default:
- return "no state"; /* = dom0 state */
+ return "no state"; /* = dom0 state */
}
return NULL;
}
static int
-vshConnectionUsability(vshControl *ctl, virConnectPtr conn, int showerror) {
+vshConnectionUsability(vshControl * ctl, virConnectPtr conn, int showerror)
+{
/* TODO: use something like virConnectionState() to
* check usability of the connection
*/
}
static int
-vshWantedDebug(vshOutType type, int mode) {
- switch(type) {
+vshWantedDebug(vshOutType type, int mode)
+{
+ switch (type) {
case VSH_DEBUG5:
if (mode < 5)
return FALSE;
}
static void
-vshPrint(vshControl *ctl, vshOutType type, const char *format, ...) {
+vshPrint(vshControl * ctl, vshOutType type, const char *format, ...)
+{
va_list ap;
-
- if (ctl->quiet==TRUE && (type==VSH_HEADER || type==VSH_FOOTER))
+
+ if (ctl->quiet == TRUE && (type == VSH_HEADER || type == VSH_FOOTER))
return;
if (!vshWantedDebug(type, ctl->debug))
return;
-
+
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
}
static void
-vshError(vshControl *ctl, int doexit, const char *format, ...) {
+vshError(vshControl * ctl, int doexit, const char *format, ...)
+{
va_list ap;
-
+
if (doexit)
fprintf(stderr, "%s: error: ", progname);
else
fputs("error: ", stderr);
-
+
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
fputc('\n', stderr);
-
+
if (doexit) {
vshDeinit(ctl);
exit(EXIT_FAILURE);
* Initialize vistsh
*/
static int
-vshInit(vshControl *ctl) {
+vshInit(vshControl * ctl)
+{
if (ctl->conn)
return FALSE;
ctl->uid = getuid();
-
+
/* set up the library error handler */
virSetErrorFunc(NULL, virshErrorHandler);
-
+
/* basic connection to hypervisor */
if (ctl->uid == 0)
ctl->conn = virConnectOpen(NULL);
else
ctl->conn = virConnectOpenReadOnly(NULL);
-
+
if (!ctl->conn)
vshError(ctl, TRUE, "failed to connect to the hypervisor");
* (i.e. STATE == 0), then we start at the top of the list.
*/
static char *
-vshReadlineCommandGenerator(const char *text, int state) {
+vshReadlineCommandGenerator(const char *text, int state)
+{
static int list_index, len;
const char *name;
*/
if (!state) {
list_index = 0;
- len = strlen (text);
+ len = strlen(text);
}
/* Return the next name which partially matches from the
*/
while ((name = commands[list_index].name)) {
list_index++;
- if (strncmp (name, text, len) == 0)
+ if (strncmp(name, text, len) == 0)
return strdup(name);
}
}
static char *
-vshReadlineOptionsGenerator(const char *text, int state) {
+vshReadlineOptionsGenerator(const char *text, int state)
+{
static int list_index, len;
static vshCmdDef *cmd = NULL;
const char *name;
if (!(p = strchr(rl_line_buffer, ' ')))
return NULL;
- cmdname = calloc((p - rl_line_buffer)+ 1, 1);
- memcpy(cmdname, rl_line_buffer, p-rl_line_buffer);
+ cmdname = calloc((p - rl_line_buffer) + 1, 1);
+ memcpy(cmdname, rl_line_buffer, p - rl_line_buffer);
cmd = vshCmddefSearch(cmdname);
list_index = 0;
- len = strlen (text);
+ len = strlen(text);
free(cmdname);
}
if (!cmd)
return NULL;
-
+
while ((name = cmd->opts[list_index].name)) {
vshCmdOptDef *opt = &cmd->opts[list_index];
char *res;
+
list_index++;
-
+
if (opt->type == VSH_OT_DATA)
/* ignore non --option */
continue;
-
+
if (len > 2) {
- if (strncmp (name, text+2, len-2))
+ if (strncmp(name, text + 2, len - 2))
continue;
}
- res = malloc(strlen(name)+3);
+ res = malloc(strlen(name) + 3);
sprintf(res, "--%s", name);
return res;
}
}
static char **
-vshReadlineCompletion(const char *text, int start, int end ATTRIBUTE_UNUSED) {
+vshReadlineCompletion(const char *text, int start,
+ int end ATTRIBUTE_UNUSED)
+{
char **matches = (char **) NULL;
- if (start==0)
+ if (start == 0)
/* command name generator */
- matches = rl_completion_matches (text, vshReadlineCommandGenerator);
+ matches = rl_completion_matches(text, vshReadlineCommandGenerator);
else
/* commands options */
- matches = rl_completion_matches (text, vshReadlineOptionsGenerator);
+ matches = rl_completion_matches(text, vshReadlineOptionsGenerator);
return matches;
}
static void
-vshReadlineInit(void) {
+vshReadlineInit(void)
+{
/* Allow conditional parsing of the ~/.inputrc file. */
rl_readline_name = "virsh";
* Deinitliaze virsh
*/
static int
-vshDeinit(vshControl *ctl) {
+vshDeinit(vshControl * ctl)
+{
if (ctl->conn) {
- if (virConnectClose(ctl->conn)!=0) {
- ctl->conn = NULL; /* prevent recursive call from vshError() */
- vshError(ctl, TRUE, "failed to disconnect from the hypervisor");
+ if (virConnectClose(ctl->conn) != 0) {
+ ctl->conn = NULL; /* prevent recursive call from vshError() */
+ vshError(ctl, TRUE,
+ "failed to disconnect from the hypervisor");
}
}
return TRUE;
}
-
+
/*
* Print usage
*/
static void
-vshUsage(vshControl *ctl, const char *cmdname) {
+vshUsage(vshControl * ctl, const char *cmdname)
+{
vshCmdDef *cmd;
-
+
/* global help */
if (!cmdname) {
fprintf(stdout, "\n%s [options] [commands]\n\n"
" -t | --timing print timing information\n"
" -v | --version program version\n\n"
" commands (non interactive mode):\n", progname);
-
- for(cmd = commands; cmd->name; cmd++)
- fprintf(stdout,
- " %-15s %s\n", cmd->name, vshCmddefGetInfo(cmd, "help"));
-
- fprintf(stdout, "\n (specify --help <command> for details about the command)\n\n");
+
+ for (cmd = commands; cmd->name; cmd++)
+ fprintf(stdout,
+ " %-15s %s\n", cmd->name, vshCmddefGetInfo(cmd,
+ "help"));
+
+ fprintf(stdout,
+ "\n (specify --help <command> for details about the command)\n\n");
return;
}
if (!vshCmddefHelp(ctl, cmdname, TRUE))
*
*/
static int
-vshParseArgv(vshControl *ctl, int argc, char **argv) {
+vshParseArgv(vshControl * ctl, int argc, char **argv)
+{
char *last = NULL;
int i, end = 0, help = 0;
- int arg, idx=0;
+ int arg, idx = 0;
struct option opt[] = {
- { "debug", 1, 0, 'd' },
- { "help", 0, 0, 'h' },
- { "quiet", 0, 0, 'q' },
- { "timing", 0, 0, 't' },
- { "version", 0, 0, 'v' },
+ {"debug", 1, 0, 'd'},
+ {"help", 0, 0, 'h'},
+ {"quiet", 0, 0, 'q'},
+ {"timing", 0, 0, 't'},
+ {"version", 0, 0, 'v'},
{0, 0, 0, 0}
- };
+ };
+
-
if (argc < 2)
return TRUE;
-
+
/* look for begin of the command, for example:
* ./virsh --debug 5 -q command --cmdoption
* <--- ^ --->
* getopt() stuff | command suff
*/
- for(i=1; i < argc; i++) {
+ for (i = 1; i < argc; i++) {
if (*argv[i] != '-') {
int valid = FALSE;
-
+
/* non "--option" argv, is it command? */
if (last) {
struct option *o;
int sz = strlen(last);
-
- for(o=opt; o->name; o++) {
- if (sz==2 && *(last+1)==o->val)
+
+ for (o = opt; o->name; o++) {
+ if (sz == 2 && *(last + 1) == o->val)
/* valid virsh short option */
valid = TRUE;
- else if (sz > 2 && strcmp(o->name, last+2)==0)
+ else if (sz > 2 && strcmp(o->name, last + 2) == 0)
/* valid virsh long option */
valid = TRUE;
}
last = argv[i];
}
end = end ? : argc;
-
+
/* standard (non-command) options */
- while((arg = getopt_long(end, argv, "d:hqtv", opt, &idx)) != -1) {
- switch(arg) {
+ while ((arg = getopt_long(end, argv, "d:hqtv", opt, &idx)) != -1) {
+ switch (arg) {
case 'd':
ctl->debug = atoi(optarg);
break;
fprintf(stdout, "%s\n", VERSION);
exit(EXIT_SUCCESS);
default:
- vshError(ctl, TRUE, "unsupported option '-%c'. See --help.", arg);
+ vshError(ctl, TRUE,
+ "unsupported option '-%c'. See --help.", arg);
break;
}
}
/* global or command specific help */
vshUsage(ctl, argc > end ? argv[end] : NULL);
exit(EXIT_SUCCESS);
- }
-
+ }
+
if (argc > end) {
/* parse command */
char *cmdstr;
- int sz=0, ret;
-
+ int sz = 0, ret;
+
ctl->imode = FALSE;
-
- for (i=end; i < argc; i++)
- sz += strlen(argv[i]) + 1; /* +1 is for blank space between items */
- cmdstr = calloc(sz+1, 1);
-
- for (i=end; i < argc; i++) {
+ for (i = end; i < argc; i++)
+ sz += strlen(argv[i]) + 1; /* +1 is for blank space between items */
+
+ cmdstr = calloc(sz + 1, 1);
+
+ for (i = end; i < argc; i++) {
strncat(cmdstr, argv[i], sz);
sz -= strlen(argv[i]);
strncat(cmdstr, " ", sz--);
}
vshPrint(ctl, VSH_DEBUG2, "command: \"%s\"\n", cmdstr);
ret = vshCommandParse(ctl, cmdstr);
-
+
free(cmdstr);
return ret;
}
return TRUE;
}
-int
-main(int argc, char **argv) {
- vshControl _ctl, *ctl=&_ctl;
+int
+main(int argc, char **argv)
+{
+ vshControl _ctl, *ctl = &_ctl;
int ret = TRUE;
- if (!(progname=strrchr(argv[0], '/')))
+ if (!(progname = strrchr(argv[0], '/')))
progname = argv[0];
else
progname++;
-
+
memset(ctl, 0, sizeof(vshControl));
- ctl->imode = TRUE; /* default is interactive mode */
+ ctl->imode = TRUE; /* default is interactive mode */
if (!vshParseArgv(ctl, argc, argv))
exit(EXIT_FAILURE);
-
+
if (!vshInit(ctl))
exit(EXIT_FAILURE);
-
+
if (!ctl->imode) {
- ret = vshCommandRun(ctl, ctl->cmd);
+ ret = vshCommandRun(ctl, ctl->cmd);
} else {
/* interactive mode */
if (!ctl->quiet) {
- vshPrint(ctl, VSH_MESG, "Welcome to %s, the virtualization interactive terminal.\n\n",
- progname);
- vshPrint(ctl, VSH_MESG, "Type: 'help' for help with commands\n"
- " 'quit' to quit\n\n");
+ vshPrint(ctl, VSH_MESG,
+ "Welcome to %s, the virtualization interactive terminal.\n\n",
+ progname);
+ vshPrint(ctl, VSH_MESG,
+ "Type: 'help' for help with commands\n"
+ " 'quit' to quit\n\n");
}
vshReadlineInit();
do {
- ctl->cmdstr = readline(ctl->uid==0 ? VSH_PROMPT_RW : VSH_PROMPT_RO);
- if (ctl->cmdstr==NULL)
- break; /* EOF */
+ ctl->cmdstr =
+ readline(ctl->uid == 0 ? VSH_PROMPT_RW : VSH_PROMPT_RO);
+ if (ctl->cmdstr == NULL)
+ break; /* EOF */
if (*ctl->cmdstr) {
add_history(ctl->cmdstr);
if (vshCommandParse(ctl, ctl->cmdstr))
}
free(ctl->cmdstr);
ctl->cmdstr = NULL;
- } while(ctl->imode);
+ } while (ctl->imode);
- if (ctl->cmdstr==NULL)
- fputc('\n', stdout); /* line break after alone prompt */
+ if (ctl->cmdstr == NULL)
+ fputc('\n', stdout); /* line break after alone prompt */
}
-
+
vshDeinit(ctl);
exit(ret ? EXIT_SUCCESS : EXIT_FAILURE);
}
* vim: set shiftwidth=4:
* vim: set expandtab:
*/
-
#include "virterror.h"
#include "internal.h"
-static virError lastErr = /* the last error */
-{ 0, 0, NULL, VIR_ERR_NONE, NULL, NULL, NULL, NULL, NULL, 0, 0};
-static virErrorFunc virErrorHandler = NULL;/* global error handlet */
-static void *virUserData = NULL; /* associated data */
+static virError lastErr = /* the last error */
+{ 0, 0, NULL, VIR_ERR_NONE, NULL, NULL, NULL, NULL, NULL, 0, 0 };
+static virErrorFunc virErrorHandler = NULL; /* global error handlet */
+static void *virUserData = NULL; /* associated data */
/*
* Macro used to format the message as a string in __virRaiseError
* Returns a pointer to the last error or NULL if none occured.
*/
virErrorPtr
-virGetLastError(void) {
+virGetLastError(void)
+{
if (lastErr.code == VIR_ERR_OK)
- return(NULL);
- return(&lastErr);
+ return (NULL);
+ return (&lastErr);
}
/*
* of parameter error.
*/
int
-virCopyLastError(virErrorPtr to) {
+virCopyLastError(virErrorPtr to)
+{
if (to == NULL)
- return(-1);
+ return (-1);
if (lastErr.code == VIR_ERR_OK)
- return(0);
+ return (0);
memcpy(to, &lastErr, sizeof(virError));
- return(lastErr.code);
+ return (lastErr.code);
}
/**
* Reset the error being pointed to
*/
void
-virResetError(virErrorPtr err) {
+virResetError(virErrorPtr err)
+{
if (err == NULL)
return;
if (err->message != NULL)
* Reset the last error caught at the library level.
*/
void
-virResetLastError(void) {
+virResetLastError(void)
+{
virResetError(&lastErr);
}
* Returns a pointer to the last error or NULL if none occured.
*/
virErrorPtr
-virConnGetLastError(virConnectPtr conn) {
+virConnGetLastError(virConnectPtr conn)
+{
if (conn == NULL)
- return(NULL);
- return(&conn->err);
+ return (NULL);
+ return (&conn->err);
}
/**
* of parameter error.
*/
int
-virConnCopyLastError(virConnectPtr conn, virErrorPtr to) {
+virConnCopyLastError(virConnectPtr conn, virErrorPtr to)
+{
if (conn == NULL)
- return(-1);
+ return (-1);
if (to == NULL)
- return(-1);
+ return (-1);
if (conn->err.code == VIR_ERR_OK)
- return(0);
+ return (0);
memcpy(to, &conn->err, sizeof(virError));
- return(conn->err.code);
+ return (conn->err.code);
}
/**
* Reset the last error caught on that connection
*/
void
-virConnResetLastError(virConnectPtr conn) {
+virConnResetLastError(virConnectPtr conn)
+{
if (conn == NULL)
return;
virResetError(&conn->err);
* are those for which no handler at the connection level could caught.
*/
void
-virSetErrorFunc(void *userData, virErrorFunc handler) {
+virSetErrorFunc(void *userData, virErrorFunc handler)
+{
virErrorHandler = handler;
virUserData = userData;
}
* library handler.
*/
void
-virConnSetErrorFunc(virConnectPtr conn, void *userData, virErrorFunc handler) {
+virConnSetErrorFunc(virConnectPtr conn, void *userData,
+ virErrorFunc handler)
+{
if (conn == NULL)
return;
conn->handler = handler;
* Default routine reporting an error to stderr.
*/
void
-virDefaultErrorFunc(virErrorPtr err) {
+virDefaultErrorFunc(virErrorPtr err)
+{
const char *lvl = "", *dom = "", *domain = "";
int len;
return;
switch (err->level) {
case VIR_ERR_NONE:
- lvl = "";
- break;
+ lvl = "";
+ break;
case VIR_ERR_WARNING:
- lvl = "warning";
- break;
+ lvl = "warning";
+ break;
case VIR_ERR_ERROR:
- lvl = "error";
- break;
- }
+ lvl = "error";
+ break;
+ }
switch (err->domain) {
case VIR_FROM_NONE:
- dom = "";
- break;
+ dom = "";
+ break;
case VIR_FROM_XEN:
- dom = "Xen ";
- break;
+ dom = "Xen ";
+ break;
case VIR_FROM_XEND:
- dom = "Xen Daemon ";
- break;
+ dom = "Xen Daemon ";
+ break;
case VIR_FROM_DOM:
- dom = "Domain ";
- break;
+ dom = "Domain ";
+ break;
}
if ((err->dom != NULL) && (err->code != VIR_ERR_INVALID_DOMAIN)) {
domain = err->dom->name;
}
len = strlen(err->message);
if ((len == 0) || (err->message[len - 1] != '\n'))
- fprintf(stderr, "libvir: %s%s %s: %s\n",
- dom, lvl, domain, err->message);
- else
- fprintf(stderr, "libvir: %s%s %s: %s",
- dom, lvl, domain, err->message);
+ fprintf(stderr, "libvir: %s%s %s: %s\n",
+ dom, lvl, domain, err->message);
+ else
+ fprintf(stderr, "libvir: %s%s %s: %s",
+ dom, lvl, domain, err->message);
}
/**
__virRaiseError(virConnectPtr conn, virDomainPtr dom,
int domain, int code, virErrorLevel level,
const char *str1, const char *str2, const char *str3,
- int int1, int int2, const char *msg, ...) {
+ int int1, int int2, const char *msg, ...)
+{
virErrorPtr to = &lastErr;
void *userData = virUserData;
virErrorFunc handler = virErrorHandler;
char *str;
if (code == VIR_ERR_OK)
- return;
+ return;
/*
* try to find the best place to save and report the error
*/
if (conn != NULL) {
to = &conn->err;
- if (conn->handler != NULL) {
- handler = conn->handler;
- userData = conn->userData;
- }
+ if (conn->handler != NULL) {
+ handler = conn->handler;
+ userData = conn->userData;
+ }
}
/*
* Returns the constant string associated to @error
*/
const char *
-__virErrorMsg(virErrorNumber error, const char *info) {
+__virErrorMsg(virErrorNumber error, const char *info)
+{
const char *errmsg = NULL;
switch (error) {
case VIR_ERR_OK:
- return(NULL);
- case VIR_ERR_INTERNAL_ERROR:
- if (info != NULL)
- errmsg = "internal error %s";
- else
- errmsg = "internal error";
- break;
- case VIR_ERR_NO_MEMORY:
- errmsg = "out of memory";
- break;
- case VIR_ERR_NO_SUPPORT:
- errmsg = "no support for hypervisor %s";
- break;
- case VIR_ERR_NO_CONNECT:
- if (info == NULL)
- errmsg = "could not connect to hypervisor";
- else
- errmsg = "could not connect to %s";
- break;
- case VIR_ERR_INVALID_CONN:
- errmsg = "invalid connection pointer in";
- break;
- case VIR_ERR_INVALID_DOMAIN:
- errmsg = "invalid domain pointer in";
- break;
- case VIR_ERR_INVALID_ARG:
- errmsg = "invalid domain pointer in";
- break;
- case VIR_ERR_OPERATION_FAILED:
- if (info != NULL)
- errmsg = "operation failed: %s";
- else
- errmsg = "operation failed";
- break;
- case VIR_ERR_GET_FAILED:
- if (info != NULL)
- errmsg = "GET operation failed: %s";
- else
- errmsg = "GET operation failed";
- break;
- case VIR_ERR_POST_FAILED:
- if (info != NULL)
- errmsg = "POST operation failed: %s";
- else
- errmsg = "POST operation failed";
- break;
- case VIR_ERR_HTTP_ERROR:
- errmsg = "got unknown HTTP error code %d";
- break;
- case VIR_ERR_UNKNOWN_HOST:
- errmsg = "unknown host %s";
- break;
- case VIR_ERR_SEXPR_SERIAL:
- if (info != NULL)
- errmsg = "failed to serialize S-Expr: %s";
- else
- errmsg = "failed to serialize S-Expr";
- break;
- case VIR_ERR_NO_XEN:
- if (info == NULL)
- errmsg = "could not use Xen hypervisor entry";
- else
- errmsg = "could not use Xen hypervisor entry %s";
- break;
- case VIR_ERR_XEN_CALL:
- errmsg = "failed Xen syscall %s %d";
- break;
- case VIR_ERR_OS_TYPE:
- if (info == NULL)
- errmsg = "unknown OS type";
- else
- errmsg = "unknown OS type %s";
- break;
- case VIR_ERR_NO_KERNEL:
- errmsg = "missing kernel informations";
- break;
- case VIR_ERR_NO_ROOT:
- if (info == NULL)
- errmsg = "missing root device informations";
- else
- errmsg = "missing root device informations in %s";
- break;
- case VIR_ERR_NO_SOURCE:
- if (info == NULL)
- errmsg = "missing source informations for device";
- else
- errmsg = "missing source informations for device %s";
- break;
- case VIR_ERR_NO_TARGET:
- if (info == NULL)
- errmsg = "missing target informations for device";
- else
- errmsg = "missing target informations for device %s";
- break;
- case VIR_ERR_NO_NAME:
- if (info == NULL)
- errmsg = "missing domain name informations";
- else
- errmsg = "missing domain name informations in %s";
- break;
- case VIR_ERR_NO_OS:
- if (info == NULL)
- errmsg = "missing operating system informations";
- else
- errmsg = "missing operating system informations for %s";
- break;
- case VIR_ERR_NO_DEVICE:
- if (info == NULL)
- errmsg = "missing devices informations";
- else
- errmsg = "missing devices informations for %s";
- break;
+ return (NULL);
+ case VIR_ERR_INTERNAL_ERROR:
+ if (info != NULL)
+ errmsg = "internal error %s";
+ else
+ errmsg = "internal error";
+ break;
+ case VIR_ERR_NO_MEMORY:
+ errmsg = "out of memory";
+ break;
+ case VIR_ERR_NO_SUPPORT:
+ errmsg = "no support for hypervisor %s";
+ break;
+ case VIR_ERR_NO_CONNECT:
+ if (info == NULL)
+ errmsg = "could not connect to hypervisor";
+ else
+ errmsg = "could not connect to %s";
+ break;
+ case VIR_ERR_INVALID_CONN:
+ errmsg = "invalid connection pointer in";
+ break;
+ case VIR_ERR_INVALID_DOMAIN:
+ errmsg = "invalid domain pointer in";
+ break;
+ case VIR_ERR_INVALID_ARG:
+ errmsg = "invalid domain pointer in";
+ break;
+ case VIR_ERR_OPERATION_FAILED:
+ if (info != NULL)
+ errmsg = "operation failed: %s";
+ else
+ errmsg = "operation failed";
+ break;
+ case VIR_ERR_GET_FAILED:
+ if (info != NULL)
+ errmsg = "GET operation failed: %s";
+ else
+ errmsg = "GET operation failed";
+ break;
+ case VIR_ERR_POST_FAILED:
+ if (info != NULL)
+ errmsg = "POST operation failed: %s";
+ else
+ errmsg = "POST operation failed";
+ break;
+ case VIR_ERR_HTTP_ERROR:
+ errmsg = "got unknown HTTP error code %d";
+ break;
+ case VIR_ERR_UNKNOWN_HOST:
+ errmsg = "unknown host %s";
+ break;
+ case VIR_ERR_SEXPR_SERIAL:
+ if (info != NULL)
+ errmsg = "failed to serialize S-Expr: %s";
+ else
+ errmsg = "failed to serialize S-Expr";
+ break;
+ case VIR_ERR_NO_XEN:
+ if (info == NULL)
+ errmsg = "could not use Xen hypervisor entry";
+ else
+ errmsg = "could not use Xen hypervisor entry %s";
+ break;
+ case VIR_ERR_XEN_CALL:
+ errmsg = "failed Xen syscall %s %d";
+ break;
+ case VIR_ERR_OS_TYPE:
+ if (info == NULL)
+ errmsg = "unknown OS type";
+ else
+ errmsg = "unknown OS type %s";
+ break;
+ case VIR_ERR_NO_KERNEL:
+ errmsg = "missing kernel informations";
+ break;
+ case VIR_ERR_NO_ROOT:
+ if (info == NULL)
+ errmsg = "missing root device informations";
+ else
+ errmsg = "missing root device informations in %s";
+ break;
+ case VIR_ERR_NO_SOURCE:
+ if (info == NULL)
+ errmsg = "missing source informations for device";
+ else
+ errmsg = "missing source informations for device %s";
+ break;
+ case VIR_ERR_NO_TARGET:
+ if (info == NULL)
+ errmsg = "missing target informations for device";
+ else
+ errmsg = "missing target informations for device %s";
+ break;
+ case VIR_ERR_NO_NAME:
+ if (info == NULL)
+ errmsg = "missing domain name informations";
+ else
+ errmsg = "missing domain name informations in %s";
+ break;
+ case VIR_ERR_NO_OS:
+ if (info == NULL)
+ errmsg = "missing operating system informations";
+ else
+ errmsg = "missing operating system informations for %s";
+ break;
+ case VIR_ERR_NO_DEVICE:
+ if (info == NULL)
+ errmsg = "missing devices informations";
+ else
+ errmsg = "missing devices informations for %s";
+ break;
}
- return(errmsg);
+ return (errmsg);
}
-
#include <xen/xen.h>
#ifndef __LINUX_PUBLIC_PRIVCMD_H__
-typedef struct hypercall_struct
-{
+typedef struct hypercall_struct {
unsigned long op;
unsigned long arg[5];
} hypercall_t;
* Handle an error at the xend daemon interface
*/
static void
-virXenError(virErrorNumber error, const char *info, int value) {
+virXenError(virErrorNumber error, const char *info, int value)
+{
const char *errmsg;
-
+
if (error == VIR_ERR_OK)
return;
errmsg = __virErrorMsg(error, info);
__virRaiseError(NULL, NULL, VIR_FROM_XEN, error, VIR_ERR_ERROR,
- errmsg, info, NULL, value, 0, errmsg, info,
- value);
+ errmsg, info, NULL, value, 0, errmsg, info, value);
}
/**
*
* Returns the handle or -1 in case of error.
*/
-int xenHypervisorOpen(int quiet) {
+int
+xenHypervisorOpen(int quiet)
+{
int ret;
ret = open(XEN_HYPERVISOR_SOCKET, O_RDWR);
if (ret < 0) {
- if (!quiet)
- virXenError(VIR_ERR_NO_XEN, XEN_HYPERVISOR_SOCKET, 0);
- return(-1);
+ if (!quiet)
+ virXenError(VIR_ERR_NO_XEN, XEN_HYPERVISOR_SOCKET, 0);
+ return (-1);
}
- return(ret);
+ return (ret);
}
/**
*
* Returns 0 in case of success or -1 in case of error.
*/
-int xenHypervisorClose(int handle) {
+int
+xenHypervisorClose(int handle)
+{
int ret;
if (handle < 0)
- return(-1);
+ return (-1);
ret = close(handle);
if (ret < 0)
- return(-1);
- return(0);
+ return (-1);
+ return (0);
}
/**
* Returns 0 in case of success and -1 in case of error.
*/
static int
-xenHypervisorDoOp(int handle, dom0_op_t *op) {
+xenHypervisorDoOp(int handle, dom0_op_t * op)
+{
int ret;
unsigned int cmd;
hypercall_t hc;
op->interface_version = DOM0_INTERFACE_VERSION;
hc.op = __HYPERVISOR_dom0_op;
- hc.arg[0] = (unsigned long)op;
+ hc.arg[0] = (unsigned long) op;
if (mlock(op, sizeof(dom0_op_t)) < 0) {
virXenError(VIR_ERR_XEN_CALL, " locking", sizeof(dom0_op_t));
- return(-1);
+ return (-1);
}
cmd = _IOC(_IOC_NONE, 'P', 0, sizeof(hc));
}
if (ret < 0)
- return(-1);
-
- return(0);
+ return (-1);
+
+ return (0);
}
/**
* Returns the hypervisor running version or 0 in case of error.
*/
unsigned long
-xenHypervisorGetVersion(int handle) {
+xenHypervisorGetVersion(int handle)
+{
int ret;
unsigned int cmd;
hypercall_t hc;
hc.op = __HYPERVISOR_xen_version;
- hc.arg[0] = (unsigned long) XENVER_version;
+ hc.arg[0] = (unsigned long) XENVER_version;
hc.arg[1] = 0;
cmd = _IOC(_IOC_NONE, 'P', 0, sizeof(hc));
if (ret < 0) {
virXenError(VIR_ERR_XEN_CALL, " getting version ", XENVER_version);
- return(0);
+ return (0);
}
/*
* use unsigned long in case the version grows behind expectations
* allowed by int
*/
- return((unsigned long) ret);
+ return ((unsigned long) ret);
}
/**
* Returns 0 in case of success, -1 in case of error.
*/
int
-xenHypervisorGetDomainInfo(int handle, int domain, dom0_getdomaininfo_t *info) {
+xenHypervisorGetDomainInfo(int handle, int domain,
+ dom0_getdomaininfo_t * info)
+{
dom0_op_t op;
int ret;
if (info == NULL)
- return(-1);
+ return (-1);
memset(info, 0, sizeof(dom0_getdomaininfo_t));
if (mlock(info, sizeof(dom0_getdomaininfo_t)) < 0) {
- virXenError(VIR_ERR_XEN_CALL, " locking", sizeof(dom0_getdomaininfo_t));
- return(-1);
+ virXenError(VIR_ERR_XEN_CALL, " locking",
+ sizeof(dom0_getdomaininfo_t));
+ return (-1);
}
op.cmd = DOM0_GETDOMAININFOLIST;
ret = xenHypervisorDoOp(handle, &op);
if (munlock(info, sizeof(dom0_getdomaininfo_t)) < 0) {
- virXenError(VIR_ERR_XEN_CALL, " release", sizeof(dom0_getdomaininfo_t));
+ virXenError(VIR_ERR_XEN_CALL, " release",
+ sizeof(dom0_getdomaininfo_t));
ret = -1;
}
if (ret < 0)
- return(-1);
- return(0);
+ return (-1);
+ return (0);
}
/**
* Returns 0 in case of success, -1 in case of error.
*/
int
-xenHypervisorPauseDomain(int handle, int domain) {
+xenHypervisorPauseDomain(int handle, int domain)
+{
dom0_op_t op;
int ret;
ret = xenHypervisorDoOp(handle, &op);
if (ret < 0)
- return(-1);
- return(0);
+ return (-1);
+ return (0);
}
/**
* Returns 0 in case of success, -1 in case of error.
*/
int
-xenHypervisorResumeDomain(int handle, int domain) {
+xenHypervisorResumeDomain(int handle, int domain)
+{
dom0_op_t op;
int ret;
ret = xenHypervisorDoOp(handle, &op);
if (ret < 0)
- return(-1);
- return(0);
+ return (-1);
+ return (0);
}
/**
* Returns 0 in case of success, -1 in case of error.
*/
int
-xenHypervisorDestroyDomain(int handle, int domain) {
+xenHypervisorDestroyDomain(int handle, int domain)
+{
dom0_op_t op;
int ret;
ret = xenHypervisorDoOp(handle, &op);
if (ret < 0)
- return(-1);
- return(0);
+ return (-1);
+ return (0);
}
/**
* Returns 0 in case of success, -1 in case of error.
*/
int
-xenHypervisorSetMaxMemory(int handle, int domain, unsigned long memory) {
+xenHypervisorSetMaxMemory(int handle, int domain, unsigned long memory)
+{
dom0_op_t op;
int ret;
ret = xenHypervisorDoOp(handle, &op);
if (ret < 0)
- return(-1);
- return(0);
+ return (-1);
+ return (0);
}
/* required for uint8_t, uint32_t, etc ... */
#include <stdint.h>
+
/* required for dom0_getdomaininfo_t */
#include <xen/dom0_ops.h>
extern "C" {
#endif
-int xenHypervisorOpen (int quiet);
-int xenHypervisorClose (int handle);
-unsigned long xenHypervisorGetVersion (int handle);
-int xenHypervisorDestroyDomain (int handle,
- int domain);
-int xenHypervisorResumeDomain (int handle,
- int domain);
-int xenHypervisorPauseDomain (int handle,
- int domain);
-int xenHypervisorGetDomainInfo (int handle,
- int domain,
- dom0_getdomaininfo_t *info);
-int xenHypervisorSetMaxMemory (int handle,
- int domain,
- unsigned long memory);
+ int xenHypervisorOpen(int quiet);
+ int xenHypervisorClose(int handle);
+ unsigned long xenHypervisorGetVersion(int handle);
+ int xenHypervisorDestroyDomain(int handle, int domain);
+ int xenHypervisorResumeDomain(int handle, int domain);
+ int xenHypervisorPauseDomain(int handle, int domain);
+ int xenHypervisorGetDomainInfo(int handle,
+ int domain,
+ dom0_getdomaininfo_t * info);
+ int xenHypervisorSetMaxMemory(int handle,
+ int domain, unsigned long memory);
#ifdef __cplusplus
}
#endif
-#endif /* __VIR_XEN_INTERNAL_H__ */
+#endif /* __VIR_XEN_INTERNAL_H__ */
* Handle an error at the xend daemon interface
*/
static void
-virXendError(virConnectPtr conn, virErrorNumber error, const char *info) {
+virXendError(virConnectPtr conn, virErrorNumber error, const char *info)
+{
const char *errmsg;
-
+
if (error == VIR_ERR_OK)
return;
* Handle an error at the xend daemon interface
*/
static void
-virXendErrorInt(virConnectPtr conn, virErrorNumber error,
- int val) {
+virXendErrorInt(virConnectPtr conn, virErrorNumber error, int val)
+{
const char *errmsg;
-
+
if (error == VIR_ERR_OK)
return;
s = socket(xend->type, SOCK_STREAM, 0);
if (s == -1) {
virXendError(xend, VIR_ERR_INTERNAL_ERROR,
- "failed to create a socket");
+ "failed to create a socket");
return -1;
}
ssize_t len;
if (do_read) {
- len = read(fd, ((char *)buffer) + offset, size - offset);
+ len = read(fd, ((char *) buffer) + offset, size - offset);
} else {
- len = write(fd, ((char *)buffer) + offset, size - offset);
+ len = write(fd, ((char *) buffer) + offset, size - offset);
}
/* recoverable error, retry */
/* unrecoverable error */
if (len == -1) {
- if (do_read)
- virXendError(NULL, VIR_ERR_INTERNAL_ERROR,
- "faid to read from Xen Daemon");
- else
- virXendError(NULL, VIR_ERR_INTERNAL_ERROR,
- "faid to read from Xen Daemon");
-
- return(-1);
+ if (do_read)
+ virXendError(NULL, VIR_ERR_INTERNAL_ERROR,
+ "faid to read from Xen Daemon");
+ else
+ virXendError(NULL, VIR_ERR_INTERNAL_ERROR,
+ "faid to read from Xen Daemon");
+
+ return (-1);
}
offset += len;
size_t offset;
if (n_buffer < 1)
- return(-1);
+ return (-1);
for (offset = 0; offset < (n_buffer - 1); offset++) {
ssize_t ret;
close(s);
if ((ret < 0) || (ret >= 300)) {
- virXendError(NULL, VIR_ERR_GET_FAILED, content);
+ virXendError(NULL, VIR_ERR_GET_FAILED, content);
}
return ret;
close(s);
if ((ret < 0) || (ret >= 300)) {
- virXendError(NULL, VIR_ERR_POST_FAILED, content);
+ virXendError(NULL, VIR_ERR_POST_FAILED, content);
}
return ret;
errno = ESRCH;
break;
default:
- virXendErrorInt(NULL, VIR_ERR_HTTP_ERROR, ret);
+ virXendErrorInt(NULL, VIR_ERR_HTTP_ERROR, ret);
errno = EINVAL;
break;
}
size_t i;
if (buffer == NULL)
- return(NULL);
+ return (NULL);
for (i = 0; i < len; i++) {
switch (string[i]) {
case ' ':
struct sockaddr_un *addr;
if ((xend == NULL) || (path == NULL))
- return(-1);
+ return (-1);
addr = &xend->addr_un;
addr->sun_family = AF_UNIX;
xend->addr = (struct sockaddr *) addr;
xend->type = PF_UNIX;
- return(0);
+ return (0);
}
/**
struct hostent *pent;
if ((xend == NULL) || (host == NULL) || (port <= 0))
- return(-1);
+ return (-1);
pent = gethostbyname(host);
if (pent == NULL) {
if (inet_aton(host, &ip) == 0) {
- virXendError(xend, VIR_ERR_UNKNOWN_HOST, host);
+ virXendError(xend, VIR_ERR_UNKNOWN_HOST, host);
errno = ESRCH;
- return(-1);
+ return (-1);
}
} else {
memcpy(&ip, pent->h_addr_list[0], sizeof(ip));
xend->addr_in.sin_port = htons(port);
memcpy(&xend->addr_in.sin_addr, &ip, sizeof(ip));
- return(0);
+ return (0);
}
/**
int
xend_setup(virConnectPtr conn)
{
- return(xend_setup_tcp(conn, "localhost", 8000));
+ return (xend_setup_tcp(conn, "localhost", 8000));
+
/* return(xend_setup_unix(conn, "/var/lib/xend/xend-socket")); */
}
xend_rename(virConnectPtr xend, const char *old, const char *new)
{
if ((xend == NULL) || (old == NULL) || (new == NULL)) {
- /* this should be caught at the interface but ... */
+ /* this should be caught at the interface but ... */
virXendError(xend, VIR_ERR_INVALID_ARG, __FUNCTION__);
- return(-1);
+ return (-1);
}
return xend_op(xend, old, "op", "rename", "name", new, NULL);
}
xend_reboot(virConnectPtr xend, const char *name)
{
if ((xend == NULL) || (name == NULL)) {
- /* this should be caught at the interface but ... */
+ /* this should be caught at the interface but ... */
virXendError(xend, VIR_ERR_INVALID_ARG, __FUNCTION__);
- return(-1);
+ return (-1);
}
return xend_op(xend, name, "op", "shutdown", "reason", "reboot", NULL);
}
xend_shutdown(virConnectPtr xend, const char *name)
{
if ((xend == NULL) || (name == NULL)) {
- /* this should be caught at the interface but ... */
+ /* this should be caught at the interface but ... */
virXendError(xend, VIR_ERR_INVALID_ARG, __FUNCTION__);
- return(-1);
+ return (-1);
}
- return xend_op(xend, name,
- "op", "shutdown", "reason", "halt", NULL);
+ return xend_op(xend, name, "op", "shutdown", "reason", "halt", NULL);
}
/**
xend_sysrq(virConnectPtr xend, const char *name, const char *key)
{
if ((xend == NULL) || (name == NULL) || (key == NULL)) {
- /* this should be caught at the interface but ... */
+ /* this should be caught at the interface but ... */
virXendError(xend, VIR_ERR_INVALID_ARG, __FUNCTION__);
- return(-1);
+ return (-1);
}
return xend_op(xend, name, "op", "sysrq", "key", key, NULL);
}
xend_destroy(virConnectPtr xend, const char *name)
{
if ((xend == NULL) || (name == NULL)) {
- /* this should be caught at the interface but ... */
+ /* this should be caught at the interface but ... */
virXendError(xend, VIR_ERR_INVALID_ARG, __FUNCTION__);
- return(-1);
+ return (-1);
}
return xend_op(xend, name, "op", "destroy", NULL);
}
xend_save(virConnectPtr xend, const char *name, const char *filename)
{
if ((xend == NULL) || (filename == NULL)) {
- /* this should be caught at the interface but ... */
+ /* this should be caught at the interface but ... */
virXendError(xend, VIR_ERR_INVALID_ARG, __FUNCTION__);
- return(-1);
+ return (-1);
}
return xend_op(xend, name, "op", "save", "file", filename, NULL);
}
xend_restore(virConnectPtr xend, const char *filename)
{
if ((xend == NULL) || (filename == NULL)) {
- /* this should be caught at the interface but ... */
+ /* this should be caught at the interface but ... */
virXendError(xend, VIR_ERR_INVALID_ARG, __FUNCTION__);
- return(-1);
+ return (-1);
}
return xend_op(xend, "", "op", "restore", "file", filename, NULL);
}
size_t i;
if (domain == NULL)
- return(NULL);
+ return (NULL);
lst = sexpr_append(lst, sexpr_string("vm", -1));
lst = sexpr_append_str(lst, "name", domain->name);
ptr = urlencode(sexpr);
if (ptr == NULL) {
- /* this should be caught at the interface but ... */
+ /* this should be caught at the interface but ... */
virXendError(xend, VIR_ERR_INTERNAL_ERROR,
- "Failed to urlencode the create S-Expr");
- return(-1);
+ "Failed to urlencode the create S-Expr");
+ return (-1);
}
ret = xend_op(xend, "", "op", "create", "config", ptr, NULL);
for (_for_i = root, node = root->car; _for_i->kind == SEXPR_CONS;
_for_i = _for_i->cdr, node = _for_i->car) {
- if (sexpr_lookup(node, "device/vbd")) {
- size += sexpr_strlen(node, "device/vbd/dev");
- size += sexpr_strlen(node, "device/vbd/uname");
- (*n_vbds)++;
- }
+ if (sexpr_lookup(node, "device/vbd")) {
+ size += sexpr_strlen(node, "device/vbd/dev");
+ size += sexpr_strlen(node, "device/vbd/uname");
+ (*n_vbds)++;
+ }
}
for (_for_i = root, node = root->car; _for_i->kind == SEXPR_CONS;
_for_i = _for_i->cdr, node = _for_i->car) {
- if (sexpr_lookup(node, "device/vif")) {
- size += sexpr_strlen(node, "device/vif/bridge");
- size += sexpr_strlen(node, "device/vif/ip");
- size += sexpr_strlen(node, "device/vif/script");
- size += sexpr_strlen(node, "device/vif/vifname");
- (*n_vifs)++;
+ if (sexpr_lookup(node, "device/vif")) {
+ size += sexpr_strlen(node, "device/vif/bridge");
+ size += sexpr_strlen(node, "device/vif/ip");
+ size += sexpr_strlen(node, "device/vif/script");
+ size += sexpr_strlen(node, "device/vif/vifname");
+ (*n_vifs)++;
}
}
sexpr_to_xend_domain_info(struct sexpr *root, virDomainInfoPtr info)
{
const char *flags;
-
+
if ((root == NULL) || (info == NULL))
- return(-1);
+ return (-1);
info->memory = sexpr_u64(root, "domain/memory") << 10;
info->maxMem = sexpr_u64(root, "domain/maxmem") << 10;
if (flags) {
if (strchr(flags, 'c'))
- info->state = VIR_DOMAIN_CRASHED;
- else if (strchr(flags, 's'))
- info->state = VIR_DOMAIN_SHUTDOWN;
- else if (strchr(flags, 'd'))
- info->state = VIR_DOMAIN_SHUTOFF;
- else if (strchr(flags, 'p'))
- info->state = VIR_DOMAIN_PAUSED;
- else if (strchr(flags, 'b'))
- info->state = VIR_DOMAIN_BLOCKED;
- else if (strchr(flags, 'r'))
- info->state = VIR_DOMAIN_RUNNING;
+ info->state = VIR_DOMAIN_CRASHED;
+ else if (strchr(flags, 's'))
+ info->state = VIR_DOMAIN_SHUTDOWN;
+ else if (strchr(flags, 'd'))
+ info->state = VIR_DOMAIN_SHUTOFF;
+ else if (strchr(flags, 'p'))
+ info->state = VIR_DOMAIN_PAUSED;
+ else if (strchr(flags, 'b'))
+ info->state = VIR_DOMAIN_BLOCKED;
+ else if (strchr(flags, 'r'))
+ info->state = VIR_DOMAIN_RUNNING;
} else {
info->state = VIR_DOMAIN_NOSTATE;
}
info->cpuTime = sexpr_float(root, "domain/cpu_time") * 1000000000;
info->nrVirtCpu = sexpr_int(root, "domain/vcpus");
- return(0);
+ return (0);
}
/**
i = 0;
for (_for_i = root, node = root->car; _for_i->kind == SEXPR_CONS;
_for_i = _for_i->cdr, node = _for_i->car) {
- if (sexpr_lookup(node, "device/vbd")) {
- dom->vbds[i].dev = sexpr_strcpy(&ptr, node, "device/vbd/dev");
- dom->vbds[i].uname = sexpr_strcpy(&ptr, node, "device/vbd/uname");
- dom->vbds[i].backend = sexpr_int(node, "device/vbd/backend");
- dom->vbds[i].mode = sexpr_mode(node, "device/vbd/mode");
- i++;
- }
+ if (sexpr_lookup(node, "device/vbd")) {
+ dom->vbds[i].dev = sexpr_strcpy(&ptr, node, "device/vbd/dev");
+ dom->vbds[i].uname =
+ sexpr_strcpy(&ptr, node, "device/vbd/uname");
+ dom->vbds[i].backend = sexpr_int(node, "device/vbd/backend");
+ dom->vbds[i].mode = sexpr_mode(node, "device/vbd/mode");
+ i++;
+ }
}
i = 0;
for (_for_i = root, node = root->car; _for_i->kind == SEXPR_CONS;
_for_i = _for_i->cdr, node = _for_i->car) {
- if (sexpr_lookup(node, "device/vif")) {
- dom->vifs[i].backend = sexpr_int(node, "device/vif/backend");
- dom->vifs[i].bridge =
- sexpr_strcpy(&ptr, node, "device/vif/bridge");
- dom->vifs[i].ip = sexpr_strcpy(&ptr, node, "device/vif/ip");
- sexpr_mac(dom->vifs[i].mac, node, "device/vif/mac");
- dom->vifs[i].script =
- sexpr_strcpy(&ptr, node, "device/vif/script");
- dom->vifs[i].vifname =
- sexpr_strcpy(&ptr, node, "device/vif/vifname");
- i++;
+ if (sexpr_lookup(node, "device/vif")) {
+ dom->vifs[i].backend = sexpr_int(node, "device/vif/backend");
+ dom->vifs[i].bridge =
+ sexpr_strcpy(&ptr, node, "device/vif/bridge");
+ dom->vifs[i].ip = sexpr_strcpy(&ptr, node, "device/vif/ip");
+ sexpr_mac(dom->vifs[i].mac, node, "device/vif/mac");
+ dom->vifs[i].script =
+ sexpr_strcpy(&ptr, node, "device/vif/script");
+ dom->vifs[i].vifname =
+ sexpr_strcpy(&ptr, node, "device/vif/vifname");
+ i++;
}
}
-error:
+ error:
return dom;
}
int ret;
if ((domain == NULL) || (info == NULL))
- return(-1);
+ return (-1);
- root = sexpr_get(domain->conn, "/xend/domain/%s?detail=1", domain->name);
+ root =
+ sexpr_get(domain->conn, "/xend/domain/%s?detail=1", domain->name);
if (root == NULL)
- return(-1);
+ return (-1);
ret = sexpr_to_xend_domain_info(root, info);
sexpr_free(root);
- return(ret);
+ return (ret);
}
/**
const char *value;
int ret = -1;
- if (uuid != NULL)
+ if (uuid != NULL)
memset(uuid, 0, 16);
root = sexpr_get(xend, "/xend/domain/%s?detail=1", domname);
if (root == NULL)
value = sexpr_node(root, "domain/domid");
if (value == NULL) {
virXendError(xend, VIR_ERR_INTERNAL_ERROR,
- "domain informations incomplete, missing domid");
+ "domain informations incomplete, missing domid");
goto error;
}
ret = strtol(value, NULL, 0);
if ((ret == 0) && (value[0] != '0')) {
virXendError(xend, VIR_ERR_INTERNAL_ERROR,
- "domain informations incorrect domid not numberic");
+ "domain informations incorrect domid not numberic");
ret = -1;
} else if (uuid != NULL) {
char **ptr = (char **) &uuid;
+
if (sexpr_uuid(ptr, root, "domain/uuid") == NULL) {
- virXendError(xend, VIR_ERR_INTERNAL_ERROR,
- "domain informations incomplete, missing uuid");
- }
+ virXendError(xend, VIR_ERR_INTERNAL_ERROR,
+ "domain informations incomplete, missing uuid");
+ }
}
-error:
+ error:
sexpr_free(root);
- return(ret);
+ return (ret);
}
/**
* the caller must free() the returned value.
*/
static char *
-xend_parse_sexp_desc(struct sexpr *root) {
+xend_parse_sexp_desc(struct sexpr *root)
+{
char *ret;
struct sexpr *cur, *node;
const char *tmp;
if (root == NULL) {
/* ERROR */
- return(NULL);
+ return (NULL);
}
ret = malloc(1000);
if (ret == NULL)
- return(NULL);
+ return (NULL);
buf.content = ret;
buf.size = 1000;
buf.use = 0;
sexpr_int(root, "domain/domid"));
tmp = sexpr_node(root, "domain/name");
if (tmp == NULL) {
- virXendError(NULL, VIR_ERR_INTERNAL_ERROR,
- "domain informations incomplete, missing name");
- goto error;
+ virXendError(NULL, VIR_ERR_INTERNAL_ERROR,
+ "domain informations incomplete, missing name");
+ goto error;
}
virBufferVSprintf(&buf, " <name>%s</name>\n", tmp);
tmp = sexpr_node(root, "domain/image/linux/kernel");
if (tmp == NULL) {
/*
- * TODO: we will need some fallback here for other guest OSes
- */
- virXendError(NULL, VIR_ERR_INTERNAL_ERROR,
- "domain informations incomplete, missing kernel");
- goto error;
+ * TODO: we will need some fallback here for other guest OSes
+ */
+ virXendError(NULL, VIR_ERR_INTERNAL_ERROR,
+ "domain informations incomplete, missing kernel");
+ goto error;
}
virBufferAdd(&buf, " <os>\n", 7);
virBufferVSprintf(&buf, " <type>linux</type>\n");
virBufferVSprintf(&buf, " <kernel>%s</kernel>\n", tmp);
tmp = sexpr_node(root, "domain/image/linux/ramdisk");
if ((tmp != NULL) && (tmp[0] != 0))
- virBufferVSprintf(&buf, " <initrd>%s</initrd>\n", tmp);
+ virBufferVSprintf(&buf, " <initrd>%s</initrd>\n", tmp);
tmp = sexpr_node(root, "domain/image/linux/root");
if ((tmp != NULL) && (tmp[0] != 0))
- virBufferVSprintf(&buf, " <root>%s</root>\n", tmp);
+ virBufferVSprintf(&buf, " <root>%s</root>\n", tmp);
tmp = sexpr_node(root, "domain/image/linux/args");
if ((tmp != NULL) && (tmp[0] != 0))
- virBufferVSprintf(&buf, " <cmdline>%s</cmdline>\n", tmp);
+ virBufferVSprintf(&buf, " <cmdline>%s</cmdline>\n", tmp);
virBufferAdd(&buf, " </os>\n", 8);
- virBufferVSprintf(&buf, " <memory>%d</memory>\n",
+ virBufferVSprintf(&buf, " <memory>%d</memory>\n",
(int) (sexpr_u64(root, "domain/maxmem") << 10));
- virBufferVSprintf(&buf, " <vcpu>%d</vcpu>\n",
+ virBufferVSprintf(&buf, " <vcpu>%d</vcpu>\n",
sexpr_int(root, "domain/vcpus"));
virBufferAdd(&buf, " <devices>\n", 12);
for (cur = root; cur->kind == SEXPR_CONS; cur = cur->cdr) {
node = cur->car;
- if (sexpr_lookup(node, "device/vbd")) {
- tmp = sexpr_node(node, "device/vbd/uname");
- if (tmp == NULL)
- continue;
- if (!memcmp(tmp, "file:", 5)) {
- tmp += 5;
- virBufferVSprintf(&buf, " <disk type='file'>\n");
- virBufferVSprintf(&buf, " <source file='%s'/>\n", tmp);
- tmp = sexpr_node(node, "device/vbd/dev");
- if (tmp == NULL) {
- virXendError(NULL, VIR_ERR_INTERNAL_ERROR,
- "domain informations incomplete, vbd has no dev");
- goto error;
- }
- virBufferVSprintf(&buf, " <target dev='%s'/>\n", tmp);
- tmp = sexpr_node(node, "device/vbd/mode");
- if ((tmp != NULL) && (!strcmp(tmp, "r")))
- virBufferVSprintf(&buf, " <readonly/>\n");
- virBufferAdd(&buf, " </disk>\n", 12);
- } else if (!memcmp(tmp, "phy:", 4)) {
- tmp += 4;
- virBufferVSprintf(&buf, " <disk type='block'>\n");
- virBufferVSprintf(&buf, " <source dev='%s'/>\n", tmp);
- tmp = sexpr_node(node, "device/vbd/dev");
- if (tmp == NULL) {
- virXendError(NULL, VIR_ERR_INTERNAL_ERROR,
- "domain informations incomplete, vbd has no dev");
- goto error;
- }
- virBufferVSprintf(&buf, " <target dev='%s'/>\n", tmp);
- tmp = sexpr_node(node, "device/vbd/mode");
- if ((tmp != NULL) && (!strcmp(tmp, "r")))
- virBufferVSprintf(&buf, " <readonly/>\n");
- virBufferAdd(&buf, " </disk>\n", 12);
- } else {
- char serial[1000];
-
- TODO
- sexpr2string(node, serial, 1000);
- virBufferVSprintf(&buf, "<!-- Failed to parse %s -->\n",
- serial);
- TODO
- }
- } else if (sexpr_lookup(node, "device/vif")) {
- tmp = sexpr_node(node, "device/vif/bridge");
- if (tmp != NULL) {
- virBufferVSprintf(&buf, " <interface type='bridge'>\n");
- virBufferVSprintf(&buf, " <source bridge='%s'/>\n", tmp);
- tmp = sexpr_node(node, "device/vif/vifname");
- if (tmp != NULL)
- virBufferVSprintf(&buf, " <target dev='%s'/>\n", tmp);
- tmp = sexpr_node(node, "device/vif/mac");
- if (tmp != NULL)
- virBufferVSprintf(&buf, " <mac address='%s'/>\n", tmp);
- tmp = sexpr_node(node, "device/vif/ip");
- if (tmp != NULL)
- virBufferVSprintf(&buf, " <ip address='%s'/>\n", tmp);
- tmp = sexpr_node(node, "device/vif/script");
- if (tmp != NULL)
- virBufferVSprintf(&buf, " <script path='%s'/>\n", tmp);
- virBufferAdd(&buf, " </interface>\n", 17);
- } else {
- char serial[1000];
-
- TODO
- sexpr2string(node->car, serial, 1000);
- virBufferVSprintf(&buf, "<!-- Failed to parse %s -->\n",
- serial);
- }
-
- }
+ if (sexpr_lookup(node, "device/vbd")) {
+ tmp = sexpr_node(node, "device/vbd/uname");
+ if (tmp == NULL)
+ continue;
+ if (!memcmp(tmp, "file:", 5)) {
+ tmp += 5;
+ virBufferVSprintf(&buf, " <disk type='file'>\n");
+ virBufferVSprintf(&buf, " <source file='%s'/>\n",
+ tmp);
+ tmp = sexpr_node(node, "device/vbd/dev");
+ if (tmp == NULL) {
+ virXendError(NULL, VIR_ERR_INTERNAL_ERROR,
+ "domain informations incomplete, vbd has no dev");
+ goto error;
+ }
+ virBufferVSprintf(&buf, " <target dev='%s'/>\n", tmp);
+ tmp = sexpr_node(node, "device/vbd/mode");
+ if ((tmp != NULL) && (!strcmp(tmp, "r")))
+ virBufferVSprintf(&buf, " <readonly/>\n");
+ virBufferAdd(&buf, " </disk>\n", 12);
+ } else if (!memcmp(tmp, "phy:", 4)) {
+ tmp += 4;
+ virBufferVSprintf(&buf, " <disk type='block'>\n");
+ virBufferVSprintf(&buf, " <source dev='%s'/>\n", tmp);
+ tmp = sexpr_node(node, "device/vbd/dev");
+ if (tmp == NULL) {
+ virXendError(NULL, VIR_ERR_INTERNAL_ERROR,
+ "domain informations incomplete, vbd has no dev");
+ goto error;
+ }
+ virBufferVSprintf(&buf, " <target dev='%s'/>\n", tmp);
+ tmp = sexpr_node(node, "device/vbd/mode");
+ if ((tmp != NULL) && (!strcmp(tmp, "r")))
+ virBufferVSprintf(&buf, " <readonly/>\n");
+ virBufferAdd(&buf, " </disk>\n", 12);
+ } else {
+ char serial[1000];
+
+ TODO sexpr2string(node, serial, 1000);
+ virBufferVSprintf(&buf, "<!-- Failed to parse %s -->\n",
+ serial);
+ TODO}
+ } else if (sexpr_lookup(node, "device/vif")) {
+ tmp = sexpr_node(node, "device/vif/bridge");
+ if (tmp != NULL) {
+ virBufferVSprintf(&buf, " <interface type='bridge'>\n");
+ virBufferVSprintf(&buf, " <source bridge='%s'/>\n",
+ tmp);
+ tmp = sexpr_node(node, "device/vif/vifname");
+ if (tmp != NULL)
+ virBufferVSprintf(&buf, " <target dev='%s'/>\n",
+ tmp);
+ tmp = sexpr_node(node, "device/vif/mac");
+ if (tmp != NULL)
+ virBufferVSprintf(&buf, " <mac address='%s'/>\n",
+ tmp);
+ tmp = sexpr_node(node, "device/vif/ip");
+ if (tmp != NULL)
+ virBufferVSprintf(&buf, " <ip address='%s'/>\n",
+ tmp);
+ tmp = sexpr_node(node, "device/vif/script");
+ if (tmp != NULL)
+ virBufferVSprintf(&buf, " <script path='%s'/>\n",
+ tmp);
+ virBufferAdd(&buf, " </interface>\n", 17);
+ } else {
+ char serial[1000];
+
+ TODO sexpr2string(node->car, serial, 1000);
+ virBufferVSprintf(&buf, "<!-- Failed to parse %s -->\n",
+ serial);
+ }
+
+ }
}
virBufferAdd(&buf, " </devices>\n", 13);
virBufferAdd(&buf, "</domain>\n", 10);
buf.content[buf.use] = 0;
- return(ret);
+ return (ret);
-error:
+ error:
if (ret != NULL)
free(ret);
- return(NULL);
+ return (NULL);
}
/**
* the caller must free() the returned value.
*/
char *
-xend_get_domain_xml(virDomainPtr domain) {
+xend_get_domain_xml(virDomainPtr domain)
+{
char *ret = NULL;
struct sexpr *root;
if (!VIR_IS_DOMAIN(domain)) {
- /* this should be caught at the interface but ... */
+ /* this should be caught at the interface but ... */
virXendError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
- return(NULL);
+ return (NULL);
}
- root = sexpr_get(domain->conn, "/xend/domain/%s?detail=1", domain->name);
+ root =
+ sexpr_get(domain->conn, "/xend/domain/%s?detail=1", domain->name);
if (root == NULL)
- return(NULL);
+ return (NULL);
ret = xend_parse_sexp_desc(root);
sexpr_free(root);
- return(ret);
+ return (ret);
}
/**
This structure represents a virtual block device.
*/
-struct xend_device_vbd
-{
- /**
+ struct xend_device_vbd {
+
+ /**
The domain ID of the backend.
Required.
*/
- int backend;
+ int backend;
- /**
+ /**
A URI representing the device. This is typically in the form
file:/path/to/image or phy:/dev/device
Required.
*/
- const char *uname;
+ const char *uname;
- /**
+ /**
The name (or number) of the device to expose to the frontend.
Required.
*/
- const char *dev;
+ const char *dev;
- /**
+ /**
A flag specifying the permissions to expose the device with.
Required.
*/
- virDeviceMode mode;
-};
+ virDeviceMode mode;
+ };
/**
This structure represents a range of PIO to enable for a guest.
*/
-struct xend_device_ioport
-{
- /**
+ struct xend_device_ioport {
+
+ /**
The beginning address of an ioport range to enable.
Required.
*/
- uint16_t from;
+ uint16_t from;
- /**
+ /**
The ending address of an ioport range to enable.
Required.
*/
- uint16_t to;
-};
+ uint16_t to;
+ };
/**
This structure represents a virtual network interface configuration.
*/
-struct xend_device_vif
-{
- /**
+ struct xend_device_vif {
+
+ /**
A string representing the domain that will serve as the backend for
this device.
Required.
*/
- int backend;
+ int backend;
- /**
+ /**
The name of the bridge device to pass to the network script.
Optional.
*/
- const char *bridge;
+ const char *bridge;
- /**
+ /**
The ip address to configure the virtal network device with.
Optional.
*/
- const char *ip;
+ const char *ip;
- /**
+ /**
The mac address to use for the virtual network device.
Required.
*/
- uint8_t mac[6];
+ uint8_t mac[6];
- /**
+ /**
The path to the network script that is to be used for initializing
the network device.
Optional.
*/
- const char *script;
+ const char *script;
- /**
+ /**
The name of the vif. The primary use for this is to allow the user
to operate on vifs by name.
Optional.
*/
- const char *vifname;
-};
+ const char *vifname;
+ };
+
+ struct xend_domain_live {
-struct xend_domain_live
-{
- /**
+ /**
true is domain is currently scheduled.
*/
- bool running;
+ bool running;
- /**
+ /**
true is domain has crashed.
*/
- bool crashed;
+ bool crashed;
- /**
+ /**
true if domain has been shutdown.
*/
- bool poweroff;
+ bool poweroff;
- /**
+ /**
true if domain has requested a reboot.
*/
- bool reboot;
+ bool reboot;
- /**
+ /**
true if domain has requested a suspend.
*/
- bool suspend;
+ bool suspend;
- /**
+ /**
true if domain is blocked on IO
*/
- bool blocked;
+ bool blocked;
- /**
+ /**
true if domain has been destroyed but resources are not
fully deallocated.
*/
- bool dying;
+ bool dying;
- /**
+ /**
true if domain is paused.
*/
- bool paused;
+ bool paused;
- /**
+ /**
the amount of time the domain has been running (in seconds)
*/
- double cpu_time;
+ double cpu_time;
- /**
+ /**
the wall clock time since the domain was created (in seconds)
*/
- double up_time;
+ double up_time;
- /**
+ /**
the time (in seconds since epoch) the domain was created
*/
- double start_time;
+ double start_time;
- /**
+ /**
the number of enabled VCPUs
*/
- int online_vcpus;
+ int online_vcpus;
- /**
+ /**
the total number of available VCPUs
*/
- int vcpu_avail;
+ int vcpu_avail;
- /**
+ /**
the domain id number
*/
- int id;
-};
+ int id;
+ };
/**
This structure represents the configuration of a domain. It's primary
purpose (currently) is for domain creation.
*/
-struct xend_domain
-{
- /**
+ struct xend_domain {
+
+ /**
The name of the domain.
Required.
*/
- const char *name;
+ const char *name;
- /**
+ /**
The amount of memory to assign to the domain before creation.
Required.
*/
- uint64_t memory;
+ uint64_t memory;
- /**
+ /**
The maximum amount of memory that can be given to the domain
while it's running. Please note that a domain can increase its
memory on its own while running up to this value.
Required.
*/
- uint64_t max_memory;
+ uint64_t max_memory;
- /**
+ /**
The uuid to use to identify the domain. This is compatible with
libuuid's uuid_t and should be able to be used interchangably.
Optional.
*/
- unsigned char *uuid;
+ unsigned char *uuid;
- /**
+ /**
The ssidref to assign to the domain.
Optional.
*/
- int ssidref;
+ int ssidref;
- /**
+ /**
The action to perform when the domain powers off.
Optional.
*/
- virDomainRestart on_poweroff;
+ virDomainRestart on_poweroff;
- /**
+ /**
The action to perform when the domain reboots.
Optional.
*/
- virDomainRestart on_reboot;
+ virDomainRestart on_reboot;
- /**
+ /**
The action to perform when the domain crashes.
Optional.
*/
- virDomainRestart on_crash;
+ virDomainRestart on_crash;
- /**
+ /**
The number of VCPUs to assign to the domain.
Required.
*/
- int vcpus;
+ int vcpus;
- /* FIXME cpus */
+ /* FIXME cpus */
- virDomainKernel image;
+ virDomainKernel image;
- /**
+ /**
The number of VBDs pointed to be vbds.
Optional.
*/
- size_t n_vbds;
- struct xend_device_vbd *vbds;
+ size_t n_vbds;
+ struct xend_device_vbd *vbds;
- /**
+ /**
The number of IO port ranges pointed to by ioports.
Optional.
*/
- size_t n_ioports;
- struct xend_device_ioport *ioports;
+ size_t n_ioports;
+ struct xend_device_ioport *ioports;
- /**
+ /**
The number of VIFs pointed to be vifs.
Optional.
*/
- size_t n_vifs;
- struct xend_device_vif *vifs;
+ size_t n_vifs;
+ struct xend_device_vif *vifs;
- /**
+ /**
A pointer to run-time information about the domain.
Only set by xen_get_domain().
*/
- struct xend_domain_live *live;
-};
+ struct xend_domain_live *live;
+ };
+
+ enum xend_node_system {
+ XEND_SYSTEM_LINUX = 1,
+ };
-enum xend_node_system
-{
- XEND_SYSTEM_LINUX = 1,
-};
+ struct xend_node {
-struct xend_node
-{
- /**
+ /**
An enumeration value specifying the host system.
*/
- enum xend_node_system system;
+ enum xend_node_system system;
- /**
+ /**
The DNS host name.
*/
- const char *host;
+ const char *host;
- /**
+ /**
The dom0 kernel release string.
*/
- const char *release;
+ const char *release;
- /**
+ /**
The result of uname -v.
*/
- const char *version;
+ const char *version;
- /**
+ /**
The machine type.
*/
- const char *machine;
+ const char *machine;
- /**
+ /**
The number of physical cpus.
*/
- int nr_cpus;
+ int nr_cpus;
- /**
+ /**
The number of NUMA nodes.
*/
- int nr_nodes;
+ int nr_nodes;
- /**
+ /**
The number of sockets per NUMA node.
*/
- int sockets_per_node;
+ int sockets_per_node;
- /**
+ /**
The number of cores per NUMA socket.
*/
- int cores_per_socket;
+ int cores_per_socket;
- /**
+ /**
The number of hyperthreads per core.
*/
- int threads_per_core;
+ int threads_per_core;
- /**
+ /**
The clock rating (in megahertz) of each core.
*/
- int cpu_mhz;
+ int cpu_mhz;
- /**
+ /**
I honestly don't know what this is.
*/
- const char *hw_caps;
+ const char *hw_caps;
- /**
+ /**
The total memory (in bytes).
*/
- uint64_t total_memory;
+ uint64_t total_memory;
- /**
+ /**
The free memory (in bytes).
*/
- uint64_t free_memory;
+ uint64_t free_memory;
- /**
+ /**
The Xen major version number.
*/
- int xen_major;
+ int xen_major;
- /**
+ /**
The Xen minor version number.
*/
- int xen_minor;
+ int xen_minor;
- /**
+ /**
The Xen extra version number.
*/
- int xen_extra;
+ int xen_extra;
- /**
+ /**
A string descirbing the Xen platform.
*/
- const char *xen_caps;
+ const char *xen_caps;
- /**
+ /**
Dunno.
*/
- const char *platform_params;
+ const char *platform_params;
- /**
+ /**
The build changeset.
*/
- const char *xen_changeset;
+ const char *xen_changeset;
- /**
+ /**
The compiler version.
*/
- const char *cc_compiler;
+ const char *cc_compiler;
- /**
+ /**
The user that compiled this binary.
*/
- const char *cc_compile_by;
+ const char *cc_compile_by;
- /**
+ /**
The system this binary was built on.
*/
- const char *cc_compile_domain;
+ const char *cc_compile_domain;
- /**
+ /**
The date that this binary was built on.
*/
- const char *cc_compile_date;
-};
+ const char *cc_compile_date;
+ };
/**
* \brief Setup the connection to the local Xend instance
*
* Make sure to call xend_cleanup().
*/
-int xend_setup(virConnectPtr conn);
+ int xend_setup(virConnectPtr conn);
/**
* \brief Setup the connection to a xend instance via TCP
*
* Make sure to call xend_cleanup().
*/
-int xend_setup_tcp(virConnectPtr xend, const char *host, int port);
+ int xend_setup_tcp(virConnectPtr xend, const char *host, int port);
/**
* \brief Setup the connection to xend instance via a Unix domain socket
*
* Make sure to call xend_cleanup().
*/
-int xend_setup_unix(virConnectPtr xend, const char *path);
+ int xend_setup_unix(virConnectPtr xend, const char *path);
/**
* \brief Delete a previously allocated Xend instance
* initialized with xend_setup[_{tcp, unix}] is no longer needed
* to free the associated resources.
*/
-void xend_cleanup(virConnectPtr xend);
+ void xend_cleanup(virConnectPtr xend);
/**
* \brief Blocks until a domain's devices are initialized
* invalid filename, the error won't occur until after this function
* returns.
*/
-int xend_wait_for_devices(virConnectPtr xend, const char *name);
+ int xend_wait_for_devices(virConnectPtr xend, const char *name);
/**
* \brief Pause a domain
* This method will make sure that Xen does not schedule the domain
* anymore until after xend_unpause() has been called.
*/
-int xend_pause(virConnectPtr xend, const char *name);
+ int xend_pause(virConnectPtr xend, const char *name);
/**
* \brief Unpause a domain
* This method will allow a paused domain (the result of xen_pause())
* to be scheduled in the future.
*/
-int xend_unpause(virConnectPtr xend, const char *name);
+ int xend_unpause(virConnectPtr xend, const char *name);
/**
* \brief Unpause a domain
*
* This method allows a domain to have its name changed after creation.
*/
-int xend_rename(virConnectPtr xend, const char *oldname, const char *name);
+ int xend_rename(virConnectPtr xend, const char *oldname,
+ const char *name);
/**
* \brief Sends a SYSRQ to a domain
*
* This method simulates the pressing of a SYSRQ sequence.
*/
-int xend_sysrq(virConnectPtr xend, const char *name, const char *key);
+ int xend_sysrq(virConnectPtr xend, const char *name, const char *key);
/**
* \brief Request a domain to reboot
* a request and the domain may ignore it. It will return immediately
* after queuing the request.
*/
-int xend_reboot(virConnectPtr xend, const char *name);
+ int xend_reboot(virConnectPtr xend, const char *name);
/**
* \brief Request a domain to shutdown
* a request and the domain may ignore it. It will return immediately
* after queuing the request.
*/
-int xend_shutdown(virConnectPtr xend, const char *name);
+ int xend_shutdown(virConnectPtr xend, const char *name);
/**
* \brief Destroy a domain
* dying and will go away completely once all of the resources have been
* unmapped (usually from the backend devices).
*/
-int xend_destroy(virConnectPtr xend, const char *name);
+ int xend_destroy(virConnectPtr xend, const char *name);
/**
* \brief Save a domain to the disk
* a file on disk. Use xend_restore() to restore a domain after
* saving.
*/
-int xend_save(virConnectPtr xend, const char *name, const char *filename);
+ int xend_save(virConnectPtr xend, const char *name,
+ const char *filename);
/**
* \brief Restore a domain from the disk
*
* This method will restore a domain saved to disk by xend_save().
*/
-int xend_restore(virConnectPtr xend, const char *filename);
+ int xend_restore(virConnectPtr xend, const char *filename);
/**
* \brief Obtain a list of currently running domains
* This method will return an array of names of currently running
* domains. The memory should be released will a call to free().
*/
-char **xend_get_domains(virConnectPtr xend);
+ char **xend_get_domains(virConnectPtr xend);
/**
* \brief Create a new domain
* domain will be paused after creation and must be unpaused with
* xend_unpause() to begin execution.
*/
-int xend_create(virConnectPtr xend, const struct xend_domain *info);
+ int xend_create(virConnectPtr xend, const struct xend_domain *info);
/**
* \brief Create a new domain
* domain will be paused after creation and must be unpaused with
* xend_unpause() to begin execution.
*/
-int xend_create_sexpr(virConnectPtr xend, const char *sexpr);
+ int xend_create_sexpr(virConnectPtr xend, const char *sexpr);
/**
* \brief Set the maximum memory for a domain
* on its own (although under normal circumstances, memory allocation for a
* domain is only done through xend_set_memory()).
*/
-int xend_set_max_memory(virConnectPtr xend, const char *name, uint64_t value);
+ int xend_set_max_memory(virConnectPtr xend, const char *name,
+ uint64_t value);
/**
* \brief Set the memory allocation for a domain
* There is no safe guard for allocations that are too small so be careful
* when using this function to reduce a domain's memory usage.
*/
-int xend_set_memory(virConnectPtr xend, const char *name, uint64_t value);
+ int xend_set_memory(virConnectPtr xend, const char *name,
+ uint64_t value);
/**
* \brief Create a virtual block device
* rather, one should use xend_wait_for_devices() to block until the device
* has been successfully attached.
*/
-int xend_vbd_create(virConnectPtr xend,
- const char *name,
- const struct xend_device_vbd *vbd);
+ int xend_vbd_create(virConnectPtr xend,
+ const char *name,
+ const struct xend_device_vbd *vbd);
/**
* \brief Destroy a virtual block device
* should use xend_wait_for_devices() to block until the device has been
* successfully detached.
*/
-int xend_vbd_destroy(virConnectPtr xend,
- const char *name,
- const struct xend_device_vbd *vbd);
+ int xend_vbd_destroy(virConnectPtr xend,
+ const char *name,
+ const struct xend_device_vbd *vbd);
/**
* \brief Create a virtual network device
* rather, one should use xend_wait_for_devices() to network until the device
* has been successfully attached.
*/
-int xend_vif_create(virConnectPtr xend,
- const char *name,
- const struct xend_device_vif *vif);
+ int xend_vif_create(virConnectPtr xend,
+ const char *name,
+ const struct xend_device_vif *vif);
/**
* \brief Destroy a virtual network device
* rather, one should use xend_wait_for_devices() to network until the device
* has been successfully detached.
*/
-int xend_vif_destroy(virConnectPtr xend,
- const char *name,
- const struct xend_device_vif *vif);
+ int xend_vif_destroy(virConnectPtr xend,
+ const char *name,
+ const struct xend_device_vif *vif);
/**
* \brief Lookup information about a domain
* it in the form of a struct xend_domain. This should be
* free()'d when no longer needed.
*/
-struct xend_domain *xend_get_domain(virConnectPtr xend,
- const char *name);
+ struct xend_domain *xend_get_domain(virConnectPtr xend,
+ const char *name);
/**
* \brief Lookup the id of a domain
*
* This method looks up the ids of a domain
*/
-int xend_get_domain_ids(virConnectPtr xend,
- const char *name,
- unsigned char *uuid);
+ int xend_get_domain_ids(virConnectPtr xend,
+ const char *name, unsigned char *uuid);
/**
* \brief Get status informations for a domain
* This method looks up information about a domain and update the
* information block provided.
*/
-int xend_get_domain_info(virDomainPtr domain,
- virDomainInfoPtr info);
+ int xend_get_domain_info(virDomainPtr domain, virDomainInfoPtr info);
/**
* \brief Lookup information about the host machine
* This method returns information about the physical host
* machine running Xen.
*/
-struct xend_node *xend_get_node(virConnectPtr xend);
+ struct xend_node *xend_get_node(virConnectPtr xend);
/**
* \brief Shutdown physical host machine
*
* This method shuts down the physical machine running Xen.
*/
-int xend_node_shutdown(virConnectPtr xend);
+ int xend_node_shutdown(virConnectPtr xend);
/**
* \brief Restarts physical host machine
*
* This method restarts the physical machine running Xen.
*/
-int xend_node_restart(virConnectPtr xend);
+ int xend_node_restart(virConnectPtr xend);
/**
* \brief Return hypervisor debugging messages
* This function will place the debugging messages from the
* hypervisor into a buffer with a null terminator.
*/
-int xend_dmesg(virConnectPtr xend,
- char *buffer,
- size_t n_buffer);
+ int xend_dmesg(virConnectPtr xend, char *buffer, size_t n_buffer);
/**
* \brief Clear the hypervisor debugging messages
* This function will clear the debugging message ring queue
* in the hypervisor.
*/
-int xend_dmesg_clear(virConnectPtr xend);
+ int xend_dmesg_clear(virConnectPtr xend);
/**
* \brief Obtain the Xend log messages
* This function will place the Xend debugging messages into
* a buffer with a null terminator.
*/
-int xend_log(virConnectPtr xend,
- char *buffer,
- size_t n_buffer);
+ int xend_log(virConnectPtr xend, char *buffer, size_t n_buffer);
/**
* \brief Provide an XML description of the domain.
*
* Provide an XML description of the domain.
*/
-char *xend_get_domain_xml(virDomainPtr domain);
+ char *xend_get_domain_xml(virDomainPtr domain);
#ifdef __cplusplus
}
#endif
-
#endif
* Handle an error at the xend daemon interface
*/
static void
-virXMLError(virErrorNumber error, const char *info, int value) {
+virXMLError(virErrorNumber error, const char *info, int value)
+{
const char *errmsg;
-
+
if (error == VIR_ERR_OK)
return;
errmsg = __virErrorMsg(error, info);
__virRaiseError(NULL, NULL, VIR_FROM_XML, error, VIR_ERR_ERROR,
- errmsg, info, NULL, value, 0, errmsg, info,
- value);
+ errmsg, info, NULL, value, 0, errmsg, info, value);
}
/**
* Returns the new available space or -1 in case of error
*/
static int
-virBufferGrow(virBufferPtr buf, unsigned int len) {
+virBufferGrow(virBufferPtr buf, unsigned int len)
+{
int size;
char *newbuf;
- if (buf == NULL) return(-1);
- if (len + buf->use < buf->size) return(0);
+ if (buf == NULL)
+ return (-1);
+ if (len + buf->use < buf->size)
+ return (0);
size = buf->use + len + 1000;
newbuf = (char *) realloc(buf->content, size);
if (newbuf == NULL) {
virXMLError(VIR_ERR_NO_MEMORY, "growing buffer", size);
- return(-1);
+ return (-1);
}
buf->content = newbuf;
buf->size = size;
- return(buf->size - buf->use);
+ return (buf->size - buf->use);
}
/**
* Returns 0 successful, -1 in case of internal or API error.
*/
int
-virBufferAdd(virBufferPtr buf, const char *str, int len) {
+virBufferAdd(virBufferPtr buf, const char *str, int len)
+{
unsigned int needSize;
if ((str == NULL) || (buf == NULL)) {
- return -1;
+ return -1;
}
- if (len == 0) return 0;
+ if (len == 0)
+ return 0;
if (len < 0)
len = strlen(str);
needSize = buf->use + len + 2;
- if (needSize > buf->size){
- if (!virBufferGrow(buf, needSize)){
- return(-1);
+ if (needSize > buf->size) {
+ if (!virBufferGrow(buf, needSize)) {
+ return (-1);
}
}
memmove(&buf->content[buf->use], str, len);
buf->use += len;
buf->content[buf->use] = 0;
- return(0);
+ return (0);
}
/**
* Returns 0 successful, -1 in case of internal or API error.
*/
int
-virBufferVSprintf(virBufferPtr buf, const char *format, ...) {
+virBufferVSprintf(virBufferPtr buf, const char *format, ...)
+{
int size, count;
va_list locarg, argptr;
if ((format == NULL) || (buf == NULL)) {
- return(-1);
+ return (-1);
}
size = buf->size - buf->use - 1;
va_start(argptr, format);
va_copy(locarg, argptr);
while (((count = vsnprintf(&buf->content[buf->use], size, format,
locarg)) < 0) || (count >= size - 1)) {
- buf->content[buf->use] = 0;
- va_end(locarg);
- if (virBufferGrow(buf, 1000) < 0) {
- return(-1);
- }
- size = buf->size - buf->use - 1;
- va_copy(locarg, argptr);
+ buf->content[buf->use] = 0;
+ va_end(locarg);
+ if (virBufferGrow(buf, 1000) < 0) {
+ return (-1);
+ }
+ size = buf->size - buf->use - 1;
+ va_copy(locarg, argptr);
}
va_end(locarg);
buf->use += count;
buf->content[buf->use] = 0;
- return(0);
+ return (0);
}
#if 0
+
/*
* This block of function are now implemented by a xend poll in
* xend_internal.c instead of querying the Xen store, code is kept
* for reference of in case Xend may not be available in the future ...
*/
+
/**
* virDomainGetXMLDevice:
* @domain: a domain object
* Returns the new string or NULL in case of error
*/
static char *
-virDomainGetXMLDeviceInfo(virDomainPtr domain, const char *sub,
- long dev, const char *name) {
+virDomainGetXMLDeviceInfo(virDomainPtr domain, const char *sub,
+ long dev, const char *name)
+{
char s[256];
unsigned int len = 0;
* Returns 0 in case of success, -1 in case of failure
*/
static int
-virDomainGetXMLDevice(virDomainPtr domain, virBufferPtr buf, long dev) {
+virDomainGetXMLDevice(virDomainPtr domain, virBufferPtr buf, long dev)
+{
char *type, *val;
type = virDomainGetXMLDeviceInfo(domain, "vbd", dev, "type");
if (type == NULL)
- return(-1);
+ return (-1);
if (!strcmp(type, "file")) {
- virBufferVSprintf(buf, " <disk type='file'>\n");
- val = virDomainGetXMLDeviceInfo(domain, "vbd", dev, "params");
- if (val != NULL) {
- virBufferVSprintf(buf, " <source file='%s'/>\n", val);
- free(val);
- }
- val = virDomainGetXMLDeviceInfo(domain, "vbd", dev, "dev");
- if (val != NULL) {
- virBufferVSprintf(buf, " <target dev='%s'/>\n", val);
- free(val);
- }
- val = virDomainGetXMLDeviceInfo(domain, "vbd", dev, "read-only");
- if (val != NULL) {
- virBufferVSprintf(buf, " <readonly/>\n", val);
- free(val);
- }
- virBufferAdd(buf, " </disk>\n", 12);
+ virBufferVSprintf(buf, " <disk type='file'>\n");
+ val = virDomainGetXMLDeviceInfo(domain, "vbd", dev, "params");
+ if (val != NULL) {
+ virBufferVSprintf(buf, " <source file='%s'/>\n", val);
+ free(val);
+ }
+ val = virDomainGetXMLDeviceInfo(domain, "vbd", dev, "dev");
+ if (val != NULL) {
+ virBufferVSprintf(buf, " <target dev='%s'/>\n", val);
+ free(val);
+ }
+ val = virDomainGetXMLDeviceInfo(domain, "vbd", dev, "read-only");
+ if (val != NULL) {
+ virBufferVSprintf(buf, " <readonly/>\n", val);
+ free(val);
+ }
+ virBufferAdd(buf, " </disk>\n", 12);
} else if (!strcmp(type, "phy")) {
- virBufferVSprintf(buf, " <disk type='device'>\n");
- val = virDomainGetXMLDeviceInfo(domain, "vbd", dev, "params");
- if (val != NULL) {
- virBufferVSprintf(buf, " <source device='%s'/>\n", val);
- free(val);
- }
- val = virDomainGetXMLDeviceInfo(domain, "vbd", dev, "dev");
- if (val != NULL) {
- virBufferVSprintf(buf, " <target dev='%s'/>\n", val);
- free(val);
- }
- val = virDomainGetXMLDeviceInfo(domain, "vbd", dev, "read-only");
- if (val != NULL) {
- virBufferVSprintf(buf, " <readonly/>\n", val);
- free(val);
- }
- virBufferAdd(buf, " </disk>\n", 12);
+ virBufferVSprintf(buf, " <disk type='device'>\n");
+ val = virDomainGetXMLDeviceInfo(domain, "vbd", dev, "params");
+ if (val != NULL) {
+ virBufferVSprintf(buf, " <source device='%s'/>\n", val);
+ free(val);
+ }
+ val = virDomainGetXMLDeviceInfo(domain, "vbd", dev, "dev");
+ if (val != NULL) {
+ virBufferVSprintf(buf, " <target dev='%s'/>\n", val);
+ free(val);
+ }
+ val = virDomainGetXMLDeviceInfo(domain, "vbd", dev, "read-only");
+ if (val != NULL) {
+ virBufferVSprintf(buf, " <readonly/>\n", val);
+ free(val);
+ }
+ virBufferAdd(buf, " </disk>\n", 12);
} else {
- TODO
- fprintf(stderr, "Don't know how to handle device type %s\n", type);
+ TODO fprintf(stderr, "Don't know how to handle device type %s\n",
+ type);
}
free(type);
- return(0);
+ return (0);
}
/**
* Returns 0 in case of success, -1 in case of failure
*/
static int
-virDomainGetXMLDevices(virDomainPtr domain, virBufferPtr buf) {
+virDomainGetXMLDevices(virDomainPtr domain, virBufferPtr buf)
+{
int ret = -1;
unsigned int num, i;
long id;
virConnectPtr conn;
if (!VIR_IS_CONNECTED_DOMAIN(domain))
- return(-1);
-
+ return (-1);
+
conn = domain->conn;
- snprintf(backend, 199, "/local/domain/0/backend/vbd/%d",
+ snprintf(backend, 199, "/local/domain/0/backend/vbd/%d",
virDomainGetID(domain));
backend[199] = 0;
list = xs_directory(conn->xshandle, 0, backend, &num);
if (list == NULL)
goto done;
- for (i = 0;i < num;i++) {
+ for (i = 0; i < num; i++) {
id = strtol(list[i], &endptr, 10);
- if ((endptr == list[i]) || (*endptr != 0)) {
- ret = -1;
- goto done;
- }
- virDomainGetXMLDevice(domain, buf, id);
+ if ((endptr == list[i]) || (*endptr != 0)) {
+ ret = -1;
+ goto done;
+ }
+ virDomainGetXMLDevice(domain, buf, id);
}
-done:
+ done:
if (list != NULL)
free(list);
- return(ret);
+ return (ret);
}
/**
* Returns 0 in case of success, -1 in case of failure
*/
static int
-virDomainGetXMLInterface(virDomainPtr domain, virBufferPtr buf, long dev) {
+virDomainGetXMLInterface(virDomainPtr domain, virBufferPtr buf, long dev)
+{
char *type, *val;
type = virDomainGetXMLDeviceInfo(domain, "vif", dev, "bridge");
if (type == NULL) {
- virBufferVSprintf(buf, " <interface type='default'>\n");
- val = virDomainGetXMLDeviceInfo(domain, "vif", dev, "mac");
- if (val != NULL) {
- virBufferVSprintf(buf, " <mac address='%s'/>\n", val);
- free(val);
- }
- val = virDomainGetXMLDeviceInfo(domain, "vif", dev, "script");
- if (val != NULL) {
- virBufferVSprintf(buf, " <script path='%s'/>\n", val);
- free(val);
- }
- virBufferAdd(buf, " </interface>\n", 17);
+ virBufferVSprintf(buf, " <interface type='default'>\n");
+ val = virDomainGetXMLDeviceInfo(domain, "vif", dev, "mac");
+ if (val != NULL) {
+ virBufferVSprintf(buf, " <mac address='%s'/>\n", val);
+ free(val);
+ }
+ val = virDomainGetXMLDeviceInfo(domain, "vif", dev, "script");
+ if (val != NULL) {
+ virBufferVSprintf(buf, " <script path='%s'/>\n", val);
+ free(val);
+ }
+ virBufferAdd(buf, " </interface>\n", 17);
} else {
- virBufferVSprintf(buf, " <interface type='bridge'>\n");
- virBufferVSprintf(buf, " <source bridge='%s'/>\n", type);
- val = virDomainGetXMLDeviceInfo(domain, "vif", dev, "mac");
- if (val != NULL) {
- virBufferVSprintf(buf, " <mac address='%s'/>\n", val);
- free(val);
- }
- val = virDomainGetXMLDeviceInfo(domain, "vif", dev, "script");
- if (val != NULL) {
- virBufferVSprintf(buf, " <script path='%s'/>\n", val);
- free(val);
- }
- virBufferAdd(buf, " </interface>\n", 17);
+ virBufferVSprintf(buf, " <interface type='bridge'>\n");
+ virBufferVSprintf(buf, " <source bridge='%s'/>\n", type);
+ val = virDomainGetXMLDeviceInfo(domain, "vif", dev, "mac");
+ if (val != NULL) {
+ virBufferVSprintf(buf, " <mac address='%s'/>\n", val);
+ free(val);
+ }
+ val = virDomainGetXMLDeviceInfo(domain, "vif", dev, "script");
+ if (val != NULL) {
+ virBufferVSprintf(buf, " <script path='%s'/>\n", val);
+ free(val);
+ }
+ virBufferAdd(buf, " </interface>\n", 17);
}
free(type);
- return(0);
+ return (0);
}
/**
* Returns 0 in case of success, -1 in case of failure
*/
static int
-virDomainGetXMLInterfaces(virDomainPtr domain, virBufferPtr buf) {
+virDomainGetXMLInterfaces(virDomainPtr domain, virBufferPtr buf)
+{
int ret = -1;
unsigned int num, i;
long id;
virConnectPtr conn;
if (!VIR_IS_CONNECTED_DOMAIN(domain))
- return(-1);
-
+ return (-1);
+
conn = domain->conn;
- snprintf(backend, 199, "/local/domain/0/backend/vif/%d",
+ snprintf(backend, 199, "/local/domain/0/backend/vif/%d",
virDomainGetID(domain));
backend[199] = 0;
list = xs_directory(conn->xshandle, 0, backend, &num);
if (list == NULL)
goto done;
- for (i = 0;i < num;i++) {
+ for (i = 0; i < num; i++) {
id = strtol(list[i], &endptr, 10);
- if ((endptr == list[i]) || (*endptr != 0)) {
- ret = -1;
- goto done;
- }
- virDomainGetXMLInterface(domain, buf, id);
+ if ((endptr == list[i]) || (*endptr != 0)) {
+ ret = -1;
+ goto done;
+ }
+ virDomainGetXMLInterface(domain, buf, id);
}
-done:
+ done:
if (list != NULL)
free(list);
- return(ret);
+ return (ret);
}
* Returns 0 in case of success, -1 in case of failure
*/
static int
-virDomainGetXMLBoot(virDomainPtr domain, virBufferPtr buf) {
+virDomainGetXMLBoot(virDomainPtr domain, virBufferPtr buf)
+{
char *vm, *str;
if (!VIR_IS_DOMAIN(domain))
- return(-1);
-
+ return (-1);
+
vm = virDomainGetVM(domain);
if (vm == NULL)
- return(-1);
+ return (-1);
virBufferAdd(buf, " <os>\n", 7);
str = virDomainGetVMInfo(domain, vm, "image/ostype");
}
str = virDomainGetVMInfo(domain, vm, "image/ramdisk");
if (str != NULL) {
- if (str[0] != 0)
- virBufferVSprintf(buf, " <initrd>%s</initrd>\n", str);
+ if (str[0] != 0)
+ virBufferVSprintf(buf, " <initrd>%s</initrd>\n", str);
free(str);
}
str = virDomainGetVMInfo(domain, vm, "image/cmdline");
if (str != NULL) {
- if (str[0] != 0)
- virBufferVSprintf(buf, " <cmdline>%s</cmdline>\n", str);
+ if (str[0] != 0)
+ virBufferVSprintf(buf, " <cmdline>%s</cmdline>\n", str);
free(str);
}
virBufferAdd(buf, " </os>\n", 8);
free(vm);
- return(0);
+ return (0);
}
/**
* the caller must free() the returned value.
*/
char *
-virDomainGetXMLDesc(virDomainPtr domain, int flags) {
+virDomainGetXMLDesc(virDomainPtr domain, int flags)
+{
char *ret = NULL;
virBuffer buf;
virDomainInfo info;
if (!VIR_IS_DOMAIN(domain))
- return(NULL);
+ return (NULL);
if (flags != 0)
- return(NULL);
+ return (NULL);
if (virDomainGetInfo(domain, &info) < 0)
- return(NULL);
+ return (NULL);
ret = malloc(1000);
if (ret == NULL)
- return(NULL);
+ return (NULL);
buf.content = ret;
buf.size = 1000;
buf.use = 0;
virBufferVSprintf(&buf, "<domain type='xen' id='%d'>\n",
virDomainGetID(domain));
- virBufferVSprintf(&buf, " <name>%s</name>\n", virDomainGetName(domain));
+ virBufferVSprintf(&buf, " <name>%s</name>\n",
+ virDomainGetName(domain));
virDomainGetXMLBoot(domain, &buf);
virBufferVSprintf(&buf, " <memory>%lu</memory>\n", info.maxMem);
virBufferVSprintf(&buf, " <vcpu>%d</vcpu>\n", (int) info.nrVirtCpu);
virDomainGetXMLInterfaces(domain, &buf);
virBufferAdd(&buf, " </devices>\n", 13);
virBufferAdd(&buf, "</domain>\n", 10);
-
+
buf.content[buf.use] = 0;
- return(ret);
+ return (ret);
}
#endif
* Returns 0 in case of success, -1 in case of error.
*/
static int
-virDomainParseXMLOSDesc(xmlNodePtr node, virBufferPtr buf) {
+virDomainParseXMLOSDesc(xmlNodePtr node, virBufferPtr buf)
+{
xmlNodePtr cur, txt;
const xmlChar *type = NULL;
const xmlChar *root = NULL;
cur = node->children;
while (cur != NULL) {
if (cur->type == XML_ELEMENT_NODE) {
- if ((type == NULL) && (xmlStrEqual(cur->name, BAD_CAST "type"))) {
- txt = cur->children;
- if ((txt->type == XML_TEXT_NODE) && (txt->next == NULL))
- type = txt->content;
- } else if ((kernel == NULL) &&
- (xmlStrEqual(cur->name, BAD_CAST "kernel"))) {
- txt = cur->children;
- if ((txt->type == XML_TEXT_NODE) && (txt->next == NULL))
- kernel = txt->content;
- } else if ((root == NULL) &&
- (xmlStrEqual(cur->name, BAD_CAST "root"))) {
- txt = cur->children;
- if ((txt->type == XML_TEXT_NODE) && (txt->next == NULL))
- root = txt->content;
- } else if ((initrd == NULL) &&
- (xmlStrEqual(cur->name, BAD_CAST "initrd"))) {
- txt = cur->children;
- if ((txt->type == XML_TEXT_NODE) && (txt->next == NULL))
- initrd = txt->content;
- } else if ((cmdline == NULL) &&
- (xmlStrEqual(cur->name, BAD_CAST "cmdline"))) {
- txt = cur->children;
- if ((txt->type == XML_TEXT_NODE) && (txt->next == NULL))
- cmdline = txt->content;
- }
- }
+ if ((type == NULL)
+ && (xmlStrEqual(cur->name, BAD_CAST "type"))) {
+ txt = cur->children;
+ if ((txt->type == XML_TEXT_NODE) && (txt->next == NULL))
+ type = txt->content;
+ } else if ((kernel == NULL) &&
+ (xmlStrEqual(cur->name, BAD_CAST "kernel"))) {
+ txt = cur->children;
+ if ((txt->type == XML_TEXT_NODE) && (txt->next == NULL))
+ kernel = txt->content;
+ } else if ((root == NULL) &&
+ (xmlStrEqual(cur->name, BAD_CAST "root"))) {
+ txt = cur->children;
+ if ((txt->type == XML_TEXT_NODE) && (txt->next == NULL))
+ root = txt->content;
+ } else if ((initrd == NULL) &&
+ (xmlStrEqual(cur->name, BAD_CAST "initrd"))) {
+ txt = cur->children;
+ if ((txt->type == XML_TEXT_NODE) && (txt->next == NULL))
+ initrd = txt->content;
+ } else if ((cmdline == NULL) &&
+ (xmlStrEqual(cur->name, BAD_CAST "cmdline"))) {
+ txt = cur->children;
+ if ((txt->type == XML_TEXT_NODE) && (txt->next == NULL))
+ cmdline = txt->content;
+ }
+ }
cur = cur->next;
}
if ((type != NULL) && (!xmlStrEqual(type, BAD_CAST "linux"))) {
/* VIR_ERR_OS_TYPE */
- virXMLError(VIR_ERR_OS_TYPE, (const char *) type, 0);
- return(-1);
+ virXMLError(VIR_ERR_OS_TYPE, (const char *) type, 0);
+ return (-1);
}
virBufferAdd(buf, "(linux ", 7);
if (kernel == NULL) {
- virXMLError(VIR_ERR_NO_KERNEL, NULL, 0);
- return(-1);
+ virXMLError(VIR_ERR_NO_KERNEL, NULL, 0);
+ return (-1);
}
virBufferVSprintf(buf, "(kernel '%s')", (const char *) kernel);
if (initrd != NULL)
- virBufferVSprintf(buf, "(ramdisk '%s')", (const char *) initrd);
+ virBufferVSprintf(buf, "(ramdisk '%s')", (const char *) initrd);
if (root == NULL) {
- const xmlChar *base, *tmp;
+ const xmlChar *base, *tmp;
+
/* need to extract root info from command line */
- if (cmdline == NULL) {
- virXMLError(VIR_ERR_NO_ROOT, (const char *) cmdline, 0);
- return(-1);
- }
- base = cmdline;
- while (*base != 0) {
- if ((base[0] == 'r') && (base[1] == 'o') && (base[2] == 'o') &&
- (base[3] == 't')) {
- base += 4;
- break;
- }
- base++;
- }
- while ((*base == ' ') || (*base == '\t')) base++;
- if (*base == '=') {
- base++;
- while ((*base == ' ') || (*base == '\t')) base++;
- }
- tmp = base;
- while ((*tmp != 0) && (*tmp != ' ') && (*tmp != '\t')) tmp++;
- if (tmp == base) {
- virXMLError(VIR_ERR_NO_ROOT, (const char *) cmdline, 0);
- return(-1);
- }
- root = xmlStrndup(base, tmp - base);
+ if (cmdline == NULL) {
+ virXMLError(VIR_ERR_NO_ROOT, (const char *) cmdline, 0);
+ return (-1);
+ }
+ base = cmdline;
+ while (*base != 0) {
+ if ((base[0] == 'r') && (base[1] == 'o') && (base[2] == 'o') &&
+ (base[3] == 't')) {
+ base += 4;
+ break;
+ }
+ base++;
+ }
+ while ((*base == ' ') || (*base == '\t'))
+ base++;
+ if (*base == '=') {
+ base++;
+ while ((*base == ' ') || (*base == '\t'))
+ base++;
+ }
+ tmp = base;
+ while ((*tmp != 0) && (*tmp != ' ') && (*tmp != '\t'))
+ tmp++;
+ if (tmp == base) {
+ virXMLError(VIR_ERR_NO_ROOT, (const char *) cmdline, 0);
+ return (-1);
+ }
+ root = xmlStrndup(base, tmp - base);
virBufferVSprintf(buf, "(root '%s')", (const char *) root);
- xmlFree((xmlChar *) root);
- virBufferVSprintf(buf, "(args '%s')", (const char *) cmdline);
+ xmlFree((xmlChar *) root);
+ virBufferVSprintf(buf, "(args '%s')", (const char *) cmdline);
} else {
virBufferVSprintf(buf, "(root '%s')", (const char *) root);
- if (cmdline != NULL)
- virBufferVSprintf(buf, "(args '%s')", (const char *) cmdline);
+ if (cmdline != NULL)
+ virBufferVSprintf(buf, "(args '%s')", (const char *) cmdline);
}
virBufferAdd(buf, ")", 1);
- return(0);
+ return (0);
}
/**
* Returns 0 in case of success, -1 in case of error.
*/
static int
-virDomainParseXMLDiskDesc(xmlNodePtr node, virBufferPtr buf) {
+virDomainParseXMLDiskDesc(xmlNodePtr node, virBufferPtr buf)
+{
xmlNodePtr cur;
xmlChar *type = NULL;
xmlChar *source = NULL;
type = xmlGetProp(node, BAD_CAST "type");
if (type != NULL) {
- if (xmlStrEqual(type, BAD_CAST "file")) typ = 0;
- else if (xmlStrEqual(type, BAD_CAST "block")) typ = 1;
- xmlFree(type);
+ if (xmlStrEqual(type, BAD_CAST "file"))
+ typ = 0;
+ else if (xmlStrEqual(type, BAD_CAST "block"))
+ typ = 1;
+ xmlFree(type);
}
cur = node->children;
while (cur != NULL) {
if (cur->type == XML_ELEMENT_NODE) {
- if ((source == NULL) &&
- (xmlStrEqual(cur->name, BAD_CAST "source"))) {
-
- if (typ == 0)
- source = xmlGetProp(cur, BAD_CAST "file");
- else
- source = xmlGetProp(cur, BAD_CAST "dev");
- } else if ((target == NULL) &&
- (xmlStrEqual(cur->name, BAD_CAST "target"))) {
- target = xmlGetProp(cur, BAD_CAST "dev");
- } else if (xmlStrEqual(cur->name, BAD_CAST "readonly")) {
- ro = 1;
- }
- }
+ if ((source == NULL) &&
+ (xmlStrEqual(cur->name, BAD_CAST "source"))) {
+
+ if (typ == 0)
+ source = xmlGetProp(cur, BAD_CAST "file");
+ else
+ source = xmlGetProp(cur, BAD_CAST "dev");
+ } else if ((target == NULL) &&
+ (xmlStrEqual(cur->name, BAD_CAST "target"))) {
+ target = xmlGetProp(cur, BAD_CAST "dev");
+ } else if (xmlStrEqual(cur->name, BAD_CAST "readonly")) {
+ ro = 1;
+ }
+ }
cur = cur->next;
}
if (source == NULL) {
- virXMLError(VIR_ERR_NO_SOURCE, (const char *) target, 0);
-
- if (target != NULL)
- xmlFree(target);
- return(-1);
+ virXMLError(VIR_ERR_NO_SOURCE, (const char *) target, 0);
+
+ if (target != NULL)
+ xmlFree(target);
+ return (-1);
}
if (target == NULL) {
- virXMLError(VIR_ERR_NO_TARGET, (const char *) source, 0);
- if (source != NULL)
- xmlFree(source);
- return(-1);
+ virXMLError(VIR_ERR_NO_TARGET, (const char *) source, 0);
+ if (source != NULL)
+ xmlFree(source);
+ return (-1);
}
virBufferAdd(buf, "(vbd ", 5);
if (target[0] == '/')
- virBufferVSprintf(buf, "(dev '%s')", (const char *) target);
+ virBufferVSprintf(buf, "(dev '%s')", (const char *) target);
else
- virBufferVSprintf(buf, "(dev '/dev/%s')", (const char *) target);
+ virBufferVSprintf(buf, "(dev '/dev/%s')", (const char *) target);
if (typ == 0)
virBufferVSprintf(buf, "(uname 'file:%s')", source);
else if (typ == 1) {
if (source[0] == '/')
- virBufferVSprintf(buf, "(uname 'phy:%s')", source);
- else
- virBufferVSprintf(buf, "(uname 'phy:/dev/%s')", source);
+ virBufferVSprintf(buf, "(uname 'phy:%s')", source);
+ else
+ virBufferVSprintf(buf, "(uname 'phy:/dev/%s')", source);
}
if (ro == 0)
virBufferVSprintf(buf, "(mode 'w')");
virBufferAdd(buf, ")", 1);
xmlFree(target);
xmlFree(source);
- return(0);
+ return (0);
}
/**
* Returns 0 in case of success, -1 in case of error.
*/
static int
-virDomainParseXMLIfDesc(xmlNodePtr node, virBufferPtr buf) {
+virDomainParseXMLIfDesc(xmlNodePtr node, virBufferPtr buf)
+{
xmlNodePtr cur;
xmlChar *type = NULL;
xmlChar *source = NULL;
type = xmlGetProp(node, BAD_CAST "type");
if (type != NULL) {
- if (xmlStrEqual(type, BAD_CAST "bridge")) typ = 0;
- else if (xmlStrEqual(type, BAD_CAST "ethernet")) typ = 1;
- xmlFree(type);
+ if (xmlStrEqual(type, BAD_CAST "bridge"))
+ typ = 0;
+ else if (xmlStrEqual(type, BAD_CAST "ethernet"))
+ typ = 1;
+ xmlFree(type);
}
cur = node->children;
while (cur != NULL) {
if (cur->type == XML_ELEMENT_NODE) {
- if ((source == NULL) &&
- (xmlStrEqual(cur->name, BAD_CAST "source"))) {
-
- if (typ == 0)
- source = xmlGetProp(cur, BAD_CAST "bridge");
- else
- source = xmlGetProp(cur, BAD_CAST "dev");
- } else if ((mac == NULL) &&
- (xmlStrEqual(cur->name, BAD_CAST "mac"))) {
- mac = xmlGetProp(cur, BAD_CAST "address");
- } else if ((script == NULL) &&
- (xmlStrEqual(cur->name, BAD_CAST "script"))) {
- script = xmlGetProp(cur, BAD_CAST "path");
- }
- }
+ if ((source == NULL) &&
+ (xmlStrEqual(cur->name, BAD_CAST "source"))) {
+
+ if (typ == 0)
+ source = xmlGetProp(cur, BAD_CAST "bridge");
+ else
+ source = xmlGetProp(cur, BAD_CAST "dev");
+ } else if ((mac == NULL) &&
+ (xmlStrEqual(cur->name, BAD_CAST "mac"))) {
+ mac = xmlGetProp(cur, BAD_CAST "address");
+ } else if ((script == NULL) &&
+ (xmlStrEqual(cur->name, BAD_CAST "script"))) {
+ script = xmlGetProp(cur, BAD_CAST "path");
+ }
+ }
cur = cur->next;
}
virBufferAdd(buf, "(vif ", 5);
if (mac != NULL)
- virBufferVSprintf(buf, "(mac '%s')", (const char *) mac);
+ virBufferVSprintf(buf, "(mac '%s')", (const char *) mac);
if (source != NULL) {
- if (typ == 0)
- virBufferVSprintf(buf, "(bridge '%s')", (const char *) source);
- else /* TODO does that work like that ? */
- virBufferVSprintf(buf, "(dev '%s')", (const char *) source);
+ if (typ == 0)
+ virBufferVSprintf(buf, "(bridge '%s')", (const char *) source);
+ else /* TODO does that work like that ? */
+ virBufferVSprintf(buf, "(dev '%s')", (const char *) source);
}
if (script != NULL)
virBufferVSprintf(buf, "(script '%s')", script);
virBufferAdd(buf, ")", 1);
if (mac != NULL)
- xmlFree(mac);
+ xmlFree(mac);
if (source != NULL)
- xmlFree(source);
+ xmlFree(source);
if (script != NULL)
- xmlFree(script);
- return(0);
+ xmlFree(script);
+ return (0);
}
/**
* the caller must free() the returned value.
*/
char *
-virDomainParseXMLDesc(const char *xmldesc, char **name) {
+virDomainParseXMLDesc(const char *xmldesc, char **name)
+{
xmlDocPtr xml = NULL;
xmlNodePtr node;
char *ret = NULL, *nam = NULL;
int i, res;
if (name != NULL)
- *name = NULL;
+ *name = NULL;
ret = malloc(1000);
if (ret == NULL)
- return(NULL);
+ return (NULL);
buf.content = ret;
buf.size = 1000;
buf.use = 0;
xml = xmlReadDoc((const xmlChar *) xmldesc, "domain.xml", NULL,
- XML_PARSE_NOENT | XML_PARSE_NONET |
- XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
+ XML_PARSE_NOENT | XML_PARSE_NONET |
+ XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
if (xml == NULL) {
goto error;
}
prop = xmlGetProp(node, BAD_CAST "type");
if (prop != NULL) {
if (!xmlStrEqual(prop, BAD_CAST "xen")) {
- xmlFree(prop);
- goto error;
- }
- xmlFree(prop);
+ xmlFree(prop);
+ goto error;
+ }
+ xmlFree(prop);
}
virBufferAdd(&buf, "(vm ", 4);
ctxt = xmlXPathNewContext(xml);
* extract soem of the basics, name, memory, cpus ...
*/
obj = xmlXPathEval(BAD_CAST "string(/domain/name[1])", ctxt);
- if ((obj == NULL) || (obj->type != XPATH_STRING) ||
+ if ((obj == NULL) || (obj->type != XPATH_STRING) ||
(obj->stringval == NULL) || (obj->stringval[0] == 0)) {
- virXMLError(VIR_ERR_NO_NAME, xmldesc, 0);
+ virXMLError(VIR_ERR_NO_NAME, xmldesc, 0);
goto error;
}
virBufferVSprintf(&buf, "(name '%s')", obj->stringval);
nam = strdup((const char *) obj->stringval);
if (nam == NULL) {
- virXMLError(VIR_ERR_NO_MEMORY, "copying name", 0);
- goto error;
+ virXMLError(VIR_ERR_NO_MEMORY, "copying name", 0);
+ goto error;
}
xmlXPathFreeObject(obj);
obj = xmlXPathEval(BAD_CAST "number(/domain/memory[1])", ctxt);
if ((obj == NULL) || (obj->type != XPATH_NUMBER) ||
(obj->floatval < 64000)) {
- virBufferVSprintf(&buf, "(memory 128)(maxmem 128)");
+ virBufferVSprintf(&buf, "(memory 128)(maxmem 128)");
} else {
unsigned long mem = (obj->floatval / 1024);
+
virBufferVSprintf(&buf, "(memory %lu)(maxmem %lu)", mem, mem);
}
xmlXPathFreeObject(obj);
obj = xmlXPathEval(BAD_CAST "number(/domain/vcpu[1])", ctxt);
if ((obj == NULL) || (obj->type != XPATH_NUMBER) ||
(obj->floatval <= 0)) {
- virBufferVSprintf(&buf, "(vcpus 1)");
+ virBufferVSprintf(&buf, "(vcpus 1)");
} else {
unsigned int cpu = (unsigned int) obj->floatval;
+
virBufferVSprintf(&buf, "(vcpus %u)", cpu);
}
xmlXPathFreeObject(obj);
virBufferAdd(&buf, "(image ", 7);
obj = xmlXPathEval(BAD_CAST "/domain/os[1]", ctxt);
if ((obj == NULL) || (obj->type != XPATH_NODESET) ||
- (obj->nodesetval == NULL) ||
- (obj->nodesetval->nodeNr != 1)) {
- virXMLError(VIR_ERR_NO_OS, nam, 0);
+ (obj->nodesetval == NULL) || (obj->nodesetval->nodeNr != 1)) {
+ virXMLError(VIR_ERR_NO_OS, nam, 0);
goto error;
}
res = virDomainParseXMLOSDesc(obj->nodesetval->nodeTab[0], &buf);
/* analyze of the devices */
obj = xmlXPathEval(BAD_CAST "/domain/devices/disk", ctxt);
if ((obj == NULL) || (obj->type != XPATH_NODESET) ||
- (obj->nodesetval == NULL) ||
- (obj->nodesetval->nodeNr < 1)) {
- virXMLError(VIR_ERR_NO_DEVICE, nam, 0);
+ (obj->nodesetval == NULL) || (obj->nodesetval->nodeNr < 1)) {
+ virXMLError(VIR_ERR_NO_DEVICE, nam, 0);
goto error;
}
- for (i = 0;i < obj->nodesetval->nodeNr;i++) {
- virBufferAdd(&buf, "(device ", 8);
- res = virDomainParseXMLDiskDesc(obj->nodesetval->nodeTab[i], &buf);
- if (res != 0) {
- goto error;
- }
- virBufferAdd(&buf, ")", 1);
+ for (i = 0; i < obj->nodesetval->nodeNr; i++) {
+ virBufferAdd(&buf, "(device ", 8);
+ res = virDomainParseXMLDiskDesc(obj->nodesetval->nodeTab[i], &buf);
+ if (res != 0) {
+ goto error;
+ }
+ virBufferAdd(&buf, ")", 1);
}
xmlXPathFreeObject(obj);
obj = xmlXPathEval(BAD_CAST "/domain/devices/interface", ctxt);
if ((obj != NULL) && (obj->type == XPATH_NODESET) &&
(obj->nodesetval != NULL) && (obj->nodesetval->nodeNr >= 0)) {
- for (i = 0;i < obj->nodesetval->nodeNr;i++) {
- virBufferAdd(&buf, "(device ", 8);
- res = virDomainParseXMLIfDesc(obj->nodesetval->nodeTab[i], &buf);
- if (res != 0) {
- goto error;
- }
- virBufferAdd(&buf, ")", 1);
- }
+ for (i = 0; i < obj->nodesetval->nodeNr; i++) {
+ virBufferAdd(&buf, "(device ", 8);
+ res =
+ virDomainParseXMLIfDesc(obj->nodesetval->nodeTab[i], &buf);
+ if (res != 0) {
+ goto error;
+ }
+ virBufferAdd(&buf, ")", 1);
+ }
}
xmlXPathFreeObject(obj);
xmlFreeDoc(xml);
if (name != NULL)
- *name = nam;
-
- return(ret);
+ *name = nam;
-error:
+ return (ret);
+
+ error:
if (nam != NULL)
- free(nam);
+ free(nam);
if (name != NULL)
- *name = NULL;
+ *name = NULL;
if (obj != NULL)
xmlXPathFreeObject(obj);
if (ctxt != NULL)
xmlFreeDoc(xml);
if (ret != NULL)
free(ret);
- return(NULL);
+ return (NULL);
}
-
*
* A buffer structure.
*/
-typedef struct _virBuffer virBuffer;
-typedef virBuffer *virBufferPtr;
-struct _virBuffer {
- char *content; /* The buffer content UTF8 */
- unsigned int use; /* The buffer size used */
- unsigned int size; /* The buffer size */
-};
-
-int virBufferAdd (virBufferPtr buf,
- const char *str,
- int len);
-int virBufferVSprintf (virBufferPtr buf,
- const char *format,
- ...);
-char * virDomainParseXMLDesc (const char *xmldesc,
- char **name);
+ typedef struct _virBuffer virBuffer;
+ typedef virBuffer *virBufferPtr;
+ struct _virBuffer {
+ char *content; /* The buffer content UTF8 */
+ unsigned int use; /* The buffer size used */
+ unsigned int size; /* The buffer size */
+ };
+
+ int virBufferAdd(virBufferPtr buf, const char *str, int len);
+ int virBufferVSprintf(virBufferPtr buf, const char *format, ...);
+ char *virDomainParseXMLDesc(const char *xmldesc, char **name);
#ifdef __cplusplus
}
-#endif /* __cplusplus */
-
-#endif /* __VIR_XML_H__ */
-
+#endif /* __cplusplus */
+#endif /* __VIR_XML_H__ */