Commit 78b122d0 authored by Michal Privoznik's avatar Michal Privoznik

libvirtSnmpError: Introduce and use printSystemError

This function formats passed string and prints stringified system
error.
Signed-off-by: 's avatarMichal Privoznik <mprivozn@redhat.com>
Reviewed-by: 's avatarMartin Kletzander <mkletzan@redhat.com>
parent bf9cb8fa
......@@ -189,8 +189,10 @@ pollingThreadFunc(void *foo)
/* Function to register domain lifecycle events collection */
int
libvirtRegisterEvents(virConnectPtr conn) {
libvirtRegisterEvents(virConnectPtr conn)
{
struct sigaction action_stop;
int rc;
int ret = -1;
memset(&action_stop, 0, sizeof action_stop);
......@@ -214,8 +216,10 @@ libvirtRegisterEvents(virConnectPtr conn) {
}
/* we need a thread to poll for events */
if (pthread_create(&poll_thread, NULL, pollingThreadFunc, NULL))
if ((rc = pthread_create(&poll_thread, NULL, pollingThreadFunc, NULL))) {
printSystemError(rc, "Unable to create polling thread");
goto cleanup;
}
ret = 0;
cleanup:
......
......@@ -65,3 +65,41 @@ printLibvirtError(const char *fmt, ...)
virResetLastError();
}
/**
* printSystemError:
* @theerrno: the errno value
* @fmt: Error message format string
*
* Print system error with @theerrno translated into human readable form.
*/
void
printSystemError(int theerrno, const char *fmt, ...)
{
char ebuf[1024];
char sysebuf[1024];
int rc;
int size = 0;
va_list ap;
if (!strerror_r(theerrno, sysebuf, sizeof(sysebuf)))
return;
va_start(ap, fmt);
rc = vsnprintf(ebuf, sizeof(ebuf), fmt, ap);
size += rc;
va_end(ap);
if (rc < 0 || size >= sizeof(ebuf))
return;
rc = snprintf(ebuf + size, sizeof(ebuf) - size, ": %s\n", sysebuf);
size += rc;
if (rc < 0 || size >= sizeof(ebuf))
return;
fputs(ebuf, stderr);
snmp_log(LOG_ERR, "%s", ebuf);
}
......@@ -36,4 +36,7 @@
void printLibvirtError(const char *fmt, ...)
ATTRIBUTE_FMT_PRINTF(1, 2);
void printSystemError(int theerrno, const char *fmt, ...)
ATTRIBUTE_FMT_PRINTF(2, 3);
#endif /* __LIBVIRT_SNMP_ERROR_H__ */
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