From 247cf7a3b22a107fa98d0ae18b62e09bab80c9a5 Mon Sep 17 00:00:00 2001 From: Daniel Veillard Date: Wed, 15 Mar 2006 12:13:25 +0000 Subject: [PATCH] * 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. Daniel --- ChangeLog | 8 + src/hash.c | 318 +++++++------ src/hash.h | 39 +- src/internal.h | 104 ++-- src/libvirt.c | 828 +++++++++++++++++--------------- src/sexpr.c | 94 ++-- src/sexpr.h | 25 +- src/virsh.c | 1104 +++++++++++++++++++++++-------------------- src/virterror.c | 344 +++++++------- src/xen_internal.c | 101 ++-- src/xen_internal.h | 29 +- src/xend_internal.c | 410 ++++++++-------- src/xend_internal.h | 402 ++++++++-------- src/xml.c | 635 +++++++++++++------------ src/xml.h | 33 +- 15 files changed, 2320 insertions(+), 2154 deletions(-) diff --git a/ChangeLog b/ChangeLog index c43c02e515..0fcd4ee26d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +Wed Mar 15 13:10:25 CET 2006 Daniel Veillard + + * 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 * configure.in: applied patch for --with-xen-distdir option from diff --git a/src/hash.c b/src/hash.c index 5d992292e0..052f49e0fc 100644 --- a/src/hash.c +++ b/src/hash.c @@ -53,15 +53,17 @@ struct _virHashTable { * 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); } @@ -75,24 +77,25 @@ virHashComputeKey(virHashTablePtr table, const char *name) { * 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); } /** @@ -105,84 +108,87 @@ virHashCreate(int size) { * 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); } /** @@ -194,7 +200,8 @@ virHashGrow(virHashTablePtr table, int size) { * deallocated with @f if provided. */ void -virHashFree(virHashTablePtr table, virHashDeallocator f) { +virHashFree(virHashTablePtr table, virHashDeallocator f) +{ int i; virHashEntryPtr iter; virHashEntryPtr next; @@ -202,30 +209,30 @@ virHashFree(virHashTablePtr table, virHashDeallocator f) { 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); } @@ -242,38 +249,38 @@ virHashFree(virHashTablePtr table, virHashDeallocator f) { * 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); @@ -282,15 +289,15 @@ virHashAddEntry(virHashTablePtr table, const char *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); } /** @@ -308,44 +315,45 @@ virHashAddEntry(virHashTablePtr table, const char *name, */ 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); @@ -356,9 +364,9 @@ virHashUpdateEntry(virHashTablePtr table, const char *name, if (insert != NULL) { - insert->next = entry; + insert->next = entry; } - return(0); + return (0); } /** @@ -371,22 +379,23 @@ virHashUpdateEntry(virHashTablePtr table, const char *name, * 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); } /** @@ -399,10 +408,11 @@ virHashLookup(virHashTablePtr table, const char *name) { * -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); } /** @@ -419,43 +429,45 @@ virHashSize(virHashTablePtr table) { */ 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); } } - diff --git a/src/hash.h b/src/hash.h index fa1534f47e..5077580184 100644 --- a/src/hash.h +++ b/src/hash.h @@ -18,12 +18,13 @@ extern "C" { /* * 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 @@ -31,41 +32,37 @@ typedef virHashTable *virHashTablePtr; * * 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__ */ diff --git a/src/internal.h b/src/internal.h index 1df8c388bf..7993df006b 100644 --- a/src/internal.h +++ b/src/internal.h @@ -74,27 +74,27 @@ extern "C" { * * 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: @@ -102,50 +102,44 @@ struct _virConnect { * 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__ */ diff --git a/src/libvirt.c b/src/libvirt.c index 0bda0b0e7d..236b9514d5 100644 --- a/src/libvirt.c +++ b/src/libvirt.c @@ -42,9 +42,10 @@ * 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; @@ -62,10 +63,12 @@ virLibConnError(virConnectPtr conn, virErrorNumber error, const char *info) { * 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; @@ -93,30 +96,32 @@ virLibDomainError(virDomainPtr domain, virErrorNumber error, const char *info) { * @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); } /** @@ -129,15 +134,16 @@ virGetVersion(unsigned long *libVer, const char *type, unsigned long *typeVer) { * 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); @@ -146,7 +152,7 @@ virConnectOpen(const char *name) { } xshandle = xs_daemon_open(); if (xshandle == NULL) { - virLibConnError(NULL, VIR_ERR_NO_CONNECT, "XenStore"); + virLibConnError(NULL, VIR_ERR_NO_CONNECT, "XenStore"); goto failed; } @@ -166,15 +172,15 @@ virConnectOpen(const char *name) { 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); } /** @@ -188,7 +194,8 @@ failed: * 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; @@ -196,8 +203,8 @@ virConnectOpenReadOnly(const char *name) { /* 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); @@ -227,22 +234,22 @@ virConnectOpenReadOnly(const char *name) { 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); } /** @@ -256,17 +263,18 @@ failed: * 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); } /** @@ -278,8 +286,9 @@ virConnectCheckStoreID(virConnectPtr conn, int id) { * 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)); } /** @@ -294,20 +303,21 @@ virDomainFreeName(virDomainPtr domain, const char *name ATTRIBUTE_UNUSED) { * 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); } /** @@ -319,12 +329,14 @@ virConnectClose(virConnectPtr conn) { * 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"); } /** @@ -341,28 +353,29 @@ virConnectGetType(virConnectPtr conn) { * @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); } /** @@ -376,7 +389,8 @@ virConnectGetVersion(virConnectPtr conn, unsigned long *hvVer) { * 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; @@ -384,45 +398,45 @@ virConnectListDomains(virConnectPtr conn, int *ids, int maxids) { 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); } /** @@ -434,14 +448,15 @@ done: * 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); } /* @@ -453,18 +468,18 @@ virConnectNumOfDomains(virConnectPtr conn) { 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); } /** @@ -480,9 +495,10 @@ virConnectNumOfDomains(virConnectPtr conn) { * 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; @@ -490,52 +506,52 @@ virDomainCreateLinux(virConnectPtr conn, 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); } /** @@ -549,10 +565,12 @@ error: * 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); } @@ -567,14 +585,15 @@ virConnectDoStoreList(virConnectPtr conn, const char *path, unsigned int *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; @@ -595,17 +614,18 @@ virDomainDoStoreQuery(virDomainPtr domain, const char *path) { */ 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; @@ -613,7 +633,7 @@ virDomainDoStoreWrite(virDomainPtr domain, const char *path, if (xs_write(domain->conn->xshandle, 0, &s[0], value, strlen(value))) ret = 0; - return(ret); + return (ret); } /** @@ -630,19 +650,18 @@ virDomainGetVM(virDomainPtr domain) 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); } /** @@ -657,23 +676,23 @@ virDomainGetVM(virDomainPtr domain) * 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); } /** @@ -686,7 +705,8 @@ virDomainGetVMInfo(virDomainPtr domain, const char *vm, * 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; @@ -694,34 +714,34 @@ virDomainLookupByID(virConnectPtr conn, int id) { 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)); @@ -734,7 +754,7 @@ virDomainLookupByID(virConnectPtr conn, int id) { 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); @@ -743,15 +763,15 @@ virDomainLookupByID(virConnectPtr conn, int id) { 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); } /** @@ -764,7 +784,8 @@ error: * 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; @@ -774,39 +795,39 @@ virDomainLookupByUUID(virConnectPtr conn, const unsigned char *uuid) { 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; @@ -816,7 +837,7 @@ virDomainLookupByUUID(virConnectPtr conn, const unsigned char *uuid) { ret->path = 0; memcpy(ret->uuid, uuid, 16); - return(ret); + return (ret); } /** @@ -829,7 +850,8 @@ virDomainLookupByUUID(virConnectPtr conn, const unsigned char *uuid) { * 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; @@ -841,20 +863,20 @@ virDomainLookupByName(virConnectPtr conn, const char *name) { 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 */ @@ -863,49 +885,49 @@ virDomainLookupByName(virConnectPtr conn, const char *name) { 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); } /** @@ -921,12 +943,13 @@ done: * 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); } /* @@ -935,15 +958,15 @@ virDomainDestroy(virDomainPtr domain) { 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); } /** @@ -956,10 +979,11 @@ virDomainDestroy(virDomainPtr domain) { * 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; @@ -968,7 +992,7 @@ virDomainFree(virDomainPtr domain) { if (domain->name) free(domain->name); free(domain); - return(0); + return (0); } /** @@ -984,21 +1008,23 @@ virDomainFree(virDomainPtr domain) { * 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)); } /** @@ -1012,21 +1038,23 @@ virDomainSuspend(virDomainPtr domain) { * 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)); } /** @@ -1042,17 +1070,18 @@ virDomainResume(virDomainPtr domain) { * 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); } /* @@ -1060,23 +1089,23 @@ virDomainSave(virDomainPtr domain, const char *to) { * 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); } /** @@ -1089,17 +1118,18 @@ virDomainSave(virDomainPtr domain, const char *to) { * 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); } /* @@ -1107,22 +1137,22 @@ virDomainRestore(virConnectPtr conn, const char *from) { * 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); } /** @@ -1139,20 +1169,21 @@ virDomainRestore(virConnectPtr conn, const char *from) { * 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 @@ -1162,7 +1193,7 @@ virDomainShutdown(virDomainPtr domain) { if (ret == 0) { domain->flags |= DOMAIN_IS_SHUTDOWN; } - return(ret); + return (ret); } /** @@ -1175,12 +1206,13 @@ virDomainShutdown(virDomainPtr domain) { * 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); } /** @@ -1193,31 +1225,33 @@ virDomainGetName(virDomainPtr domain) { * 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); } /** @@ -1229,12 +1263,13 @@ virDomainGetUUID(virDomainPtr domain, unsigned char *uuid) { * 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); } /** @@ -1246,23 +1281,24 @@ virDomainGetID(virDomainPtr domain) { * 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); } /** @@ -1276,33 +1312,35 @@ virDomainGetOSType(virDomainPtr domain) { * 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); } /** @@ -1318,26 +1356,27 @@ virDomainGetMaxMemory(virDomainPtr domain) { * 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 @@ -1352,7 +1391,7 @@ virDomainSetMaxMemory(virDomainPtr domain, unsigned long memory) { if (!xs_write(domain->conn->xshandle, 0, &s[0], &v[0], strlen(v))) ret = -1; - return(ret); + return (ret); } /** @@ -1367,7 +1406,8 @@ virDomainSetMaxMemory(virDomainPtr domain, unsigned long memory) { * 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; @@ -1376,65 +1416,66 @@ virDomainGetInfo(virDomainPtr domain, virDomainInfoPtr info) { 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 @@ -1442,39 +1483,39 @@ xend_info: 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); } /** @@ -1489,15 +1530,16 @@ xend_info: * 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)); } diff --git a/src/sexpr.c b/src/sexpr.c index 105ed3ec5c..baf034142c 100644 --- a/src/sexpr.c +++ b/src/sexpr.c @@ -30,9 +30,10 @@ * 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; @@ -56,7 +57,7 @@ sexpr_new(void) 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; @@ -193,11 +194,11 @@ struct sexpr * 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); } /** @@ -219,33 +220,34 @@ sexpr2string(struct sexpr * sexpr, char *buffer, size_t n_buffer) 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, ' ')) @@ -254,21 +256,21 @@ sexpr2string(struct sexpr * sexpr, char *buffer, size_t n_buffer) 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)) @@ -278,7 +280,7 @@ trim(const char *string) { while (IS_SPACE(*string)) string++; - return(string); + return (string); } /** @@ -345,9 +347,10 @@ _string2sexpr(const char *buffer, size_t * end) } 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++; @@ -359,23 +362,24 @@ _string2sexpr(const char *buffer, size_t * end) } 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); } /** @@ -415,7 +419,7 @@ sexpr_lookup(struct sexpr *sexpr, const char *node) char buffer[4096], *ptr, *token; if ((node == NULL) || (sexpr == NULL)) - return(NULL); + return (NULL); snprintf(buffer, sizeof(buffer), "%s", node); @@ -437,7 +441,7 @@ sexpr_lookup(struct sexpr *sexpr, const char *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) { diff --git a/src/sexpr.h b/src/sexpr.h index c776b2e5df..3026ee54fb 100644 --- a/src/sexpr.h +++ b/src/sexpr.h @@ -33,24 +33,17 @@ struct sexpr { }; /* 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 diff --git a/src/virsh.c b/src/virsh.c index faeb4d7e52..785f544d7a 100644 --- a/src/virsh.c +++ b/src/virsh.c @@ -11,7 +11,7 @@ * $Id$ */ -#define _GNU_SOURCE /* isblank() */ +#define _GNU_SOURCE /* isblank() */ #include "libvirt.h" #include "virterror.h" @@ -47,10 +47,10 @@ static char *progname; ((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, @@ -61,7 +61,8 @@ typedef enum { * The error handler for virtsh */ static void -virshErrorHandler(void *unused, virErrorPtr error) { +virshErrorHandler(void *unused, virErrorPtr error) +{ if ((unused != NULL) || (error == NULL)) return; @@ -94,20 +95,20 @@ virshErrorHandler(void *unused, virErrorPtr error) { /* * 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; @@ -116,89 +117,94 @@ typedef struct __vshCmd vshCmd; /* * 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 @@ -209,29 +215,30 @@ static int vshConnectionUsability(vshControl *ctl, virConnectPtr conn, int showe * "help" command */ static vshCmdInfo info_help[] = { - { "syntax", "help []" }, - { "help", "print help" }, - { "desc", "Prints global help or command specific help." }, - { "version", "Prints versionning informations." }, - { NULL, NULL } + {"syntax", "help []"}, + {"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); @@ -241,24 +248,27 @@ cmdHelp(vshControl *ctl, vshCmd *cmd) { * "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; @@ -270,7 +280,7 @@ cmdConnect(vshControl *ctl, vshCmd *cmd) { if (!ctl->conn) vshError(ctl, FALSE, "failed to connect to the hypervisor"); - + return ctl->conn ? TRUE : FALSE; } @@ -278,21 +288,22 @@ cmdConnect(vshControl *ctl, vshCmd *cmd) { * "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... */ @@ -301,24 +312,25 @@ cmdList(vshControl *ctl, vshCmd *cmd ATTRIBUTE_UNUSED) { } 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); @@ -329,34 +341,36 @@ cmdList(vshControl *ctl, vshCmd *cmd ATTRIBUTE_UNUSED) { * "dstate" command */ static vshCmdInfo info_dstate[] = { - { "syntax", "dstate " }, - { "help", "domain state" }, - { "desc", "Returns state about a running domain." }, - { NULL, NULL } + {"syntax", "dstate "}, + {"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; } @@ -365,36 +379,37 @@ cmdDstate(vshControl *ctl, vshCmd *cmd) { * "suspend" command */ static vshCmdInfo info_suspend[] = { - { "syntax", "suspend " }, - { "help", "suspend a domain" }, - { "desc", "Suspend a running domain." }, - { NULL, NULL } + {"syntax", "suspend "}, + {"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; } @@ -403,41 +418,42 @@ cmdSuspend(vshControl *ctl, vshCmd *cmd) { * "save" command */ static vshCmdInfo info_save[] = { - { "syntax", "save " }, - { "help", "save a domain state to a file" }, - { "desc", "Save a running domain." }, - { NULL, NULL } + {"syntax", "save "}, + {"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; } @@ -446,31 +462,32 @@ cmdSave(vshControl *ctl, vshCmd *cmd) { * "restore" command */ static vshCmdInfo info_restore[] = { - { "syntax", "restore a domain from " }, - { "help", "restore a domain from a saved state in a file" }, - { "desc", "Restore a domain." }, - { NULL, NULL } + {"syntax", "restore a domain from "}, + {"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"); @@ -483,36 +500,37 @@ cmdRestore(vshControl *ctl, vshCmd *cmd) { * "resume" command */ static vshCmdInfo info_resume[] = { - { "syntax", "resume " }, - { "help", "resume a domain" }, - { "desc", "Resume a previously suspended domain." }, - { NULL, NULL } + {"syntax", "resume "}, + {"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; } @@ -521,36 +539,37 @@ cmdResume(vshControl *ctl, vshCmd *cmd) { * "shutdown" command */ static vshCmdInfo info_shutdown[] = { - { "syntax", "shutdown " }, - { "help", "gracefully shutdown a domain" }, - { "desc", "Run shutdown in the targetted domain" }, - { NULL, NULL } + {"syntax", "shutdown "}, + {"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; } @@ -559,37 +578,38 @@ cmdShutdown(vshControl *ctl, vshCmd *cmd) { * "destroy" command */ static vshCmdInfo info_destroy[] = { - { "syntax", "destroy " }, - { "help", "destroy a domain" }, - { "desc", "Destroy a given domain." }, - { NULL, NULL } + {"syntax", "destroy "}, + {"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; } @@ -597,64 +617,62 @@ cmdDestroy(vshControl *ctl, vshCmd *cmd) { * "dinfo" command */ static vshCmdInfo info_dinfo[] = { - { "syntax", "dinfo " }, - { "help", "domain information" }, - { "desc", "Returns basic information about the domain." }, - { NULL, NULL } + {"syntax", "dinfo "}, + {"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; } @@ -663,29 +681,30 @@ cmdDinfo(vshControl *ctl, vshCmd *cmd) { * "dumpxml" command */ static vshCmdInfo info_dumpxml[] = { - { "syntax", "dumpxml " }, - { "help", "domain information in XML" }, - { "desc", "Ouput the domain informations as an XML dump to stdout" }, - { NULL, NULL } + {"syntax", "dumpxml "}, + {"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); @@ -693,7 +712,7 @@ cmdDumpXML(vshControl *ctl, vshCmd *cmd) { } else { ret = FALSE; } - + virDomainFree(dom); return ret; } @@ -702,18 +721,19 @@ cmdDumpXML(vshControl *ctl, vshCmd *cmd) { * "nameof" command */ static vshCmdInfo info_nameof[] = { - { "syntax", "nameof " }, - { "help", "convert a domain Id to domain name" }, - { NULL, NULL } + {"syntax", "nameof "}, + {"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; @@ -722,7 +742,7 @@ cmdNameof(vshControl *ctl, vshCmd *cmd) { return FALSE; if (!found) return FALSE; - + dom = virDomainLookupByID(ctl->conn, id); if (dom) { vshPrint(ctl, VSH_MESG, "%s\n", virDomainGetName(dom)); @@ -738,18 +758,19 @@ cmdNameof(vshControl *ctl, vshCmd *cmd) { * "idof" command */ static vshCmdInfo info_idof[] = { - { "syntax", "idof " }, - { "help", "convert a domain name to domain Id" }, - { NULL, NULL } + {"syntax", "idof "}, + {"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; @@ -757,7 +778,7 @@ cmdIdof(vshControl *ctl, vshCmd *cmd) { return FALSE; if (!name) return FALSE; - + dom = virDomainLookupByName(ctl->conn, name); if (dom) { vshPrint(ctl, VSH_MESG, "%d\n", virDomainGetID(dom)); @@ -773,15 +794,16 @@ cmdIdof(vshControl *ctl, vshCmd *cmd) { * "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; @@ -794,7 +816,7 @@ cmdVersion(vshControl *ctl, vshCmd *cmd ATTRIBUTE_UNUSED) { if (!vshConnectionUsability(ctl, ctl->conn, TRUE)) return FALSE; - + hvType = virConnectGetType(ctl->conn); if (hvType == NULL) { vshError(ctl, FALSE, "failed to get hypervisor type\n"); @@ -820,7 +842,7 @@ cmdVersion(vshControl *ctl, vshCmd *cmd ATTRIBUTE_UNUSED) { 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; @@ -828,23 +850,22 @@ cmdVersion(vshControl *ctl, vshCmd *cmd ATTRIBUTE_UNUSED) { 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; } @@ -853,13 +874,14 @@ cmdVersion(vshControl *ctl, vshCmd *cmd ATTRIBUTE_UNUSED) { * "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; } @@ -868,23 +890,23 @@ cmdQuit(vshControl *ctl, vshCmd *cmd ATTRIBUTE_UNUSED) { * 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} }; /* --------------- @@ -892,33 +914,36 @@ static vshCmdDef commands[] = { * --------------- */ 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--; @@ -930,63 +955,65 @@ vshCmddefGetData(vshCmdDef *cmd, int 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) @@ -1000,20 +1027,20 @@ vshCmddefHelp(vshControl *ctl, const char *cmdname, int 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 ", opt->name); - else if (opt->type==VSH_OT_STRING) + else if (opt->type == VSH_OT_STRING) snprintf(buf, sizeof(buf), "--%s ", 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); } @@ -1024,12 +1051,14 @@ vshCmddefHelp(vshControl *ctl, const char *cmdname, int withprog) { * 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) @@ -1039,11 +1068,13 @@ vshCommandOptFree(vshCmdOpt *arg) { } static void -vshCommandFree(vshCmd *cmd) { +vshCommandFree(vshCmd * cmd) +{ vshCmd *c = cmd; - while(c) { + while (c) { vshCmd *tmp = c; + c = c->next; if (tmp->opts) @@ -1056,11 +1087,12 @@ vshCommandFree(vshCmd *cmd) { * 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; } @@ -1071,10 +1103,11 @@ vshCommandOpt(vshCmd *cmd, const char *name) { * 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) @@ -1086,8 +1119,10 @@ vshCommandOptInt(vshCmd *cmd, const char *name, int *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; @@ -1098,42 +1133,48 @@ vshCommandOptString(vshCmd *cmd, const char *name, int *found) { * 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; } @@ -1141,26 +1182,28 @@ vshCommandOptDomain(vshControl *ctl, vshCmd *cmd, const char *optname, char **na * 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; } @@ -1177,54 +1220,56 @@ vshCommandRun(vshControl *ctl, vshCmd *cmd) { #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++; @@ -1233,141 +1278,140 @@ vshCommandGetToken(vshControl *ctl, char *str, char **end, char **res) { 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) @@ -1375,17 +1419,17 @@ vshCommandParse(vshControl *ctl, char *cmdstr) { clast = c; } } - + return TRUE; -syntaxError: + syntaxError: if (ctl->cmd) vshCommandFree(ctl->cmd); if (first) vshCommandOptFree(first); if (tkdata) free(tkdata); - return FALSE; + return FALSE; } @@ -1394,10 +1438,11 @@ syntaxError: * --------------- */ 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: @@ -1407,13 +1452,14 @@ vshDomainStateToString(int state) { 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 */ @@ -1426,8 +1472,9 @@ vshConnectionUsability(vshControl *ctl, virConnectPtr conn, int showerror) { } static int -vshWantedDebug(vshOutType type, int mode) { - switch(type) { +vshWantedDebug(vshOutType type, int mode) +{ + switch (type) { case VSH_DEBUG5: if (mode < 5) return FALSE; @@ -1456,35 +1503,37 @@ vshWantedDebug(vshOutType type, int mode) { } 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); @@ -1495,21 +1544,22 @@ vshError(vshControl *ctl, int doexit, const char *format, ...) { * 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"); @@ -1527,7 +1577,8 @@ vshInit(vshControl *ctl) { * (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; @@ -1537,7 +1588,7 @@ vshReadlineCommandGenerator(const char *text, int state) { */ if (!state) { list_index = 0; - len = strlen (text); + len = strlen(text); } /* Return the next name which partially matches from the @@ -1545,7 +1596,7 @@ vshReadlineCommandGenerator(const char *text, int state) { */ while ((name = commands[list_index].name)) { list_index++; - if (strncmp (name, text, len) == 0) + if (strncmp(name, text, len) == 0) return strdup(name); } @@ -1554,7 +1605,8 @@ vshReadlineCommandGenerator(const char *text, int state) { } 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; @@ -1567,32 +1619,33 @@ vshReadlineOptionsGenerator(const char *text, int state) { 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; } @@ -1602,21 +1655,24 @@ vshReadlineOptionsGenerator(const char *text, int state) { } 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"; @@ -1628,23 +1684,26 @@ vshReadlineInit(void) { * 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" @@ -1655,12 +1714,14 @@ vshUsage(vshControl *ctl, const char *cmdname) { " -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 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 for details about the command)\n\n"); return; } if (!vshCmddefHelp(ctl, cmdname, TRUE)) @@ -1672,42 +1733,43 @@ vshUsage(vshControl *ctl, const char *cmdname) { * */ 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; } @@ -1720,10 +1782,10 @@ vshParseArgv(vshControl *ctl, int argc, char **argv) { 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; @@ -1740,7 +1802,8 @@ vshParseArgv(vshControl *ctl, int argc, char **argv) { 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; } } @@ -1749,68 +1812,72 @@ vshParseArgv(vshControl *ctl, int argc, char **argv) { /* 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)) @@ -1818,12 +1885,12 @@ main(int argc, char **argv) { } 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); } @@ -1833,4 +1900,3 @@ main(int argc, char **argv) { * vim: set shiftwidth=4: * vim: set expandtab: */ - diff --git a/src/virterror.c b/src/virterror.c index c3510bcc0d..552cb79644 100644 --- a/src/virterror.c +++ b/src/virterror.c @@ -16,10 +16,10 @@ #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 @@ -68,10 +68,11 @@ static void *virUserData = NULL; /* associated data */ * 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); } /* @@ -85,13 +86,14 @@ virGetLastError(void) { * 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); } /** @@ -101,7 +103,8 @@ virCopyLastError(virErrorPtr to) { * Reset the error being pointed to */ void -virResetError(virErrorPtr err) { +virResetError(virErrorPtr err) +{ if (err == NULL) return; if (err->message != NULL) @@ -121,7 +124,8 @@ virResetError(virErrorPtr err) { * Reset the last error caught at the library level. */ void -virResetLastError(void) { +virResetLastError(void) +{ virResetError(&lastErr); } @@ -136,10 +140,11 @@ virResetLastError(void) { * 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); } /** @@ -154,15 +159,16 @@ virConnGetLastError(virConnectPtr conn) { * 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); } /** @@ -172,7 +178,8 @@ virConnCopyLastError(virConnectPtr conn, virErrorPtr to) { * Reset the last error caught on that connection */ void -virConnResetLastError(virConnectPtr conn) { +virConnResetLastError(virConnectPtr conn) +{ if (conn == NULL) return; virResetError(&conn->err); @@ -188,7 +195,8 @@ virConnResetLastError(virConnectPtr conn) { * 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; } @@ -204,7 +212,9 @@ virSetErrorFunc(void *userData, virErrorFunc handler) { * library handler. */ void -virConnSetErrorFunc(virConnectPtr conn, void *userData, virErrorFunc handler) { +virConnSetErrorFunc(virConnectPtr conn, void *userData, + virErrorFunc handler) +{ if (conn == NULL) return; conn->handler = handler; @@ -218,7 +228,8 @@ virConnSetErrorFunc(virConnectPtr conn, void *userData, virErrorFunc handler) { * Default routine reporting an error to stderr. */ void -virDefaultErrorFunc(virErrorPtr err) { +virDefaultErrorFunc(virErrorPtr err) +{ const char *lvl = "", *dom = "", *domain = ""; int len; @@ -226,39 +237,39 @@ virDefaultErrorFunc(virErrorPtr err) { 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); } /** @@ -283,24 +294,25 @@ 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, ...) { + 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; + } } /* @@ -352,124 +364,124 @@ __virRaiseError(virConnectPtr conn, virDomainPtr dom, * 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); } - diff --git a/src/xen_internal.c b/src/xen_internal.c index 98a6dbd7c3..30d94feb3e 100644 --- a/src/xen_internal.c +++ b/src/xen_internal.c @@ -23,8 +23,7 @@ #include #ifndef __LINUX_PUBLIC_PRIVCMD_H__ -typedef struct hypercall_struct -{ +typedef struct hypercall_struct { unsigned long op; unsigned long arg[5]; } hypercall_t; @@ -45,16 +44,16 @@ typedef struct hypercall_struct * 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); } /** @@ -65,17 +64,19 @@ virXenError(virErrorNumber error, const char *info, int 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); } /** @@ -86,16 +87,18 @@ int xenHypervisorOpen(int quiet) { * * 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); } /** @@ -108,18 +111,19 @@ int xenHypervisorClose(int handle) { * 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)); @@ -134,9 +138,9 @@ xenHypervisorDoOp(int handle, dom0_op_t *op) { } if (ret < 0) - return(-1); - - return(0); + return (-1); + + return (0); } /** @@ -148,13 +152,14 @@ xenHypervisorDoOp(int handle, dom0_op_t *op) { * 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)); @@ -162,13 +167,13 @@ xenHypervisorGetVersion(int handle) { 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); } /** @@ -182,18 +187,21 @@ xenHypervisorGetVersion(int handle) { * 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; @@ -206,13 +214,14 @@ xenHypervisorGetDomainInfo(int handle, int domain, dom0_getdomaininfo_t *info) { 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); } /** @@ -225,7 +234,8 @@ xenHypervisorGetDomainInfo(int handle, int domain, dom0_getdomaininfo_t *info) { * 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; @@ -235,8 +245,8 @@ xenHypervisorPauseDomain(int handle, int domain) { ret = xenHypervisorDoOp(handle, &op); if (ret < 0) - return(-1); - return(0); + return (-1); + return (0); } /** @@ -249,7 +259,8 @@ xenHypervisorPauseDomain(int handle, int domain) { * 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; @@ -259,8 +270,8 @@ xenHypervisorResumeDomain(int handle, int domain) { ret = xenHypervisorDoOp(handle, &op); if (ret < 0) - return(-1); - return(0); + return (-1); + return (0); } /** @@ -273,7 +284,8 @@ xenHypervisorResumeDomain(int handle, int domain) { * 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; @@ -283,8 +295,8 @@ xenHypervisorDestroyDomain(int handle, int domain) { ret = xenHypervisorDoOp(handle, &op); if (ret < 0) - return(-1); - return(0); + return (-1); + return (0); } /** @@ -298,7 +310,8 @@ xenHypervisorDestroyDomain(int handle, int domain) { * 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; @@ -309,6 +322,6 @@ xenHypervisorSetMaxMemory(int handle, int domain, unsigned long memory) { ret = xenHypervisorDoOp(handle, &op); if (ret < 0) - return(-1); - return(0); + return (-1); + return (0); } diff --git a/src/xen_internal.h b/src/xen_internal.h index a819b44148..6ff7d29e02 100644 --- a/src/xen_internal.h +++ b/src/xen_internal.h @@ -13,6 +13,7 @@ /* required for uint8_t, uint32_t, etc ... */ #include + /* required for dom0_getdomaininfo_t */ #include @@ -20,23 +21,19 @@ 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__ */ diff --git a/src/xend_internal.c b/src/xend_internal.c index 1d94e772b0..7c3bc9966b 100644 --- a/src/xend_internal.c +++ b/src/xend_internal.c @@ -67,9 +67,10 @@ struct xend { * 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; @@ -87,10 +88,10 @@ virXendError(virConnectPtr conn, virErrorNumber error, const char *info) { * 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; @@ -126,7 +127,7 @@ do_connect(virConnectPtr xend) 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; } @@ -161,9 +162,9 @@ wr_sync(int fd, void *buffer, size_t size, int do_read) 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 */ @@ -178,14 +179,14 @@ wr_sync(int fd, void *buffer, size_t size, int do_read) /* 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; @@ -257,7 +258,7 @@ sreads(int fd, char *buffer, size_t n_buffer) size_t offset; if (n_buffer < 1) - return(-1); + return (-1); for (offset = 0; offset < (n_buffer - 1); offset++) { ssize_t ret; @@ -364,7 +365,7 @@ xend_get(virConnectPtr xend, const char *path, close(s); if ((ret < 0) || (ret >= 300)) { - virXendError(NULL, VIR_ERR_GET_FAILED, content); + virXendError(NULL, VIR_ERR_GET_FAILED, content); } return ret; @@ -412,7 +413,7 @@ xend_post(virConnectPtr xend, const char *path, const char *ops, close(s); if ((ret < 0) || (ret >= 300)) { - virXendError(NULL, VIR_ERR_POST_FAILED, content); + virXendError(NULL, VIR_ERR_POST_FAILED, content); } return ret; @@ -440,7 +441,7 @@ http2unix(int ret) errno = ESRCH; break; default: - virXendErrorInt(NULL, VIR_ERR_HTTP_ERROR, ret); + virXendErrorInt(NULL, VIR_ERR_HTTP_ERROR, ret); errno = EINVAL; break; } @@ -935,7 +936,7 @@ urlencode(const char *string) size_t i; if (buffer == NULL) - return(NULL); + return (NULL); for (i = 0; i < len; i++) { switch (string[i]) { case ' ': @@ -1027,7 +1028,7 @@ xend_setup_unix(virConnectPtr xend, const char *path) struct sockaddr_un *addr; if ((xend == NULL) || (path == NULL)) - return(-1); + return (-1); addr = &xend->addr_un; addr->sun_family = AF_UNIX; @@ -1041,7 +1042,7 @@ xend_setup_unix(virConnectPtr xend, const char *path) xend->addr = (struct sockaddr *) addr; xend->type = PF_UNIX; - return(0); + return (0); } /** @@ -1062,14 +1063,14 @@ xend_setup_tcp(virConnectPtr xend, const char *host, int port) 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)); @@ -1083,7 +1084,7 @@ xend_setup_tcp(virConnectPtr xend, const char *host, int port) xend->addr_in.sin_port = htons(port); memcpy(&xend->addr_in.sin_addr, &ip, sizeof(ip)); - return(0); + return (0); } /** @@ -1098,7 +1099,8 @@ xend_setup_tcp(virConnectPtr xend, const char *host, int port) 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")); */ } @@ -1173,9 +1175,9 @@ int 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); } @@ -1193,9 +1195,9 @@ int 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); } @@ -1214,12 +1216,11 @@ int 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); } /** @@ -1236,9 +1237,9 @@ int 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); } @@ -1258,9 +1259,9 @@ int 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); } @@ -1283,9 +1284,9 @@ int 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); } @@ -1305,9 +1306,9 @@ int 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); } @@ -1390,7 +1391,7 @@ xend_domain_to_sexpr(const struct xend_domain *domain) 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); @@ -1505,10 +1506,10 @@ xend_create_sexpr(virConnectPtr xend, const char *sexpr) 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); @@ -1750,21 +1751,21 @@ sexpr_to_xend_domain_size(struct sexpr *root, int *n_vbds, int *n_vifs) 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)++; } } @@ -1795,10 +1796,10 @@ static int 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; @@ -1806,23 +1807,23 @@ sexpr_to_xend_domain_info(struct sexpr *root, virDomainInfoPtr info) 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); } /** @@ -1913,33 +1914,34 @@ sexpr_to_xend_domain(struct sexpr *root) 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; } @@ -1960,15 +1962,16 @@ xend_get_domain_info(virDomainPtr domain, virDomainInfoPtr info) 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); } /** @@ -2017,7 +2020,7 @@ xend_get_domain_ids(virConnectPtr xend, const char *domname, 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) @@ -2026,25 +2029,26 @@ xend_get_domain_ids(virConnectPtr xend, const char *domname, 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); } /** @@ -2222,7 +2226,8 @@ xend_log(virConnectPtr xend, char *buffer, size_t n_buffer) * 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; @@ -2230,11 +2235,11 @@ xend_parse_sexp_desc(struct sexpr *root) { 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; @@ -2243,122 +2248,125 @@ xend_parse_sexp_desc(struct sexpr *root) { 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, " %s\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, " \n", 7); virBufferVSprintf(&buf, " linux\n"); virBufferVSprintf(&buf, " %s\n", tmp); tmp = sexpr_node(root, "domain/image/linux/ramdisk"); if ((tmp != NULL) && (tmp[0] != 0)) - virBufferVSprintf(&buf, " %s\n", tmp); + virBufferVSprintf(&buf, " %s\n", tmp); tmp = sexpr_node(root, "domain/image/linux/root"); if ((tmp != NULL) && (tmp[0] != 0)) - virBufferVSprintf(&buf, " %s\n", tmp); + virBufferVSprintf(&buf, " %s\n", tmp); tmp = sexpr_node(root, "domain/image/linux/args"); if ((tmp != NULL) && (tmp[0] != 0)) - virBufferVSprintf(&buf, " %s\n", tmp); + virBufferVSprintf(&buf, " %s\n", tmp); virBufferAdd(&buf, " \n", 8); - virBufferVSprintf(&buf, " %d\n", + virBufferVSprintf(&buf, " %d\n", (int) (sexpr_u64(root, "domain/maxmem") << 10)); - virBufferVSprintf(&buf, " %d\n", + virBufferVSprintf(&buf, " %d\n", sexpr_int(root, "domain/vcpus")); virBufferAdd(&buf, " \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, " \n"); - virBufferVSprintf(&buf, " \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, " \n", tmp); - tmp = sexpr_node(node, "device/vbd/mode"); - if ((tmp != NULL) && (!strcmp(tmp, "r"))) - virBufferVSprintf(&buf, " \n"); - virBufferAdd(&buf, " \n", 12); - } else if (!memcmp(tmp, "phy:", 4)) { - tmp += 4; - virBufferVSprintf(&buf, " \n"); - virBufferVSprintf(&buf, " \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, " \n", tmp); - tmp = sexpr_node(node, "device/vbd/mode"); - if ((tmp != NULL) && (!strcmp(tmp, "r"))) - virBufferVSprintf(&buf, " \n"); - virBufferAdd(&buf, " \n", 12); - } else { - char serial[1000]; - - TODO - sexpr2string(node, serial, 1000); - virBufferVSprintf(&buf, "\n", - serial); - TODO - } - } else if (sexpr_lookup(node, "device/vif")) { - tmp = sexpr_node(node, "device/vif/bridge"); - if (tmp != NULL) { - virBufferVSprintf(&buf, " \n"); - virBufferVSprintf(&buf, " \n", tmp); - tmp = sexpr_node(node, "device/vif/vifname"); - if (tmp != NULL) - virBufferVSprintf(&buf, " \n", tmp); - tmp = sexpr_node(node, "device/vif/mac"); - if (tmp != NULL) - virBufferVSprintf(&buf, " \n", tmp); - tmp = sexpr_node(node, "device/vif/ip"); - if (tmp != NULL) - virBufferVSprintf(&buf, " \n", tmp); - tmp = sexpr_node(node, "device/vif/script"); - if (tmp != NULL) - virBufferVSprintf(&buf, "