[ovs-dev] [PATCH] util: New ovs_retval_to_string() function.

Andrew Evans aevans at nicira.com
Sun Jan 30 19:37:53 UTC 2011


Many OVS functions return 0, EOF, or errno. There are several places in the
codebase where we do something like:

    if (!retval) {
        return "";
    }
    if (retval > 0) {
        return strerror(retval);
    }
    if (retval == EOF) {
        return "End of file";
    }
    return "***unknown error***";

This consolidates that code in one place. My forthcoming Manager status patch
requires this too.
---
 lib/ovsdb-error.c |    1 -
 lib/pcap.c        |    7 +++----
 lib/util.c        |   32 ++++++++++++++++++++++++++++----
 lib/util.h        |    1 +
 ofproto/ofproto.c |    7 ++-----
 5 files changed, 34 insertions(+), 14 deletions(-)

diff --git a/lib/ovsdb-error.c b/lib/ovsdb-error.c
index db8019f..d6b4576 100644
--- a/lib/ovsdb-error.c
+++ b/lib/ovsdb-error.c
@@ -237,4 +237,3 @@ ovsdb_error_assert(struct ovsdb_error *error)
         ovsdb_error_destroy(error);
     }
 }
-
diff --git a/lib/pcap.c b/lib/pcap.c
index 8c52f48..afd41fa 100644
--- a/lib/pcap.c
+++ b/lib/pcap.c
@@ -76,8 +76,7 @@ pcap_read_header(FILE *file)
     struct pcap_hdr ph;
     if (fread(&ph, sizeof ph, 1, file) != 1) {
         int error = ferror(file) ? errno : EOF;
-        VLOG_WARN("failed to read pcap header: %s",
-                  error > 0 ? strerror(error) : "end of file");
+        VLOG_WARN("failed to read pcap header: %s", ovs_retval_to_string(error));
         return error;
     }
     if (ph.magic_number != 0xa1b2c3d4 && ph.magic_number != 0xd4c3b2a1) {
@@ -118,7 +117,7 @@ pcap_read(FILE *file, struct ofpbuf **bufp)
     if (fread(&prh, sizeof prh, 1, file) != 1) {
         int error = ferror(file) ? errno : EOF;
         VLOG_WARN("failed to read pcap record header: %s",
-                  error > 0 ? strerror(error) : "end of file");
+                  ovs_retval_to_string(error));
         return error;
     }
 
@@ -144,7 +143,7 @@ pcap_read(FILE *file, struct ofpbuf **bufp)
     if (fread(data, len, 1, file) != 1) {
         int error = ferror(file) ? errno : EOF;
         VLOG_WARN("failed to read pcap packet: %s",
-                  error > 0 ? strerror(error) : "end of file");
+                  ovs_retval_to_string(error));
         ofpbuf_delete(buf);
         return error;
     }
diff --git a/lib/util.c b/lib/util.c
index d6e470c..b7577f5 100644
--- a/lib/util.c
+++ b/lib/util.c
@@ -158,8 +158,7 @@ ovs_fatal(int err_no, const char *format, ...)
     vfprintf(stderr, format, args);
     va_end(args);
     if (err_no != 0)
-        fprintf(stderr, " (%s)",
-                err_no == EOF ? "end of file" : strerror(err_no));
+        fprintf(stderr, " (%s)", ovs_retval_to_string(err_no));
     putc('\n', stderr);
 
     exit(EXIT_FAILURE);
@@ -176,14 +175,39 @@ ovs_error(int err_no, const char *format, ...)
     vfprintf(stderr, format, args);
     va_end(args);
     if (err_no != 0) {
-        fprintf(stderr, " (%s)",
-                err_no == EOF ? "end of file" : strerror(err_no));
+        fprintf(stderr, " (%s)", ovs_retval_to_string(err_no));
     }
     putc('\n', stderr);
 
     errno = save_errno;
 }
 
+/* Many OVS functions return an int which is one of:
+ * - 0: no error yet
+ * - >0: errno value
+ * - EOF: end of file (not necessarily an error; depends on the function called)
+ *
+ * Returns the appropriate human-readable string. The caller must copy the
+ * string if it wants to hold onto it, as the storage may be overwritten on
+ * subsequent function calls.
+ */
+const char *
+ovs_retval_to_string(int retval)
+{
+    if (!retval) {
+        return "";
+    }
+    if (retval > 0) {
+        return strerror(retval);
+    }
+    switch (retval) {
+    case EOF:
+        return "End of file";
+    default:
+        return "***unknown error***";
+    }
+}
+
 /* Sets program_name based on 'argv0'.  Should be called at the beginning of
  * main(), as "set_program_name(argv[0]);".  */
 void set_program_name(const char *argv0)
diff --git a/lib/util.h b/lib/util.h
index 4979049..f3bf80c 100644
--- a/lib/util.h
+++ b/lib/util.h
@@ -137,6 +137,7 @@ void ovs_strlcpy(char *dst, const char *src, size_t size);
 void ovs_fatal(int err_no, const char *format, ...)
     PRINTF_FORMAT(2, 3) NO_RETURN;
 void ovs_error(int err_no, const char *format, ...) PRINTF_FORMAT(2, 3);
+const char *ovs_retval_to_string(int);
 void ovs_hex_dump(FILE *, const void *, size_t, uintptr_t offset, bool ascii);
 
 bool str_to_int(const char *, int base, int *);
diff --git a/ofproto/ofproto.c b/ofproto/ofproto.c
index 67ce714..c980f69 100644
--- a/ofproto/ofproto.c
+++ b/ofproto/ofproto.c
@@ -1363,13 +1363,10 @@ ofproto_get_ofproto_controller_info(const struct ofproto * ofproto,
 
         cinfo->pairs.n = 0;
 
-        if (last_error == EOF) {
-            cinfo->pairs.keys[cinfo->pairs.n] = "last_error";
-            cinfo->pairs.values[cinfo->pairs.n++] = xstrdup("End of file");
-        } else if (last_error > 0) {
+        if (last_error) {
             cinfo->pairs.keys[cinfo->pairs.n] = "last_error";
             cinfo->pairs.values[cinfo->pairs.n++] =
-                xstrdup(strerror(last_error));
+                xstrdup(ovs_retval_to_string(last_error));
         }
 
         cinfo->pairs.keys[cinfo->pairs.n] = "state";
-- 
1.7.2.3





More information about the dev mailing list