maint: avoid nested use of virConnect{Ref,Close}

The public virConnectRef and virConnectClose API are just thin
wrappers around virObjectRef/virObjectRef, with added object
validation and an error reset.  Within our backend drivers, use
of the object validation is just an inefficiency since we always
pass valid objects.  More important to think about is what
happens with the error reset; our uses of virConnectRef happened
to be safe (since we hadn't encountered any earlier errors), but
in several cases the use of virConnectClose could lose a real
error.

Ideally, we should also avoid calling virConnectOpen() from
within backend drivers - but that is a known situation that
needs much more design work.

* src/qemu/qemu_process.c (qemuProcessReconnectHelper)
(qemuProcessReconnect): Avoid nested public API call.
* src/qemu/qemu_driver.c (qemuAutostartDomains)
(qemuStateInitialize, qemuStateStop): Likewise.
* src/qemu/qemu_migration.c (doPeer2PeerMigrate): Likewise.
* src/storage/storage_driver.c (storageDriverAutostart):
Likewise.
* src/uml/uml_driver.c (umlAutostartConfigs): Likewise.
* src/lxc/lxc_process.c (virLXCProcessAutostartAll): Likewise.
(virLXCProcessReboot): Likewise, and avoid leaking conn on error.

Signed-off-by: Eric Blake <eblake@redhat.com>
This commit is contained in:
Eric Blake 2014-01-03 08:08:52 -07:00
parent c05aebfd65
commit 25221a1b21
6 changed files with 18 additions and 27 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2010-2013 Red Hat, Inc. * Copyright (C) 2010-2014 Red Hat, Inc.
* Copyright IBM Corp. 2008 * Copyright IBM Corp. 2008
* *
* lxc_process.c: LXC process lifecycle management * lxc_process.c: LXC process lifecycle management
@ -103,7 +103,7 @@ virLXCProcessReboot(virLXCDriverPtr driver,
VIR_DEBUG("Faking reboot"); VIR_DEBUG("Faking reboot");
if (conn) { if (conn) {
virConnectRef(conn); virObjectRef(conn);
autodestroy = true; autodestroy = true;
} else { } else {
conn = virConnectOpen("lxc:///"); conn = virConnectOpen("lxc:///");
@ -125,12 +125,10 @@ virLXCProcessReboot(virLXCDriverPtr driver,
goto cleanup; goto cleanup;
} }
if (conn)
virConnectClose(conn);
ret = 0; ret = 0;
cleanup: cleanup:
virObjectUnref(conn);
return ret; return ret;
} }
@ -1428,8 +1426,7 @@ virLXCProcessAutostartAll(virLXCDriverPtr driver)
virLXCProcessAutostartDomain, virLXCProcessAutostartDomain,
&data); &data);
if (conn) virObjectUnref(conn);
virConnectClose(conn);
} }
static int static int

View File

@ -320,8 +320,7 @@ qemuAutostartDomains(virQEMUDriverPtr driver)
virDomainObjListForEach(driver->domains, qemuAutostartDomain, &data); virDomainObjListForEach(driver->domains, qemuAutostartDomain, &data);
if (conn) virObjectUnref(conn);
virConnectClose(conn);
virObjectUnref(cfg); virObjectUnref(cfg);
} }
@ -843,15 +842,13 @@ qemuStateInitialize(bool privileged,
if (!qemu_driver->workerPool) if (!qemu_driver->workerPool)
goto error; goto error;
if (conn) virObjectUnref(conn);
virConnectClose(conn);
virNWFilterRegisterCallbackDriver(&qemuCallbackDriver); virNWFilterRegisterCallbackDriver(&qemuCallbackDriver);
return 0; return 0;
error: error:
if (conn) virObjectUnref(conn);
virConnectClose(conn);
VIR_FREE(driverConf); VIR_FREE(driverConf);
VIR_FREE(membase); VIR_FREE(membase);
VIR_FREE(mempath); VIR_FREE(mempath);
@ -970,8 +967,7 @@ qemuStateStop(void) {
virDomainFree(domains[i]); virDomainFree(domains[i]);
VIR_FREE(domains); VIR_FREE(domains);
VIR_FREE(flags); VIR_FREE(flags);
if (conn) virObjectUnref(conn);
virConnectClose(conn);
virObjectUnref(cfg); virObjectUnref(cfg);
return ret; return ret;

View File

@ -2,7 +2,7 @@
/* /*
* qemu_migration.c: QEMU migration handling * qemu_migration.c: QEMU migration handling
* *
* Copyright (C) 2006-2013 Red Hat, Inc. * Copyright (C) 2006-2014 Red Hat, Inc.
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
@ -4021,7 +4021,7 @@ static int doPeer2PeerMigrate(virQEMUDriverPtr driver,
cleanup: cleanup:
orig_err = virSaveLastError(); orig_err = virSaveLastError();
qemuDomainObjEnterRemote(vm); qemuDomainObjEnterRemote(vm);
virConnectClose(dconn); virObjectUnref(dconn);
qemuDomainObjExitRemote(vm); qemuDomainObjExitRemote(vm);
if (orig_err) { if (orig_err) {
virSetError(orig_err); virSetError(orig_err);

View File

@ -1,7 +1,7 @@
/* /*
* qemu_process.c: QEMU process management * qemu_process.c: QEMU process management
* *
* Copyright (C) 2006-2013 Red Hat, Inc. * Copyright (C) 2006-2014 Red Hat, Inc.
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
@ -3270,7 +3270,7 @@ endjob:
if (obj && virObjectUnref(obj)) if (obj && virObjectUnref(obj))
virObjectUnlock(obj); virObjectUnlock(obj);
virConnectClose(conn); virObjectUnref(conn);
virObjectUnref(cfg); virObjectUnref(cfg);
return; return;
@ -3305,7 +3305,7 @@ error:
virObjectUnlock(obj); virObjectUnlock(obj);
} }
} }
virConnectClose(conn); virObjectUnref(conn);
virObjectUnref(cfg); virObjectUnref(cfg);
} }
@ -3352,11 +3352,11 @@ qemuProcessReconnectHelper(virDomainObjPtr obj,
* that the threads we start see a valid connection throughout their * that the threads we start see a valid connection throughout their
* lifetime. We simply increase the reference counter here. * lifetime. We simply increase the reference counter here.
*/ */
virConnectRef(data->conn); virObjectRef(data->conn);
if (virThreadCreate(&thread, false, qemuProcessReconnect, data) < 0) { if (virThreadCreate(&thread, false, qemuProcessReconnect, data) < 0) {
virConnectClose(data->conn); virObjectUnref(data->conn);
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Could not create thread. QEMU initialization " _("Could not create thread. QEMU initialization "

View File

@ -1,7 +1,7 @@
/* /*
* storage_driver.c: core driver for storage APIs * storage_driver.c: core driver for storage APIs
* *
* Copyright (C) 2006-2013 Red Hat, Inc. * Copyright (C) 2006-2014 Red Hat, Inc.
* Copyright (C) 2006-2008 Daniel P. Berrange * Copyright (C) 2006-2008 Daniel P. Berrange
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
@ -130,8 +130,7 @@ storageDriverAutostart(virStorageDriverStatePtr driver) {
virStoragePoolObjUnlock(pool); virStoragePoolObjUnlock(pool);
} }
if (conn) virObjectUnref(conn);
virConnectClose(conn);
} }
/** /**

View File

@ -224,8 +224,7 @@ umlAutostartConfigs(struct uml_driver *driver) {
virDomainObjListForEach(driver->domains, umlAutostartDomain, &data); virDomainObjListForEach(driver->domains, umlAutostartDomain, &data);
umlDriverUnlock(driver); umlDriverUnlock(driver);
if (conn) virObjectUnref(conn);
virConnectClose(conn);
} }