* src/hash.c: tiny fix

* src/internal.h: starting to work on reentrancy
* src/libvirt.c: applied patch from Jim Fehlig to fix
  virDomainLookupByID when run as root.
Daniel
This commit is contained in:
Daniel Veillard 2006-04-05 09:31:29 +00:00
parent 48e85b5c41
commit 6bd95bf2a3
4 changed files with 46 additions and 21 deletions

View File

@ -1,3 +1,10 @@
Wed Apr 5 09:32:54 EDT 2006 Daniel Veillard <veillard@redhat.com>
* src/hash.c: tiny fix
* src/internal.h: starting to work on reentrancy
* src/libvirt.c: applied patch from Jim Fehlig to fix
virDomainLookupByID when run as root.
Tue Apr 4 22:49:33 CEST 2006 Karel Zak <kzak@redhat.com> Tue Apr 4 22:49:33 CEST 2006 Karel Zak <kzak@redhat.com>
* src/virsh.c: rename dstate, idof and nameof to domstate, * src/virsh.c: rename dstate, idof and nameof to domstate,

View File

@ -17,8 +17,6 @@
* Author: breese@users.sourceforge.net * Author: breese@users.sourceforge.net
*/ */
#define IN_LIBXML
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include "hash.h" #include "hash.h"

View File

@ -10,6 +10,7 @@
#include <sys/un.h> #include <sys/un.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <netinet/tcp.h> #include <netinet/tcp.h>
#include <libxml/threads.h>
#include "hash.h" #include "hash.h"
#include "libvirt.h" #include "libvirt.h"
@ -114,7 +115,8 @@ struct _virConnect {
void *userData; /* the user data */ void *userData; /* the user data */
/* misc */ /* misc */
virHashTablePtr domains; /* hash table for known domains */ xmlMutexPtr domains_mux;/* a mutex to protect the domain hash table */
virHashTablePtr domains;/* hash table for known domains */
int flags; /* a set of connection flags */ int flags; /* a set of connection flags */
}; };

View File

@ -33,10 +33,11 @@
* TODO: * TODO:
* - use lock to protect against concurrent accesses ? * - use lock to protect against concurrent accesses ?
* - use reference counting to garantee coherent pointer state ? * - use reference counting to garantee coherent pointer state ?
* - error reporting layer
* - memory wrappers for malloc/free ? * - memory wrappers for malloc/free ?
*/ */
static int virDomainFreeName(virDomainPtr domain, const char *name);
static virDriverPtr virDriverTab[MAX_DRIVERS]; static virDriverPtr virDriverTab[MAX_DRIVERS];
static int initialized = 0; static int initialized = 0;
@ -254,6 +255,9 @@ virConnectOpen(const char *name)
ret->domains = virHashCreate(20); ret->domains = virHashCreate(20);
if (ret->domains == NULL) if (ret->domains == NULL)
goto failed; goto failed;
ret->domains_mux = xmlNewMutex();
if (ret->domains_mux == NULL)
goto failed;
ret->flags = 0; ret->flags = 0;
return (ret); return (ret);
@ -264,6 +268,10 @@ failed:
if ((ret->drivers[i] != NULL) && (ret->drivers[i]->close != NULL)) if ((ret->drivers[i] != NULL) && (ret->drivers[i]->close != NULL))
ret->drivers[i]->close(ret); ret->drivers[i]->close(ret);
} }
if (ret->domains != NULL)
virHashFree(ret->domains, (virHashDeallocator) virDomainFreeName);
if (ret->domains_mux != NULL)
xmlFreeMutex(ret->domains_mux);
free(ret); free(ret);
} }
return (NULL); return (NULL);
@ -318,6 +326,9 @@ virConnectOpenReadOnly(const char *name)
ret->domains = virHashCreate(20); ret->domains = virHashCreate(20);
if (ret->domains == NULL) if (ret->domains == NULL)
goto failed; goto failed;
ret->domains_mux = xmlNewMutex();
if (ret->domains_mux == NULL)
goto failed;
ret->flags = VIR_CONNECT_RO; ret->flags = VIR_CONNECT_RO;
return (ret); return (ret);
@ -328,6 +339,10 @@ failed:
if ((ret->drivers[i] != NULL) && (ret->drivers[i]->close != NULL)) if ((ret->drivers[i] != NULL) && (ret->drivers[i]->close != NULL))
ret->drivers[i]->close(ret); ret->drivers[i]->close(ret);
} }
if (ret->domains != NULL)
virHashFree(ret->domains, (virHashDeallocator) virDomainFreeName);
if (ret->domains_mux != NULL)
xmlFreeMutex(ret->domains_mux);
free(ret); free(ret);
} }
return (NULL); return (NULL);
@ -367,6 +382,8 @@ virConnectClose(virConnectPtr conn)
return (-1); return (-1);
virHashFree(conn->domains, (virHashDeallocator) virDomainFreeName); virHashFree(conn->domains, (virHashDeallocator) virDomainFreeName);
conn->domains = NULL; conn->domains = NULL;
xmlFreeMutex(conn->domains_mux);
conn->domains_mux = NULL;
for (i = 0;i < conn->nb_drivers;i++) { for (i = 0;i < conn->nb_drivers;i++) {
if ((conn->drivers[i] != NULL) && (conn->drivers[i]->close != NULL)) if ((conn->drivers[i] != NULL) && (conn->drivers[i]->close != NULL))
conn->drivers[i]->close(conn); conn->drivers[i]->close(conn);
@ -610,6 +627,9 @@ virDomainPtr
virDomainLookupByID(virConnectPtr conn, int id) virDomainLookupByID(virConnectPtr conn, int id)
{ {
char *path = NULL; char *path = NULL;
char **names;
char **tmp;
int ident;
virDomainPtr ret; virDomainPtr ret;
char *name = NULL; char *name = NULL;
unsigned char uuid[16]; unsigned char uuid[16];
@ -623,27 +643,25 @@ virDomainLookupByID(virConnectPtr conn, int id)
return (NULL); return (NULL);
} }
/* lookup is easier with the Xen store so try it first */ /* retrieve home path of the domain */
if (conn->xshandle != NULL) { 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 = xenDaemonListDomains(conn);
char **tmp = names;
int ident;
if (names != NULL) { /* path does not contain name, use xend API to retrieve name */
while (*tmp != NULL) { names = xenDaemonListDomains(conn);
ident = xenDaemonDomainLookupByName_ids(conn, *tmp, &uuid[0]); tmp = names;
if (ident == id) {
name = strdup(*tmp); if (names != NULL) {
break; while (*tmp != NULL) {
} ident = xenDaemonDomainLookupByName_ids(conn, *tmp, &uuid[0]);
tmp++; if (ident == id) {
} name = strdup(*tmp);
free(names); break;
} }
tmp++;
}
free(names);
} }
ret = (virDomainPtr) malloc(sizeof(virDomain)); ret = (virDomainPtr) malloc(sizeof(virDomain));