提交 04a31e99 编写于 作者: D Daniel P. Berrange

Add locking for thread safety to openvz driver

上级 e8c92c3b
Thu Dec 4 21:43:41 GMT 2008 Daniel P. Berrange <berrange@redhat.com>
* src/openvz_conf.h: Add driver lock
* src/openvz_driver.c: Add locking for thread safety
Thu Dec 4 21:41:41 GMT 2008 Daniel P. Berrange <berrange@redhat.com> Thu Dec 4 21:41:41 GMT 2008 Daniel P. Berrange <berrange@redhat.com>
* src/openvz_driver.c: Merge all return paths from openvz * src/openvz_driver.c: Merge all return paths from openvz
......
...@@ -53,6 +53,8 @@ enum { OPENVZ_WARN, OPENVZ_ERR }; ...@@ -53,6 +53,8 @@ enum { OPENVZ_WARN, OPENVZ_ERR };
#define VZCTL_BRIDGE_MIN_VERSION ((3 * 1000 * 1000) + (0 * 1000) + 22 + 1) #define VZCTL_BRIDGE_MIN_VERSION ((3 * 1000 * 1000) + (0 * 1000) + 22 + 1)
struct openvz_driver { struct openvz_driver {
PTHREAD_MUTEX_T(lock);
virCapsPtr caps; virCapsPtr caps;
virDomainObjList domains; virDomainObjList domains;
int version; int version;
......
...@@ -67,6 +67,16 @@ static int openvzGetMaxVCPUs(virConnectPtr conn, const char *type); ...@@ -67,6 +67,16 @@ static int openvzGetMaxVCPUs(virConnectPtr conn, const char *type);
static int openvzDomainGetMaxVcpus(virDomainPtr dom); static int openvzDomainGetMaxVcpus(virDomainPtr dom);
static int openvzDomainSetVcpus(virDomainPtr dom, unsigned int nvcpus); static int openvzDomainSetVcpus(virDomainPtr dom, unsigned int nvcpus);
static void openvzDriverLock(struct openvz_driver *driver)
{
pthread_mutex_lock(&driver->lock);
}
static void openvzDriverUnlock(struct openvz_driver *driver)
{
pthread_mutex_unlock(&driver->lock);
}
struct openvz_driver ovz_driver; struct openvz_driver ovz_driver;
static void cmdExecFree(const char *cmdExec[]) static void cmdExecFree(const char *cmdExec[])
...@@ -159,7 +169,10 @@ static virDomainPtr openvzDomainLookupByID(virConnectPtr conn, ...@@ -159,7 +169,10 @@ static virDomainPtr openvzDomainLookupByID(virConnectPtr conn,
virDomainObjPtr vm; virDomainObjPtr vm;
virDomainPtr dom = NULL; virDomainPtr dom = NULL;
openvzDriverLock(driver);
vm = virDomainFindByID(&driver->domains, id); vm = virDomainFindByID(&driver->domains, id);
openvzDriverUnlock(driver);
if (!vm) { if (!vm) {
openvzError(conn, VIR_ERR_NO_DOMAIN, NULL); openvzError(conn, VIR_ERR_NO_DOMAIN, NULL);
goto cleanup; goto cleanup;
...@@ -170,12 +183,16 @@ static virDomainPtr openvzDomainLookupByID(virConnectPtr conn, ...@@ -170,12 +183,16 @@ static virDomainPtr openvzDomainLookupByID(virConnectPtr conn,
dom->id = vm->def->id; dom->id = vm->def->id;
cleanup: cleanup:
if (vm)
virDomainObjUnlock(vm);
return dom; return dom;
} }
static int openvzGetVersion(virConnectPtr conn, unsigned long *version) { static int openvzGetVersion(virConnectPtr conn, unsigned long *version) {
struct openvz_driver *driver = conn->privateData; struct openvz_driver *driver = conn->privateData;
openvzDriverLock(driver);
*version = driver->version; *version = driver->version;
openvzDriverUnlock(driver);
return 0; return 0;
} }
...@@ -185,7 +202,10 @@ static char *openvzGetOSType(virDomainPtr dom) ...@@ -185,7 +202,10 @@ static char *openvzGetOSType(virDomainPtr dom)
virDomainObjPtr vm; virDomainObjPtr vm;
char *ret = NULL; char *ret = NULL;
openvzDriverLock(driver);
vm = virDomainFindByUUID(&driver->domains, dom->uuid); vm = virDomainFindByUUID(&driver->domains, dom->uuid);
openvzDriverUnlock(driver);
if (!vm) { if (!vm) {
openvzError(dom->conn, VIR_ERR_NO_DOMAIN, NULL); openvzError(dom->conn, VIR_ERR_NO_DOMAIN, NULL);
goto cleanup; goto cleanup;
...@@ -195,6 +215,8 @@ static char *openvzGetOSType(virDomainPtr dom) ...@@ -195,6 +215,8 @@ static char *openvzGetOSType(virDomainPtr dom)
openvzError(dom->conn, VIR_ERR_NO_MEMORY, NULL); openvzError(dom->conn, VIR_ERR_NO_MEMORY, NULL);
cleanup: cleanup:
if (vm)
virDomainObjUnlock(vm);
return ret; return ret;
} }
...@@ -205,7 +227,10 @@ static virDomainPtr openvzDomainLookupByUUID(virConnectPtr conn, ...@@ -205,7 +227,10 @@ static virDomainPtr openvzDomainLookupByUUID(virConnectPtr conn,
virDomainObjPtr vm; virDomainObjPtr vm;
virDomainPtr dom = NULL; virDomainPtr dom = NULL;
openvzDriverLock(driver);
vm = virDomainFindByUUID(&driver->domains, uuid); vm = virDomainFindByUUID(&driver->domains, uuid);
openvzDriverUnlock(driver);
if (!vm) { if (!vm) {
openvzError(conn, VIR_ERR_NO_DOMAIN, NULL); openvzError(conn, VIR_ERR_NO_DOMAIN, NULL);
goto cleanup; goto cleanup;
...@@ -216,6 +241,8 @@ static virDomainPtr openvzDomainLookupByUUID(virConnectPtr conn, ...@@ -216,6 +241,8 @@ static virDomainPtr openvzDomainLookupByUUID(virConnectPtr conn,
dom->id = vm->def->id; dom->id = vm->def->id;
cleanup: cleanup:
if (vm)
virDomainObjUnlock(vm);
return dom; return dom;
} }
...@@ -225,7 +252,10 @@ static virDomainPtr openvzDomainLookupByName(virConnectPtr conn, ...@@ -225,7 +252,10 @@ static virDomainPtr openvzDomainLookupByName(virConnectPtr conn,
virDomainObjPtr vm; virDomainObjPtr vm;
virDomainPtr dom = NULL; virDomainPtr dom = NULL;
openvzDriverLock(driver);
vm = virDomainFindByName(&driver->domains, name); vm = virDomainFindByName(&driver->domains, name);
openvzDriverUnlock(driver);
if (!vm) { if (!vm) {
openvzError(conn, VIR_ERR_NO_DOMAIN, NULL); openvzError(conn, VIR_ERR_NO_DOMAIN, NULL);
goto cleanup; goto cleanup;
...@@ -236,6 +266,8 @@ static virDomainPtr openvzDomainLookupByName(virConnectPtr conn, ...@@ -236,6 +266,8 @@ static virDomainPtr openvzDomainLookupByName(virConnectPtr conn,
dom->id = vm->def->id; dom->id = vm->def->id;
cleanup: cleanup:
if (vm)
virDomainObjUnlock(vm);
return dom; return dom;
} }
...@@ -245,7 +277,10 @@ static int openvzDomainGetInfo(virDomainPtr dom, ...@@ -245,7 +277,10 @@ static int openvzDomainGetInfo(virDomainPtr dom,
virDomainObjPtr vm; virDomainObjPtr vm;
int ret = -1; int ret = -1;
openvzDriverLock(driver);
vm = virDomainFindByUUID(&driver->domains, dom->uuid); vm = virDomainFindByUUID(&driver->domains, dom->uuid);
openvzDriverUnlock(driver);
if (!vm) { if (!vm) {
openvzError(dom->conn, VIR_ERR_INVALID_DOMAIN, openvzError(dom->conn, VIR_ERR_INVALID_DOMAIN,
"%s", _("no domain with matching uuid")); "%s", _("no domain with matching uuid"));
...@@ -270,6 +305,8 @@ static int openvzDomainGetInfo(virDomainPtr dom, ...@@ -270,6 +305,8 @@ static int openvzDomainGetInfo(virDomainPtr dom,
ret = 0; ret = 0;
cleanup: cleanup:
if (vm)
virDomainObjUnlock(vm);
return ret; return ret;
} }
...@@ -279,7 +316,10 @@ static char *openvzDomainDumpXML(virDomainPtr dom, int flags) { ...@@ -279,7 +316,10 @@ static char *openvzDomainDumpXML(virDomainPtr dom, int flags) {
virDomainObjPtr vm; virDomainObjPtr vm;
char *ret = NULL; char *ret = NULL;
openvzDriverLock(driver);
vm = virDomainFindByUUID(&driver->domains, dom->uuid); vm = virDomainFindByUUID(&driver->domains, dom->uuid);
openvzDriverUnlock(driver);
if (!vm) { if (!vm) {
openvzError(dom->conn, VIR_ERR_INVALID_DOMAIN, openvzError(dom->conn, VIR_ERR_INVALID_DOMAIN,
"%s", _("no domain with matching uuid")); "%s", _("no domain with matching uuid"));
...@@ -289,6 +329,8 @@ static char *openvzDomainDumpXML(virDomainPtr dom, int flags) { ...@@ -289,6 +329,8 @@ static char *openvzDomainDumpXML(virDomainPtr dom, int flags) {
ret = virDomainDefFormat(dom->conn, vm->def, flags); ret = virDomainDefFormat(dom->conn, vm->def, flags);
cleanup: cleanup:
if (vm)
virDomainObjUnlock(vm);
return ret; return ret;
} }
...@@ -318,7 +360,10 @@ static int openvzDomainShutdown(virDomainPtr dom) { ...@@ -318,7 +360,10 @@ static int openvzDomainShutdown(virDomainPtr dom) {
const char *prog[] = {VZCTL, "--quiet", "stop", PROGRAM_SENTINAL, NULL}; const char *prog[] = {VZCTL, "--quiet", "stop", PROGRAM_SENTINAL, NULL};
int ret = -1; int ret = -1;
openvzDriverLock(driver);
vm = virDomainFindByUUID(&driver->domains, dom->uuid); vm = virDomainFindByUUID(&driver->domains, dom->uuid);
openvzDriverUnlock(driver);
if (!vm) { if (!vm) {
openvzError(dom->conn, VIR_ERR_INVALID_DOMAIN, openvzError(dom->conn, VIR_ERR_INVALID_DOMAIN,
"%s", _("no domain with matching uuid")); "%s", _("no domain with matching uuid"));
...@@ -340,6 +385,8 @@ static int openvzDomainShutdown(virDomainPtr dom) { ...@@ -340,6 +385,8 @@ static int openvzDomainShutdown(virDomainPtr dom) {
ret = 0; ret = 0;
cleanup: cleanup:
if (vm)
virDomainObjUnlock(vm);
return ret; return ret;
} }
...@@ -350,7 +397,10 @@ static int openvzDomainReboot(virDomainPtr dom, ...@@ -350,7 +397,10 @@ static int openvzDomainReboot(virDomainPtr dom,
const char *prog[] = {VZCTL, "--quiet", "restart", PROGRAM_SENTINAL, NULL}; const char *prog[] = {VZCTL, "--quiet", "restart", PROGRAM_SENTINAL, NULL};
int ret = -1; int ret = -1;
openvzDriverLock(driver);
vm = virDomainFindByUUID(&driver->domains, dom->uuid); vm = virDomainFindByUUID(&driver->domains, dom->uuid);
openvzDriverUnlock(driver);
if (!vm) { if (!vm) {
openvzError(dom->conn, VIR_ERR_INVALID_DOMAIN, openvzError(dom->conn, VIR_ERR_INVALID_DOMAIN,
"%s", _("no domain with matching uuid")); "%s", _("no domain with matching uuid"));
...@@ -369,6 +419,8 @@ static int openvzDomainReboot(virDomainPtr dom, ...@@ -369,6 +419,8 @@ static int openvzDomainReboot(virDomainPtr dom,
ret = 0; ret = 0;
cleanup: cleanup:
if (vm)
virDomainObjUnlock(vm);
return ret; return ret;
} }
...@@ -599,6 +651,7 @@ openvzDomainDefineXML(virConnectPtr conn, const char *xml) ...@@ -599,6 +651,7 @@ openvzDomainDefineXML(virConnectPtr conn, const char *xml)
const char *prog[OPENVZ_MAX_ARG]; const char *prog[OPENVZ_MAX_ARG];
prog[0] = NULL; prog[0] = NULL;
openvzDriverLock(driver);
if ((vmdef = virDomainDefParseString(conn, driver->caps, xml)) == NULL) if ((vmdef = virDomainDefParseString(conn, driver->caps, xml)) == NULL)
goto cleanup; goto cleanup;
...@@ -656,6 +709,9 @@ openvzDomainDefineXML(virConnectPtr conn, const char *xml) ...@@ -656,6 +709,9 @@ openvzDomainDefineXML(virConnectPtr conn, const char *xml)
cleanup: cleanup:
virDomainDefFree(vmdef); virDomainDefFree(vmdef);
cmdExecFree(prog); cmdExecFree(prog);
if (vm)
virDomainObjUnlock(vm);
openvzDriverUnlock(driver);
return dom; return dom;
} }
...@@ -671,6 +727,7 @@ openvzDomainCreateXML(virConnectPtr conn, const char *xml, ...@@ -671,6 +727,7 @@ openvzDomainCreateXML(virConnectPtr conn, const char *xml,
const char *progcreate[OPENVZ_MAX_ARG]; const char *progcreate[OPENVZ_MAX_ARG];
progcreate[0] = NULL; progcreate[0] = NULL;
openvzDriverLock(driver);
if ((vmdef = virDomainDefParseString(conn, driver->caps, xml)) == NULL) if ((vmdef = virDomainDefParseString(conn, driver->caps, xml)) == NULL)
goto cleanup; goto cleanup;
...@@ -737,6 +794,9 @@ openvzDomainCreateXML(virConnectPtr conn, const char *xml, ...@@ -737,6 +794,9 @@ openvzDomainCreateXML(virConnectPtr conn, const char *xml,
cleanup: cleanup:
virDomainDefFree(vmdef); virDomainDefFree(vmdef);
cmdExecFree(progcreate); cmdExecFree(progcreate);
if (vm)
virDomainObjUnlock(vm);
openvzDriverUnlock(driver);
return dom; return dom;
} }
...@@ -748,7 +808,10 @@ openvzDomainCreate(virDomainPtr dom) ...@@ -748,7 +808,10 @@ openvzDomainCreate(virDomainPtr dom)
const char *prog[] = {VZCTL, "--quiet", "start", PROGRAM_SENTINAL, NULL }; const char *prog[] = {VZCTL, "--quiet", "start", PROGRAM_SENTINAL, NULL };
int ret = -1; int ret = -1;
openvzDriverLock(driver);
vm = virDomainFindByName(&driver->domains, dom->name); vm = virDomainFindByName(&driver->domains, dom->name);
openvzDriverUnlock(driver);
if (!vm) { if (!vm) {
openvzError(dom->conn, VIR_ERR_INVALID_DOMAIN, openvzError(dom->conn, VIR_ERR_INVALID_DOMAIN,
"%s", _("no domain with matching id")); "%s", _("no domain with matching id"));
...@@ -774,6 +837,8 @@ openvzDomainCreate(virDomainPtr dom) ...@@ -774,6 +837,8 @@ openvzDomainCreate(virDomainPtr dom)
ret = 0; ret = 0;
cleanup: cleanup:
if (vm)
virDomainObjUnlock(vm);
return ret; return ret;
} }
...@@ -785,6 +850,7 @@ openvzDomainUndefine(virDomainPtr dom) ...@@ -785,6 +850,7 @@ openvzDomainUndefine(virDomainPtr dom)
const char *prog[] = { VZCTL, "--quiet", "destroy", PROGRAM_SENTINAL, NULL }; const char *prog[] = { VZCTL, "--quiet", "destroy", PROGRAM_SENTINAL, NULL };
int ret = -1; int ret = -1;
openvzDriverLock(driver);
vm = virDomainFindByUUID(&driver->domains, dom->uuid); vm = virDomainFindByUUID(&driver->domains, dom->uuid);
if (!vm) { if (!vm) {
openvzError(dom->conn, VIR_ERR_INVALID_DOMAIN, "%s", _("no domain with matching uuid")); openvzError(dom->conn, VIR_ERR_INVALID_DOMAIN, "%s", _("no domain with matching uuid"));
...@@ -804,9 +870,13 @@ openvzDomainUndefine(virDomainPtr dom) ...@@ -804,9 +870,13 @@ openvzDomainUndefine(virDomainPtr dom)
} }
virDomainRemoveInactive(&driver->domains, vm); virDomainRemoveInactive(&driver->domains, vm);
vm = NULL;
ret = 0; ret = 0;
cleanup: cleanup:
if (vm)
virDomainObjUnlock(vm);
openvzDriverUnlock(driver);
return ret; return ret;
} }
...@@ -820,7 +890,10 @@ openvzDomainSetAutostart(virDomainPtr dom, int autostart) ...@@ -820,7 +890,10 @@ openvzDomainSetAutostart(virDomainPtr dom, int autostart)
"--save", NULL }; "--save", NULL };
int ret = -1; int ret = -1;
openvzDriverLock(driver);
vm = virDomainFindByUUID(&driver->domains, dom->uuid); vm = virDomainFindByUUID(&driver->domains, dom->uuid);
openvzDriverUnlock(driver);
if (!vm) { if (!vm) {
openvzError(dom->conn, VIR_ERR_INVALID_DOMAIN, "%s", _("no domain with matching uuid")); openvzError(dom->conn, VIR_ERR_INVALID_DOMAIN, "%s", _("no domain with matching uuid"));
goto cleanup; goto cleanup;
...@@ -834,6 +907,8 @@ openvzDomainSetAutostart(virDomainPtr dom, int autostart) ...@@ -834,6 +907,8 @@ openvzDomainSetAutostart(virDomainPtr dom, int autostart)
ret = 0; ret = 0;
cleanup: cleanup:
if (vm)
virDomainObjUnlock(vm);
return ret; return ret;
} }
...@@ -845,7 +920,10 @@ openvzDomainGetAutostart(virDomainPtr dom, int *autostart) ...@@ -845,7 +920,10 @@ openvzDomainGetAutostart(virDomainPtr dom, int *autostart)
char value[1024]; char value[1024];
int ret = -1; int ret = -1;
openvzDriverLock(driver);
vm = virDomainFindByUUID(&driver->domains, dom->uuid); vm = virDomainFindByUUID(&driver->domains, dom->uuid);
openvzDriverUnlock(driver);
if (!vm) { if (!vm) {
openvzError(dom->conn, VIR_ERR_INVALID_DOMAIN, openvzError(dom->conn, VIR_ERR_INVALID_DOMAIN,
"%s", _("no domain with matching uuid")); "%s", _("no domain with matching uuid"));
...@@ -864,6 +942,8 @@ openvzDomainGetAutostart(virDomainPtr dom, int *autostart) ...@@ -864,6 +942,8 @@ openvzDomainGetAutostart(virDomainPtr dom, int *autostart)
ret = 0; ret = 0;
cleanup: cleanup:
if (vm)
virDomainObjUnlock(vm);
return ret; return ret;
} }
...@@ -890,7 +970,10 @@ static int openvzDomainSetVcpus(virDomainPtr dom, unsigned int nvcpus) { ...@@ -890,7 +970,10 @@ static int openvzDomainSetVcpus(virDomainPtr dom, unsigned int nvcpus) {
unsigned int pcpus; unsigned int pcpus;
int ret = -1; int ret = -1;
openvzDriverLock(driver);
vm = virDomainFindByUUID(&driver->domains, dom->uuid); vm = virDomainFindByUUID(&driver->domains, dom->uuid);
openvzDriverUnlock(driver);
if (!vm) { if (!vm) {
openvzError(dom->conn, VIR_ERR_INVALID_DOMAIN, openvzError(dom->conn, VIR_ERR_INVALID_DOMAIN,
"%s", _("no domain with matching uuid")); "%s", _("no domain with matching uuid"));
...@@ -921,6 +1004,8 @@ static int openvzDomainSetVcpus(virDomainPtr dom, unsigned int nvcpus) { ...@@ -921,6 +1004,8 @@ static int openvzDomainSetVcpus(virDomainPtr dom, unsigned int nvcpus) {
ret = 0; ret = 0;
cleanup: cleanup:
if (vm)
virDomainObjUnlock(vm);
return ret; return ret;
} }
...@@ -999,7 +1084,9 @@ static char *openvzGetCapabilities(virConnectPtr conn) { ...@@ -999,7 +1084,9 @@ static char *openvzGetCapabilities(virConnectPtr conn) {
struct openvz_driver *driver = conn->privateData; struct openvz_driver *driver = conn->privateData;
char *ret; char *ret;
openvzDriverLock(driver);
ret = virCapabilitiesFormatXML(driver->caps); ret = virCapabilitiesFormatXML(driver->caps);
openvzDriverUnlock(driver);
return ret; return ret;
} }
...@@ -1042,9 +1129,14 @@ static int openvzNumDomains(virConnectPtr conn) { ...@@ -1042,9 +1129,14 @@ static int openvzNumDomains(virConnectPtr conn) {
struct openvz_driver *driver = conn->privateData; struct openvz_driver *driver = conn->privateData;
int nactive = 0, i; int nactive = 0, i;
for (i = 0 ; i < driver->domains.count ; i++) openvzDriverLock(driver);
for (i = 0 ; i < driver->domains.count ; i++) {
virDomainObjLock(driver->domains.objs[i]);
if (virDomainIsActive(driver->domains.objs[i])) if (virDomainIsActive(driver->domains.objs[i]))
nactive++; nactive++;
virDomainObjUnlock(driver->domains.objs[i]);
}
openvzDriverUnlock(driver);
return nactive; return nactive;
} }
...@@ -1139,9 +1231,14 @@ static int openvzNumDefinedDomains(virConnectPtr conn) { ...@@ -1139,9 +1231,14 @@ static int openvzNumDefinedDomains(virConnectPtr conn) {
struct openvz_driver *driver = conn->privateData; struct openvz_driver *driver = conn->privateData;
int ninactive = 0, i; int ninactive = 0, i;
for (i = 0 ; i < driver->domains.count ; i++) openvzDriverLock(driver);
for (i = 0 ; i < driver->domains.count ; i++) {
virDomainObjLock(driver->domains.objs[i]);
if (!virDomainIsActive(driver->domains.objs[i])) if (!virDomainIsActive(driver->domains.objs[i]))
ninactive++; ninactive++;
virDomainObjUnlock(driver->domains.objs[i]);
}
openvzDriverUnlock(driver);
return ninactive; return ninactive;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册