Commit e52c5857 authored by Michal Privoznik's avatar Michal Privoznik

libvirtSnmp: Rewrite some functions

Some function initialize retval to 0 and then for every error
path they reset it to -1.
Signed-off-by: 's avatarMichal Privoznik <mprivozn@redhat.com>
Reviewed-by: 's avatarMartin Kletzander <mkletzan@redhat.com>
parent 898f1096
...@@ -258,9 +258,7 @@ int libvirtSnmpInit(void) ...@@ -258,9 +258,7 @@ int libvirtSnmpInit(void)
/* TODO: configure the URI */ /* TODO: configure the URI */
/* Use libvirt env variable LIBVIRT_DEFAULT_URI by default*/ /* Use libvirt env variable LIBVIRT_DEFAULT_URI by default*/
conn = virConnectOpenAuth(NULL, virConnectAuthPtrDefault, 0); if (!(conn = virConnectOpenAuth(NULL, virConnectAuthPtrDefault, 0))) {
if (NULL == conn) {
printf("No connection to hypervisor\n"); printf("No connection to hypervisor\n");
showError(conn); showError(conn);
return -1; return -1;
...@@ -283,6 +281,7 @@ libvirtDeinitTimer(int timer ATTRIBUTE_UNUSED, void *opaque ATTRIBUTE_UNUSED) ...@@ -283,6 +281,7 @@ libvirtDeinitTimer(int timer ATTRIBUTE_UNUSED, void *opaque ATTRIBUTE_UNUSED)
void libvirtSnmpShutdown(void) void libvirtSnmpShutdown(void)
{ {
int timer; int timer;
int rc;
/* in case server is being stopped, run is still 1, so let's /* in case server is being stopped, run is still 1, so let's
* shutdown the thread in a clean way */ * shutdown the thread in a clean way */
...@@ -299,8 +298,9 @@ void libvirtSnmpShutdown(void) ...@@ -299,8 +298,9 @@ void libvirtSnmpShutdown(void)
printf("Failed to unregister domain events\n"); printf("Failed to unregister domain events\n");
} }
if (0 != virConnectClose(conn)) { if ((rc = virConnectClose(conn))) {
printf("Failed to disconnect from hypervisor\n"); printf("Failed to disconnect from hypervisor. "
"Leaked references: %d\n", rc);
showError(conn); showError(conn);
} }
} }
...@@ -320,18 +320,11 @@ int ...@@ -320,18 +320,11 @@ int
libvirtSnmpCreate(unsigned char *uuid, int state) libvirtSnmpCreate(unsigned char *uuid, int state)
{ {
virDomainPtr dom; virDomainPtr dom;
int ret; int ret = -1;
unsigned int flags = 0; unsigned int flags = 0;
dom = virDomainLookupByUUID(conn, uuid);
if (dom == NULL) {
printf("Cannot find domain to create\n");
return -1;
}
switch(state) { switch(state) {
case VIR_DOMAIN_RUNNING: case VIR_DOMAIN_RUNNING:
flags = 0;
break; break;
case VIR_DOMAIN_PAUSED: case VIR_DOMAIN_PAUSED:
flags = VIR_DOMAIN_START_PAUSED; flags = VIR_DOMAIN_START_PAUSED;
...@@ -341,10 +334,18 @@ libvirtSnmpCreate(unsigned char *uuid, int state) ...@@ -341,10 +334,18 @@ libvirtSnmpCreate(unsigned char *uuid, int state)
return -1; return -1;
} }
ret = virDomainCreateWithFlags(dom, flags); if (!(dom = virDomainLookupByUUID(conn, uuid))) {
if (ret) { printf("Cannot find domain to create\n");
return -1;
}
if (virDomainCreateWithFlags(dom, flags) < 0) {
showError(conn); showError(conn);
goto cleanup;
} }
ret = 0;
cleanup:
virDomainFree(dom); virDomainFree(dom);
return ret; return ret;
} }
...@@ -353,27 +354,26 @@ int ...@@ -353,27 +354,26 @@ int
libvirtSnmpDestroy(unsigned char *uuid) libvirtSnmpDestroy(unsigned char *uuid)
{ {
virDomainPtr dom; virDomainPtr dom;
int ret;
dom = virDomainLookupByUUID(conn, uuid); if (!(dom = virDomainLookupByUUID(conn, uuid))) {
if (dom == NULL) {
printf("Cannot find domain to destroy\n"); printf("Cannot find domain to destroy\n");
return -1; return -1;
} }
ret = virDomainDestroy(dom); if (virDomainDestroy(dom) < 0) {
if (ret) {
showError(conn); showError(conn);
return -1;
} }
virDomainFree(dom); virDomainFree(dom);
return ret; return 0;
} }
int int
libvirtSnmpChangeState(unsigned char *uuid, int newstate, int oldstate) libvirtSnmpChangeState(unsigned char *uuid, int newstate, int oldstate)
{ {
virDomainPtr dom; virDomainPtr dom;
int ret = 0; int ret = -1;
dom = virDomainLookupByUUID(conn, uuid); dom = virDomainLookupByUUID(conn, uuid);
if (dom == NULL) { if (dom == NULL) {
...@@ -389,13 +389,11 @@ libvirtSnmpChangeState(unsigned char *uuid, int newstate, int oldstate) ...@@ -389,13 +389,11 @@ libvirtSnmpChangeState(unsigned char *uuid, int newstate, int oldstate)
ret = virDomainShutdown(dom); ret = virDomainShutdown(dom);
else { else {
printf("Wrong state transition from %d to %d\n", oldstate, newstate); printf("Wrong state transition from %d to %d\n", oldstate, newstate);
ret = -1;
goto out; goto out;
} }
if (ret != 0) { if (ret < 0)
showError(conn); showError(conn);
}
out: out:
virDomainFree(dom); virDomainFree(dom);
return ret; return ret;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment