Commit f9648989 authored by Michal Privoznik's avatar Michal Privoznik

libvirtSnmp: turn showError() into printf-like function

Currently, the following pattern is used with showError:

  printf("Some error message\n");
  showError(conn);

Not only showError does not need connection pointer, those two
lines can be merged into a single one:

  showError("Some error message");
Signed-off-by: 's avatarMichal Privoznik <mprivozn@redhat.com>
Reviewed-by: 's avatarMartin Kletzander <mkletzan@redhat.com>
parent 2cf002f1
......@@ -43,6 +43,17 @@
#define ATTRIBUTE_UNUSED __attribute__((__unused__))
#endif
# ifndef ATTRIBUTE_FMT_PRINTF
# ifndef __clang__
# define ATTRIBUTE_FMT_PRINTF(fmtpos, argpos) \
__attribute__((__format__ (__gnu_printf__, fmtpos, argpos)))
# else
# define ATTRIBUTE_FMT_PRINTF(fmtpos, argpos) \
__attribute__((__format__ (__printf__, fmtpos, argpos)))
# endif
# endif
int verbose = 0;
virConnectPtr conn;
int callbackRet = -1;
......@@ -76,11 +87,31 @@ stop(int sig)
}
static void
showError(virConnectPtr conn)
ATTRIBUTE_FMT_PRINTF(1,2)
showError(const char *fmt, ...)
{
const char *err = virGetLastErrorMessage();
const char *libvirtErr = virGetLastErrorMessage();
char ebuf[1024];
int rc;
int size = 0;
va_list ap;
va_start(ap, fmt);
rc = vsnprintf(ebuf, sizeof(ebuf), fmt, ap);
size += rc;
va_end(ap);
if (rc < 0 || size >= sizeof(ebuf))
return;
snmp_log(LOG_ERR, "libvirt reported: \"%s\"\n", err);
rc = snprintf(ebuf + size, sizeof(ebuf) - size, ": %s\n", libvirtErr);
size += rc;
if (rc < 0 || size >= sizeof(ebuf))
return;
fputs(ebuf, stderr);
snmp_log(LOG_ERR, "%s", ebuf);
virResetLastError();
}
......@@ -95,8 +126,7 @@ insertGuest(netsnmp_container *container, virDomainPtr domain)
unsigned char uuid[VIR_UUID_BUFLEN];
if (virDomainGetInfo(domain, &info) < 0) {
printf("Failed to get domain info\n");
showError(conn);
showError("Failed to get domain info");
goto error;
}
......@@ -108,8 +138,7 @@ insertGuest(netsnmp_container *container, virDomainPtr domain)
/* set the index of the row */
if (virDomainGetUUID(domain, uuid) < 0) {
printf("Failed to get domain UUID\n");
showError(conn);
showError("Failed to get domain UUID");
goto error;
}
......@@ -122,8 +151,7 @@ insertGuest(netsnmp_container *container, virDomainPtr domain)
/* set the data */
if (!(name = virDomainGetName(domain))) {
printf("Failed to get domain name\n");
showError(conn);
showError("Failed to get domain name");
goto error;
}
......@@ -167,8 +195,7 @@ libvirtSnmpLoadGuests(netsnmp_container *container)
virDomainPtr *domains = NULL;
if ((ndomains = virConnectListAllDomains(conn, &domains, 0)) < 0) {
printf("Failed to list all domains\n");
showError(conn);
showError("Failed to list all domains");
goto cleanup;
}
......@@ -194,7 +221,7 @@ pollingThreadFunc(void *foo)
{
while (run) {
if (virEventRunDefaultImpl() < 0) {
showError(conn);
showError("Failed to run event loop");
pthread_exit(NULL);
}
}
......@@ -222,8 +249,10 @@ libvirtRegisterEvents(virConnectPtr conn) {
(domainEventCallback),
NULL, myFreeFunc);
if (callbackRet == -1)
return -1;
if (callbackRet == -1) {
showError("Failed to register libvirt event handler");
goto cleanup;
}
/* we need a thread to poll for events */
if (pthread_create(&poll_thread, NULL, pollingThreadFunc, NULL))
......@@ -258,8 +287,7 @@ int libvirtSnmpInit(void)
/* TODO: configure the URI */
/* Use libvirt env variable LIBVIRT_DEFAULT_URI by default*/
if (!(conn = virConnectOpenAuth(NULL, virConnectAuthPtrDefault, 0))) {
printf("No connection to hypervisor\n");
showError(conn);
showError("No connection to hypervisor");
return -1;
}
......@@ -298,9 +326,8 @@ void libvirtSnmpShutdown(void)
}
if ((rc = virConnectClose(conn))) {
printf("Failed to disconnect from hypervisor. "
"Leaked references: %d\n", rc);
showError(conn);
showError("Failed to disconnect from hypervisor. "
"Leaked references: %d\n", rc);
}
}
......@@ -339,7 +366,7 @@ libvirtSnmpCreate(unsigned char *uuid, int state)
}
if (virDomainCreateWithFlags(dom, flags) < 0) {
showError(conn);
showError("Failed to create domain: %s", virDomainGetName(dom));
goto cleanup;
}
......@@ -353,6 +380,7 @@ int
libvirtSnmpDestroy(unsigned char *uuid)
{
virDomainPtr dom;
int ret = -1;
if (!(dom = virDomainLookupByUUID(conn, uuid))) {
printf("Cannot find domain to destroy\n");
......@@ -360,10 +388,12 @@ libvirtSnmpDestroy(unsigned char *uuid)
}
if (virDomainDestroy(dom) < 0) {
showError(conn);
return -1;
showError("Failed to destroy domain %s", virDomainGetName(dom));
goto cleanup;
}
ret = 0;
cleanup:
virDomainFree(dom);
return 0;
}
......@@ -374,8 +404,7 @@ libvirtSnmpChangeState(unsigned char *uuid, int newstate, int oldstate)
virDomainPtr dom;
int ret = -1;
dom = virDomainLookupByUUID(conn, uuid);
if (dom == NULL) {
if (!(dom = virDomainLookupByUUID(conn, uuid))) {
printf("Cannot find domain to change\n");
return 1;
}
......@@ -392,7 +421,7 @@ libvirtSnmpChangeState(unsigned char *uuid, int newstate, int oldstate)
}
if (ret < 0)
showError(conn);
showError("Failed to change state of %s", virDomainGetName(dom));
out:
virDomainFree(dom);
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