From c18ea70d06e980dc768166881c5aeac0b3491561 Mon Sep 17 00:00:00 2001 From: Andrew Evans Date: Sun, 30 Jan 2011 11:29:14 -0800 Subject: [PATCH] util: New ovs_retval_to_string() function. Many OVS functions return 0, EOF, or errno. There are several places in the codebase where a return value is converted to a string. All must decide whether the return value is set, and if it is, whether it is an errno value, EOF, or otherwise invalid. This commit consolidates that code. Reviewed by Ben Pfaff. --- lib/jsonrpc.c | 6 ++++++ lib/ovsdb-error.c | 1 - lib/pcap.c | 7 +++---- lib/util.c | 33 +++++++++++++++++++++++++++++---- lib/util.h | 1 + ofproto/ofproto.c | 7 ++----- 6 files changed, 41 insertions(+), 14 deletions(-) diff --git a/lib/jsonrpc.c b/lib/jsonrpc.c index 7c761ea0d..fb20bc585 100644 --- a/lib/jsonrpc.c +++ b/lib/jsonrpc.c @@ -142,6 +142,12 @@ jsonrpc_wait(struct jsonrpc *rpc) } } +/* + * Possible status values: + * - 0: no error yet + * - >0: errno value + * - EOF: end of file (remote end closed connection; not necessarily an error) + */ int jsonrpc_get_status(const struct jsonrpc *rpc) { diff --git a/lib/ovsdb-error.c b/lib/ovsdb-error.c index db8019f38..d6b4576ea 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 8c52f4834..afd41fa81 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 d6e470c92..193efb923 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,40 @@ 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) +{ + static char unknown[48]; + + if (!retval) { + return ""; + } + if (retval > 0) { + return strerror(retval); + } + if (retval == EOF) { + return "End of file"; + } + snprintf(unknown, sizeof unknown, "***unknown return value: %d***", retval); + return unknown; +} + /* 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 497904906..f3bf80c6e 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 67ce714d0..c980f6931 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"; -- 2.43.0