[ovs-dev] [PATCH 1/3] util: Store version in global "program_version" string.

Justin Pettit jpettit at nicira.com
Thu Aug 4 08:50:12 UTC 2011


Useful in an upcoming commit.
---
 lib/util.c                          |   28 +++++++++++++++++++++-------
 lib/util.h                          |   10 +++++-----
 ovsdb/ovsdb-client.c                |    4 ++--
 ovsdb/ovsdb-server.c                |    4 ++--
 ovsdb/ovsdb-tool.c                  |    4 ++--
 tests/test-bundle.c                 |    2 +-
 tests/test-classifier.c             |    2 +-
 tests/test-flows.c                  |    2 +-
 tests/test-json.c                   |    2 +-
 tests/test-jsonrpc.c                |    2 +-
 tests/test-lockfile.c               |    2 +-
 tests/test-multipath.c              |    2 +-
 tests/test-openflowd.c              |    4 ++--
 tests/test-ovsdb.c                  |    2 +-
 tests/test-timeval.c                |    2 +-
 tests/test-unix-socket.c            |    2 +-
 tests/test-vconn.c                  |    2 +-
 utilities/nlmon.c                   |    2 +-
 utilities/ovs-appctl.c              |    4 ++--
 utilities/ovs-benchmark.c           |    4 ++--
 utilities/ovs-controller.c          |    4 ++--
 utilities/ovs-dpctl.c               |    4 ++--
 utilities/ovs-ofctl.c               |    4 ++--
 utilities/ovs-vlan-bug-workaround.c |    4 ++--
 utilities/ovs-vsctl.c               |    4 ++--
 vswitchd/ovs-brcompatd.c            |    4 ++--
 vswitchd/ovs-vswitchd.c             |    4 ++--
 27 files changed, 64 insertions(+), 50 deletions(-)

diff --git a/lib/util.c b/lib/util.c
index 639424d..e4dd6de 100644
--- a/lib/util.c
+++ b/lib/util.c
@@ -33,6 +33,7 @@ VLOG_DEFINE_THIS_MODULE(util);
 COVERAGE_DEFINE(util_xalloc);
 
 const char *program_name;
+char *program_version;
 
 void
 out_of_memory(void)
@@ -277,21 +278,34 @@ ovs_retval_to_string(int 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)
+/* Sets global "program_name" and "program_version" variables.  Should
+ * be called at the beginning of main() with "argv[0]" as the argument
+ * to 'argv0'.
+ *
+ * The 'date' and 'time' arguments should likely be called with
+ * "__DATE__" and "__TIME__" to use the time the binary was built.
+ * Alternatively, the SET_PROGRAM_NAME macro may be called to do this
+ * automatically.
+ */
+void set_program_name(const char *argv0, const char *date, const char *time)
 {
     const char *slash = strrchr(argv0, '/');
     program_name = slash ? slash + 1 : argv0;
+
+    if (program_version) {
+        free(program_version);
+    }
+
+    program_version = xasprintf("%s (Open vSwitch) "VERSION BUILDNR"\n"
+                                "Compiled %s %s\n",
+                                program_name, date, time);
 }
 
 /* Print the version information for the program.  */
 void
-ovs_print_version(char *date, char *time,
-                  uint8_t min_ofp, uint8_t max_ofp)
+ovs_print_version(uint8_t min_ofp, uint8_t max_ofp)
 {
-    printf("%s (Open vSwitch) "VERSION BUILDNR"\n", program_name);
-    printf("Compiled %s %s\n", date, time);
+    printf("%s", program_version);
     if (min_ofp || max_ofp) {
         printf("OpenFlow versions %#x:%#x\n", min_ofp, max_ofp);
     }
diff --git a/lib/util.h b/lib/util.h
index 601f49f..54ec5ad 100644
--- a/lib/util.h
+++ b/lib/util.h
@@ -54,6 +54,7 @@
 #endif /* __cplusplus */
 
 extern const char *program_name;
+extern char *program_version;
 
 /* Returns the number of elements in ARRAY. */
 #define ARRAY_SIZE(ARRAY) (sizeof ARRAY / sizeof *ARRAY)
@@ -140,12 +141,11 @@ extern const char *program_name;
 extern "C" {
 #endif
 
-void set_program_name(const char *);
+void set_program_name(const char *name, const char *date, const char *time);
+#define SET_PROGRAM_NAME(name) \
+        set_program_name(name, __DATE__, __TIME__)
 
-void ovs_print_version(char *date, char *time,
-                       uint8_t min_ofp, uint8_t max_ofp);
-#define OVS_PRINT_VERSION(min_ofp, max_ofp) \
-        ovs_print_version(__DATE__, __TIME__, (min_ofp), (max_ofp))
+void ovs_print_version(uint8_t min_ofp, uint8_t max_ofp);
 
 void out_of_memory(void) NO_RETURN;
 void *xmalloc(size_t) MALLOC_LIKE;
diff --git a/ovsdb/ovsdb-client.c b/ovsdb/ovsdb-client.c
index 2ed1189..634e6b3 100644
--- a/ovsdb/ovsdb-client.c
+++ b/ovsdb/ovsdb-client.c
@@ -82,7 +82,7 @@ main(int argc, char *argv[])
     struct jsonrpc *rpc;
 
     proctitle_init(argc, argv);
-    set_program_name(argv[0]);
+    SET_PROGRAM_NAME(argv[0]);
     parse_options(argc, argv);
     signal(SIGPIPE, SIG_IGN);
 
@@ -190,7 +190,7 @@ parse_options(int argc, char *argv[])
             usage();
 
         case 'V':
-            OVS_PRINT_VERSION(0, 0);
+            ovs_print_version(0, 0);
             exit(EXIT_SUCCESS);
 
         case 'v':
diff --git a/ovsdb/ovsdb-server.c b/ovsdb/ovsdb-server.c
index 2d332fe..497b688 100644
--- a/ovsdb/ovsdb-server.c
+++ b/ovsdb/ovsdb-server.c
@@ -93,7 +93,7 @@ main(int argc, char *argv[])
     long long int status_timer = LLONG_MIN;
 
     proctitle_init(argc, argv);
-    set_program_name(argv[0]);
+    SET_PROGRAM_NAME(argv[0]);
     stress_init_command();
     signal(SIGPIPE, SIG_IGN);
     process_init();
@@ -709,7 +709,7 @@ parse_options(int argc, char *argv[], char **file_namep,
             usage();
 
         case 'V':
-            OVS_PRINT_VERSION(0, 0);
+            ovs_print_version(0, 0);
             exit(EXIT_SUCCESS);
 
         VLOG_OPTION_HANDLERS
diff --git a/ovsdb/ovsdb-tool.c b/ovsdb/ovsdb-tool.c
index 74dfa5a..47428fb 100644
--- a/ovsdb/ovsdb-tool.c
+++ b/ovsdb/ovsdb-tool.c
@@ -49,7 +49,7 @@ static void parse_options(int argc, char *argv[]);
 int
 main(int argc, char *argv[])
 {
-    set_program_name(argv[0]);
+    SET_PROGRAM_NAME(argv[0]);
     parse_options(argc, argv);
     signal(SIGPIPE, SIG_IGN);
     run_command(argc - optind, argv + optind, all_commands);
@@ -85,7 +85,7 @@ parse_options(int argc, char *argv[])
             usage();
 
         case 'V':
-            OVS_PRINT_VERSION(0, 0);
+            ovs_print_version(0, 0);
             exit(EXIT_SUCCESS);
 
         case 'v':
diff --git a/tests/test-bundle.c b/tests/test-bundle.c
index 16e264d..063bd3e 100644
--- a/tests/test-bundle.c
+++ b/tests/test-bundle.c
@@ -107,7 +107,7 @@ main(int argc, char *argv[])
     struct slave_group sg;
     int old_active;
 
-    set_program_name(argv[0]);
+    SET_PROGRAM_NAME(argv[0]);
     random_init();
 
     if (argc != 2) {
diff --git a/tests/test-classifier.c b/tests/test-classifier.c
index 1cfd5cf..b216a16 100644
--- a/tests/test-classifier.c
+++ b/tests/test-classifier.c
@@ -895,7 +895,7 @@ static const struct command commands[] = {
 int
 main(int argc, char *argv[])
 {
-    set_program_name(argv[0]);
+    SET_PROGRAM_NAME(argv[0]);
     init_values();
     run_command(argc - 1, argv + 1, commands);
     return 0;
diff --git a/tests/test-flows.c b/tests/test-flows.c
index 559af3a..5606551 100644
--- a/tests/test-flows.c
+++ b/tests/test-flows.c
@@ -40,7 +40,7 @@ main(int argc OVS_UNUSED, char *argv[])
     int retval;
     int n = 0, errors = 0;
 
-    set_program_name(argv[0]);
+    SET_PROGRAM_NAME(argv[0]);
 
     flows = stdin;
     pcap = fdopen(3, "rb");
diff --git a/tests/test-json.c b/tests/test-json.c
index f297dc2..e2528e0 100644
--- a/tests/test-json.c
+++ b/tests/test-json.c
@@ -110,7 +110,7 @@ main(int argc, char *argv[])
     FILE *stream;
     bool ok;
 
-    set_program_name(argv[0]);
+    SET_PROGRAM_NAME(argv[0]);
 
     for (;;) {
         static const struct option options[] = {
diff --git a/tests/test-jsonrpc.c b/tests/test-jsonrpc.c
index d892ece..970349a 100644
--- a/tests/test-jsonrpc.c
+++ b/tests/test-jsonrpc.c
@@ -43,7 +43,7 @@ int
 main(int argc, char *argv[])
 {
     proctitle_init(argc, argv);
-    set_program_name(argv[0]);
+    SET_PROGRAM_NAME(argv[0]);
     parse_options(argc, argv);
     run_command(argc - optind, argv + optind, all_commands);
     return 0;
diff --git a/tests/test-lockfile.c b/tests/test-lockfile.c
index 121b2ba..b4f541b 100644
--- a/tests/test-lockfile.c
+++ b/tests/test-lockfile.c
@@ -241,7 +241,7 @@ main(int argc, char *argv[])
     extern struct vlog_module VLM_lockfile;
     size_t i;
 
-    set_program_name(argv[0]);
+    SET_PROGRAM_NAME(argv[0]);
     vlog_set_levels(&VLM_lockfile, VLF_ANY_FACILITY, VLL_ERR);
 
     if (argc != 2) {
diff --git a/tests/test-multipath.c b/tests/test-multipath.c
index 03a666f..5861b61 100644
--- a/tests/test-multipath.c
+++ b/tests/test-multipath.c
@@ -36,7 +36,7 @@ main(int argc, char *argv[])
     bool ok = true;
     int n;
 
-    set_program_name(argv[0]);
+    SET_PROGRAM_NAME(argv[0]);
     random_init();
 
     if (argc != 2) {
diff --git a/tests/test-openflowd.c b/tests/test-openflowd.c
index b22b2aa..a209bdf 100644
--- a/tests/test-openflowd.c
+++ b/tests/test-openflowd.c
@@ -97,7 +97,7 @@ main(int argc, char *argv[])
     bool exiting;
 
     proctitle_init(argc, argv);
-    set_program_name(argv[0]);
+    SET_PROGRAM_NAME(argv[0]);
     parse_options(argc, argv, &s);
     signal(SIGPIPE, SIG_IGN);
 
@@ -429,7 +429,7 @@ parse_options(int argc, char *argv[], struct ofsettings *s)
             usage();
 
         case 'V':
-            OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
+            ovs_print_version(OFP_VERSION, OFP_VERSION);
             exit(EXIT_SUCCESS);
 
         DAEMON_OPTION_HANDLERS
diff --git a/tests/test-ovsdb.c b/tests/test-ovsdb.c
index 1cdf69a..f75f1b1 100644
--- a/tests/test-ovsdb.c
+++ b/tests/test-ovsdb.c
@@ -59,7 +59,7 @@ static void parse_options(int argc, char *argv[]);
 int
 main(int argc, char *argv[])
 {
-    set_program_name(argv[0]);
+    SET_PROGRAM_NAME(argv[0]);
     parse_options(argc, argv);
     run_command(argc - optind, argv + optind, all_commands);
     return 0;
diff --git a/tests/test-timeval.c b/tests/test-timeval.c
index 442b27a..c48ae5d 100644
--- a/tests/test-timeval.c
+++ b/tests/test-timeval.c
@@ -92,7 +92,7 @@ int
 main(int argc, char *argv[])
 {
     proctitle_init(argc, argv);
-    set_program_name(argv[0]);
+    SET_PROGRAM_NAME(argv[0]);
 
     if (argc != 2) {
         usage();
diff --git a/tests/test-unix-socket.c b/tests/test-unix-socket.c
index ec90048..8843f20 100644
--- a/tests/test-unix-socket.c
+++ b/tests/test-unix-socket.c
@@ -30,7 +30,7 @@ main(int argc, char *argv[])
     const char *sockname2;
     int sock1, sock2;
 
-    set_program_name(argv[0]);
+    SET_PROGRAM_NAME(argv[0]);
 
     if (argc != 2 && argc != 3) {
         ovs_fatal(0, "usage: %s SOCKETNAME1 [SOCKETNAME2]", argv[0]);
diff --git a/tests/test-vconn.c b/tests/test-vconn.c
index 2b14fa8..5c8c064 100644
--- a/tests/test-vconn.c
+++ b/tests/test-vconn.c
@@ -400,7 +400,7 @@ static const struct command commands[] = {
 int
 main(int argc, char *argv[])
 {
-    set_program_name(argv[0]);
+    SET_PROGRAM_NAME(argv[0]);
     vlog_set_levels(NULL, VLF_ANY_FACILITY, VLL_EMER);
     vlog_set_levels(NULL, VLF_CONSOLE, VLL_DBG);
     signal(SIGPIPE, SIG_IGN);
diff --git a/utilities/nlmon.c b/utilities/nlmon.c
index b6396d5..2df52df 100644
--- a/utilities/nlmon.c
+++ b/utilities/nlmon.c
@@ -42,7 +42,7 @@ main(int argc OVS_UNUSED, char *argv[])
     struct nl_sock *sock;
     int error;
 
-    set_program_name(argv[0]);
+    SET_PROGRAM_NAME(argv[0]);
     vlog_set_levels(NULL, VLF_ANY_FACILITY, VLL_DBG);
 
     error = nl_sock_create(NETLINK_ROUTE, &sock);
diff --git a/utilities/ovs-appctl.c b/utilities/ovs-appctl.c
index cd059bf..4e0fa31 100644
--- a/utilities/ovs-appctl.c
+++ b/utilities/ovs-appctl.c
@@ -44,7 +44,7 @@ main(int argc, char *argv[])
     char *reply;
     int i;
 
-    set_program_name(argv[0]);
+    SET_PROGRAM_NAME(argv[0]);
 
     /* Parse command line and connect to target. */
     target = parse_command_line(argc, argv);
@@ -147,7 +147,7 @@ parse_command_line(int argc, char *argv[])
             break;
 
         case 'V':
-            OVS_PRINT_VERSION(0, 0);
+            ovs_print_version(0, 0);
             exit(EXIT_SUCCESS);
 
         case '?':
diff --git a/utilities/ovs-benchmark.c b/utilities/ovs-benchmark.c
index e4e9225..4b92d91 100644
--- a/utilities/ovs-benchmark.c
+++ b/utilities/ovs-benchmark.c
@@ -69,7 +69,7 @@ time_in_msec(void)
 int
 main(int argc, char *argv[])
 {
-    set_program_name(argv[0]);
+    SET_PROGRAM_NAME(argv[0]);
     vlog_set_levels(NULL, VLF_ANY_FACILITY, VLL_EMER);
     parse_options(argc, argv);
     run_command(argc - optind, argv + optind, all_commands);
@@ -192,7 +192,7 @@ parse_options(int argc, char *argv[])
             usage();
 
         case 'V':
-            OVS_PRINT_VERSION(0, 0);
+            ovs_print_version(0, 0);
             exit(EXIT_SUCCESS);
 
         case '?':
diff --git a/utilities/ovs-controller.c b/utilities/ovs-controller.c
index 022b1a4..3b06d34 100644
--- a/utilities/ovs-controller.c
+++ b/utilities/ovs-controller.c
@@ -101,7 +101,7 @@ main(int argc, char *argv[])
     int i;
 
     proctitle_init(argc, argv);
-    set_program_name(argv[0]);
+    SET_PROGRAM_NAME(argv[0]);
     parse_options(argc, argv);
     signal(SIGPIPE, SIG_IGN);
 
@@ -397,7 +397,7 @@ parse_options(int argc, char *argv[])
             usage();
 
         case 'V':
-            OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
+            ovs_print_version(OFP_VERSION, OFP_VERSION);
             exit(EXIT_SUCCESS);
 
         VLOG_OPTION_HANDLERS
diff --git a/utilities/ovs-dpctl.c b/utilities/ovs-dpctl.c
index 415d276..f2dfa57 100644
--- a/utilities/ovs-dpctl.c
+++ b/utilities/ovs-dpctl.c
@@ -56,7 +56,7 @@ static void parse_options(int argc, char *argv[]);
 int
 main(int argc, char *argv[])
 {
-    set_program_name(argv[0]);
+    SET_PROGRAM_NAME(argv[0]);
     parse_options(argc, argv);
     signal(SIGPIPE, SIG_IGN);
     run_command(argc - optind, argv + optind, all_commands);
@@ -108,7 +108,7 @@ parse_options(int argc, char *argv[])
             usage();
 
         case 'V':
-            OVS_PRINT_VERSION(0, 0);
+            ovs_print_version(0, 0);
             exit(EXIT_SUCCESS);
 
         VLOG_OPTION_HANDLERS
diff --git a/utilities/ovs-ofctl.c b/utilities/ovs-ofctl.c
index 6be899b..632d3cf 100644
--- a/utilities/ovs-ofctl.c
+++ b/utilities/ovs-ofctl.c
@@ -74,7 +74,7 @@ static void parse_options(int argc, char *argv[]);
 int
 main(int argc, char *argv[])
 {
-    set_program_name(argv[0]);
+    SET_PROGRAM_NAME(argv[0]);
     parse_options(argc, argv);
     signal(SIGPIPE, SIG_IGN);
     run_command(argc - optind, argv + optind, all_commands);
@@ -138,7 +138,7 @@ parse_options(int argc, char *argv[])
             usage();
 
         case 'V':
-            OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
+            ovs_print_version(OFP_VERSION, OFP_VERSION);
             exit(EXIT_SUCCESS);
 
         case OPT_STRICT:
diff --git a/utilities/ovs-vlan-bug-workaround.c b/utilities/ovs-vlan-bug-workaround.c
index 309d64e..4416d0e 100644
--- a/utilities/ovs-vlan-bug-workaround.c
+++ b/utilities/ovs-vlan-bug-workaround.c
@@ -43,7 +43,7 @@ main(int argc, char *argv[])
     const char *netdev, *setting;
     int fd;
 
-    set_program_name(argv[0]);
+    SET_PROGRAM_NAME(argv[0]);
 
     parse_options(argc, argv);
     if (argc - optind != 2) {
@@ -130,7 +130,7 @@ parse_options(int argc, char *argv[])
             break;
 
         case 'V':
-            OVS_PRINT_VERSION(0, 0);
+            ovs_print_version(0, 0);
             exit(EXIT_SUCCESS);
 
         case '?':
diff --git a/utilities/ovs-vsctl.c b/utilities/ovs-vsctl.c
index b59d886..1308920 100644
--- a/utilities/ovs-vsctl.c
+++ b/utilities/ovs-vsctl.c
@@ -160,7 +160,7 @@ main(int argc, char *argv[])
     size_t n_commands;
     char *args;
 
-    set_program_name(argv[0]);
+    SET_PROGRAM_NAME(argv[0]);
     signal(SIGPIPE, SIG_IGN);
     vlog_set_levels(NULL, VLF_CONSOLE, VLL_WARN);
     vlog_set_levels(&VLM_reconnect, VLF_ANY_FACILITY, VLL_WARN);
@@ -262,7 +262,7 @@ parse_options(int argc, char *argv[])
             usage();
 
         case 'V':
-            OVS_PRINT_VERSION(0, 0);
+            ovs_print_version(0, 0);
             exit(EXIT_SUCCESS);
 
         case 't':
diff --git a/vswitchd/ovs-brcompatd.c b/vswitchd/ovs-brcompatd.c
index 3dd25c3..3b555b5 100644
--- a/vswitchd/ovs-brcompatd.c
+++ b/vswitchd/ovs-brcompatd.c
@@ -804,7 +804,7 @@ main(int argc, char *argv[])
     int retval;
 
     proctitle_init(argc, argv);
-    set_program_name(argv[0]);
+    SET_PROGRAM_NAME(argv[0]);
     vlog_set_levels(&VLM_reconnect, VLF_ANY_FACILITY, VLL_WARN);
 
     parse_options(argc, argv);
@@ -885,7 +885,7 @@ parse_options(int argc, char *argv[])
             usage();
 
         case 'V':
-            OVS_PRINT_VERSION(0, 0);
+            ovs_print_version(0, 0);
             exit(EXIT_SUCCESS);
 
         case OPT_APPCTL:
diff --git a/vswitchd/ovs-vswitchd.c b/vswitchd/ovs-vswitchd.c
index 7d4e4d7..74cdfc7 100644
--- a/vswitchd/ovs-vswitchd.c
+++ b/vswitchd/ovs-vswitchd.c
@@ -66,7 +66,7 @@ main(int argc, char *argv[])
     int retval;
 
     proctitle_init(argc, argv);
-    set_program_name(argv[0]);
+    SET_PROGRAM_NAME(argv[0]);
     stress_init_command();
     remote = parse_options(argc, argv);
     signal(SIGPIPE, SIG_IGN);
@@ -151,7 +151,7 @@ parse_options(int argc, char *argv[])
             usage();
 
         case 'V':
-            OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
+            ovs_print_version(OFP_VERSION, OFP_VERSION);
             exit(EXIT_SUCCESS);
 
         case OPT_MLOCKALL:
-- 
1.7.1




More information about the dev mailing list