[libvirt] [snmp PATCH 16/20] libvirtSnmpError: Introduce and use printSystemError

Michal Privoznik mprivozn at redhat.com
Thu Oct 18 12:26:54 UTC 2018


This function formats passed string and prints stringified system
error.

Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
---
 src/libvirtSnmp.c      |  8 ++++++--
 src/libvirtSnmpError.c | 38 ++++++++++++++++++++++++++++++++++++++
 src/libvirtSnmpError.h |  3 +++
 3 files changed, 47 insertions(+), 2 deletions(-)

diff --git a/src/libvirtSnmp.c b/src/libvirtSnmp.c
index 97bb927..3a93bc6 100644
--- a/src/libvirtSnmp.c
+++ b/src/libvirtSnmp.c
@@ -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:
diff --git a/src/libvirtSnmpError.c b/src/libvirtSnmpError.c
index c356aae..33a65c2 100644
--- a/src/libvirtSnmpError.c
+++ b/src/libvirtSnmpError.c
@@ -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);
+}
diff --git a/src/libvirtSnmpError.h b/src/libvirtSnmpError.h
index 46c98f0..e8822d1 100644
--- a/src/libvirtSnmpError.h
+++ b/src/libvirtSnmpError.h
@@ -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__ */
-- 
2.18.1




More information about the libvir-list mailing list