[ovs-dev] [ofproto tests 12/29] Make installation directories overridable at runtime.

Ben Pfaff blp at nicira.com
Tue Nov 16 19:21:02 UTC 2010


This makes it possible to run tests that need access to installation
directories, such as the rundir, without having access to the actual
installation directories (/var/run is generally not world-writable), by
setting environment variables.  This is not a good way to do things in
general--usually it would be better to choose the correct directories
at configure time--so for now this is undocumented.
---
 lib/automake.mk           |   17 +++++++-----
 lib/daemon.c              |    6 ++--
 lib/dirs.c.in             |   66 +++++++++++++++++++++++++++++++++++++++++++++
 lib/dirs.h                |   10 +++---
 lib/unixctl.c             |    8 +++---
 lib/vlog.c                |    4 +-
 python/ovs/automake.mk    |   10 ++++---
 python/ovs/dirs.py        |    9 +++---
 utilities/ovs-appctl.c    |    4 +-
 utilities/ovs-discover.c  |    2 +-
 utilities/ovs-ofctl.c     |    4 +-
 utilities/ovs-openflowd.c |    4 +-
 utilities/ovs-vsctl.c     |    2 +-
 vswitchd/bridge.c         |    4 +-
 vswitchd/ovs-brcompatd.c  |    2 +-
 vswitchd/system-stats.c   |    6 ++--
 16 files changed, 115 insertions(+), 43 deletions(-)
 create mode 100644 lib/dirs.c.in

diff --git a/lib/automake.mk b/lib/automake.mk
index b5135ec..20cf997 100644
--- a/lib/automake.mk
+++ b/lib/automake.mk
@@ -206,7 +206,8 @@ endif
 EXTRA_DIST += \
 	lib/dh1024.pem \
 	lib/dh2048.pem \
-	lib/dh4096.pem
+	lib/dh4096.pem \
+	lib/dirs.c.in
 
 EXTRA_DIST += \
 	lib/common.man \
@@ -228,12 +229,14 @@ EXTRA_DIST += \
 	lib/vlog-syn.man \
 	lib/vlog.man
 
-lib/dirs.c: Makefile
-	($(ro_c) && \
-	 echo 'const char ovs_pkgdatadir[] = "$(pkgdatadir)";' && \
-	 echo 'const char ovs_rundir[] = "@RUNDIR@";' && \
-	 echo 'const char ovs_logdir[] = "@LOGDIR@";' && \
-	 echo 'const char ovs_bindir[] = "$(bindir)";') > lib/dirs.c.tmp
+lib/dirs.c: lib/dirs.c.in Makefile
+	($(ro_c) && sed < $(srcdir)/lib/dirs.c.in \
+		-e 's,[@]srcdir[@],$(srcdir),g' \
+		-e 's,[@]LOGDIR[@],"$(LOGDIR)",g' \
+		-e 's,[@]RUNDIR[@],"$(RUNDIR)",g' \
+		-e 's,[@]bindir[@],"$(bindir)",g' \
+		-e 's,[@]pkgdatadir[@],"$(pkgdatadir)",g') \
+	     > lib/dirs.c.tmp
 	mv lib/dirs.c.tmp lib/dirs.c
 
 install-data-local: lib-install-data-local
diff --git a/lib/daemon.c b/lib/daemon.c
index c6489cd..c0f1682 100644
--- a/lib/daemon.c
+++ b/lib/daemon.c
@@ -67,8 +67,8 @@ char *
 make_pidfile_name(const char *name)
 {
     return (!name
-            ? xasprintf("%s/%s.pid", ovs_rundir, program_name)
-            : abs_file_name(ovs_rundir, name));
+            ? xasprintf("%s/%s.pid", ovs_rundir(), program_name)
+            : abs_file_name(ovs_rundir(), name));
 }
 
 /* Sets up a following call to daemonize() to create a pidfile named 'name'.
@@ -500,7 +500,7 @@ daemon_usage(void)
         "  --pidfile[=FILE]        create pidfile (default: %s/%s.pid)\n"
         "  --overwrite-pidfile     with --pidfile, start even if already "
                                    "running\n",
-        ovs_rundir, program_name);
+        ovs_rundir(), program_name);
 }
 
 /* Opens and reads a PID from 'pidfile'.  Returns the nonnegative PID if
diff --git a/lib/dirs.c.in b/lib/dirs.c.in
new file mode 100644
index 0000000..e360f6c
--- /dev/null
+++ b/lib/dirs.c.in
@@ -0,0 +1,66 @@
+#line 2 "@srcdir@/lib/dirs.c.in"
+/*
+ * Copyright (c) 2008, 2009, 2010 Nicira Networks.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <config.h>
+#include "dirs.h"
+#include <stdlib.h>
+
+struct directory {
+    const char *value;          /* Actual value or NULL if not known yet. */
+    const char *default_value;  /* Default value. */
+    const char *var_name;       /* Environment variable to override default. */
+};
+
+static const char *
+get_dir(struct directory *d)
+{
+    if (!d->value) {
+        d->value = getenv(d->var_name);
+        if (!d->value || !d->value[0]) {
+            d->value = d->default_value;
+        }
+    }
+    return d->value;
+}
+
+const char *
+ovs_pkgdatadir(void)
+{
+    static struct directory d = { NULL, @pkgdatadir@, "OVS_PKGDATADIR" };
+    return get_dir(&d);
+}
+
+const char *
+ovs_rundir(void)
+{
+    static struct directory d = { NULL, @RUNDIR@, "OVS_RUNDIR" };
+    return get_dir(&d);
+}
+
+const char *
+ovs_logdir(void)
+{
+    static struct directory d = { NULL, @LOGDIR@, "OVS_LOGDIR" };
+    return get_dir(&d);
+}
+
+const char *
+ovs_bindir(void)
+{
+    static struct directory d = { NULL, @bindir@, "OVS_BINDIR" };
+    return get_dir(&d);
+}
diff --git a/lib/dirs.h b/lib/dirs.h
index ea5e3b5..3035305 100644
--- a/lib/dirs.h
+++ b/lib/dirs.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2009 Nicira Networks.
+ * Copyright (c) 2008, 2009, 2010 Nicira Networks.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -17,9 +17,9 @@
 #ifndef DIRS_H
 #define DIRS_H 1
 
-extern const char ovs_pkgdatadir[]; /* /usr/local/share/openvswitch */
-extern const char ovs_rundir[];     /* /usr/local/var/run/openvswitch */
-extern const char ovs_logdir[];     /* /usr/local/var/log/openvswitch */
-extern const char ovs_bindir[];     /* /usr/local/bin */
+const char *ovs_pkgdatadir(void); /* /usr/local/share/openvswitch */
+const char *ovs_rundir(void);     /* /usr/local/var/run/openvswitch */
+const char *ovs_logdir(void);     /* /usr/local/var/log/openvswitch */
+const char *ovs_bindir(void);     /* /usr/local/bin */
 
 #endif /* dirs.h */
diff --git a/lib/unixctl.c b/lib/unixctl.c
index e10de49..161374e 100644
--- a/lib/unixctl.c
+++ b/lib/unixctl.c
@@ -208,9 +208,9 @@ unixctl_server_create(const char *path, struct unixctl_server **serverp)
     list_init(&server->conns);
 
     if (path) {
-        server->path = abs_file_name(ovs_rundir, path);
+        server->path = abs_file_name(ovs_rundir(), path);
     } else {
-        server->path = xasprintf("%s/%s.%ld.ctl", ovs_rundir,
+        server->path = xasprintf("%s/%s.%ld.ctl", ovs_rundir(),
                                  program_name, (long int) getpid());
     }
 
@@ -471,7 +471,7 @@ unixctl_server_destroy(struct unixctl_server *server)
 
 /* Connects to a Vlog server socket.  'path' should be the name of a Vlog
  * server socket.  If it does not start with '/', it will be prefixed with
- * ovs_rundir (e.g. /var/run/openvswitch).
+ * the rundir (e.g. /usr/local/var/run/openvswitch).
  *
  * Returns 0 if successful, otherwise a positive errno value.  If successful,
  * sets '*clientp' to the new unixctl_client, otherwise to NULL. */
@@ -485,7 +485,7 @@ unixctl_client_create(const char *path, struct unixctl_client **clientp)
 
     /* Determine location. */
     client = xmalloc(sizeof *client);
-    client->connect_path = abs_file_name(ovs_rundir, path);
+    client->connect_path = abs_file_name(ovs_rundir(), path);
     client->bind_path = xasprintf("/tmp/vlog.%ld.%d",
                                   (long int) getpid(), counter++);
 
diff --git a/lib/vlog.c b/lib/vlog.c
index 377aaa6..2598111 100644
--- a/lib/vlog.c
+++ b/lib/vlog.c
@@ -291,7 +291,7 @@ vlog_set_log_file(const char *file_name)
     old_log_file_name = log_file_name;
     log_file_name = (file_name
                      ? xstrdup(file_name)
-                     : xasprintf("%s/%s.log", ovs_logdir, program_name));
+                     : xasprintf("%s/%s.log", ovs_logdir(), program_name));
     free(old_log_file_name);
     file_name = NULL;           /* Might have been freed. */
 
@@ -741,5 +741,5 @@ vlog_usage(void)
            "  -v, --verbose           set maximum verbosity level\n"
            "  --log-file[=FILE]       enable logging to specified FILE\n"
            "                          (default: %s/%s.log)\n",
-           ovs_logdir, program_name);
+           ovs_logdir(), program_name);
 }
diff --git a/python/ovs/automake.mk b/python/ovs/automake.mk
index 5c10c2a..4a1e7c1 100644
--- a/python/ovs/automake.mk
+++ b/python/ovs/automake.mk
@@ -27,10 +27,12 @@ if HAVE_PYTHON
 nobase_pkgdata_DATA = $(ovs_pyfiles)
 ovs-install-data-local:
 	$(MKDIR_P) python/ovs
-	(echo 'PKGDATADIR = """$(pkgdatadir)"""' && \
-	 echo 'RUNDIR = """@RUNDIR@"""' && \
-	 echo 'LOGDIR = """@LOGDIR@"""' && \
-	 echo 'BINDIR = """$(bindir)"""') > python/ovs/dirs.py.tmp
+	(echo "import os" && \
+	 echo 'PKGDATADIR = os.environ.get("OVS_PKGDATADIR", """$(pkgdatadir)""")' && \
+	 echo 'RUNDIR = os.environ.get("OVS_RUNDIR", """@RUNDIR@""")' && \
+	 echo 'LOGDIR = os.environ.get("OVS_LOGDIR", """@LOGDIR@""")' && \
+	 echo 'BINDIR = os.environ.get("OVS_BINDIR", """$(bindir)""")') \
+		> python/ovs/dirs.py.tmp
 	$(MKDIR_P) $(DESTDIR)$(pkgdatadir)/python/ovs
 	$(INSTALL_DATA) python/ovs/dirs.py.tmp $(DESTDIR)$(pkgdatadir)/python/ovs/dirs.py
 	rm python/ovs/dirs.py.tmp
diff --git a/python/ovs/dirs.py b/python/ovs/dirs.py
index f8e7308..5b006cc 100644
--- a/python/ovs/dirs.py
+++ b/python/ovs/dirs.py
@@ -1,7 +1,8 @@
 # These are the default directories.  They will be replaced by the
 # configured directories at install time.
 
-PKGDATADIR = "/usr/local/share/openvswitch"
-RUNDIR = "/var/run"
-LOGDIR = "/usr/local/var/log"
-BINDIR = "/usr/local/bin"
+import os
+PKGDATADIR = os.environ.get("OVS_PKGDATADIR", "/usr/local/share/openvswitch")
+RUNDIR = os.environ.get("OVS_RUNDIR", "/var/run")
+LOGDIR = os.environ.get("OVS_LOGDIR", "/usr/local/var/log")
+BINDIR = os.environ.get("OVS_BINDIR", "/usr/local/bin")
diff --git a/utilities/ovs-appctl.c b/utilities/ovs-appctl.c
index 021ac84..dc45742 100644
--- a/utilities/ovs-appctl.c
+++ b/utilities/ovs-appctl.c
@@ -175,14 +175,14 @@ connect_to_target(const char *target)
         char *pidfile_name;
         pid_t pid;
 
-        pidfile_name = xasprintf("%s/%s.pid", ovs_rundir, target);
+        pidfile_name = xasprintf("%s/%s.pid", ovs_rundir(), target);
         pid = read_pidfile(pidfile_name);
         if (pid < 0) {
             ovs_fatal(-pid, "cannot read pidfile \"%s\"", pidfile_name);
         }
         free(pidfile_name);
         socket_name = xasprintf("%s/%s.%ld.ctl",
-                                ovs_rundir, target, (long int) pid);
+                                ovs_rundir(), target, (long int) pid);
     } else {
         socket_name = xstrdup(target);
     }
diff --git a/utilities/ovs-discover.c b/utilities/ovs-discover.c
index 0feaa0f..a89ebc5 100644
--- a/utilities/ovs-discover.c
+++ b/utilities/ovs-discover.c
@@ -397,6 +397,6 @@ usage(void)
                                       "running\n"
            "  -h, --help              display this help message\n"
            "  -V, --version           display version information\n",
-           ovs_rundir, program_name);
+           ovs_rundir(), program_name);
     exit(EXIT_SUCCESS);
 }
diff --git a/utilities/ovs-ofctl.c b/utilities/ovs-ofctl.c
index be68bdc..232eecd 100644
--- a/utilities/ovs-ofctl.c
+++ b/utilities/ovs-ofctl.c
@@ -217,7 +217,7 @@ open_vconn__(const char *name, const char *default_suffix,
     struct stat s;
     char *bridge_path, *datapath_name, *datapath_type;
 
-    bridge_path = xasprintf("%s/%s.%s", ovs_rundir, name, default_suffix);
+    bridge_path = xasprintf("%s/%s.%s", ovs_rundir(), name, default_suffix);
     dp_parse_name(name, &datapath_name, &datapath_type);
 
     if (strstr(name, ":")) {
@@ -239,7 +239,7 @@ open_vconn__(const char *name, const char *default_suffix,
         }
 
         socket_name = xasprintf("%s/%s.%s",
-                                ovs_rundir, dpif_name, default_suffix);
+                                ovs_rundir(), dpif_name, default_suffix);
         if (stat(socket_name, &s)) {
             ovs_fatal(errno, "cannot connect to %s: stat failed on %s",
                       name, socket_name);
diff --git a/utilities/ovs-openflowd.c b/utilities/ovs-openflowd.c
index 75413f3..da3a260 100644
--- a/utilities/ovs-openflowd.c
+++ b/utilities/ovs-openflowd.c
@@ -444,8 +444,8 @@ parse_options(int argc, char *argv[], struct ofsettings *s)
 
     /* Figure out controller names. */
     if (!controllers.n) {
-        svec_add_nocopy(&controllers,
-                        xasprintf("punix:%s/%s.mgmt", ovs_rundir, s->dp_name));
+        svec_add_nocopy(&controllers, xasprintf("punix:%s/%s.mgmt",
+                                                ovs_rundir(), s->dp_name));
     }
     for (i = 1; i < argc; i++) {
         svec_add(&controllers, argv[i]);
diff --git a/utilities/ovs-vsctl.c b/utilities/ovs-vsctl.c
index ec1cfb3..4ff7101 100644
--- a/utilities/ovs-vsctl.c
+++ b/utilities/ovs-vsctl.c
@@ -531,7 +531,7 @@ default_db(void)
 {
     static char *def;
     if (!def) {
-        def = xasprintf("unix:%s/db.sock", ovs_rundir);
+        def = xasprintf("unix:%s/db.sock", ovs_rundir());
     }
     return def;
 }
diff --git a/vswitchd/bridge.c b/vswitchd/bridge.c
index 13e897f..5652392 100644
--- a/vswitchd/bridge.c
+++ b/vswitchd/bridge.c
@@ -1623,7 +1623,7 @@ bridge_reconfigure_one(struct bridge *br)
     /* Configure OpenFlow controller connection snooping. */
     svec_init(&snoops);
     svec_add_nocopy(&snoops, xasprintf("punix:%s/%s.snoop",
-                                       ovs_rundir, br->name));
+                                       ovs_rundir(), br->name));
     svec_init(&old_snoops);
     ofproto_get_snoops(br->ofproto, &old_snoops);
     if (!svec_equal(&snoops, &old_snoops)) {
@@ -1643,7 +1643,7 @@ static void
 bridge_ofproto_controller_for_mgmt(const struct bridge *br,
                                    struct ofproto_controller *oc)
 {
-    oc->target = xasprintf("punix:%s/%s.mgmt", ovs_rundir, br->name);
+    oc->target = xasprintf("punix:%s/%s.mgmt", ovs_rundir(), br->name);
     oc->max_backoff = 0;
     oc->probe_interval = 60;
     oc->band = OFPROTO_OUT_OF_BAND;
diff --git a/vswitchd/ovs-brcompatd.c b/vswitchd/ovs-brcompatd.c
index b61dc10..ecf00b3 100644
--- a/vswitchd/ovs-brcompatd.c
+++ b/vswitchd/ovs-brcompatd.c
@@ -1413,7 +1413,7 @@ parse_options(int argc, char *argv[])
     };
     char *short_options = long_options_to_short_options(long_options);
 
-    appctl_command = xasprintf("%s/ovs-appctl %%s", ovs_bindir);
+    appctl_command = xasprintf("%s/ovs-appctl %%s", ovs_bindir());
     for (;;) {
         int c;
 
diff --git a/vswitchd/system-stats.c b/vswitchd/system-stats.c
index 45b8cce..9c5a25c 100644
--- a/vswitchd/system-stats.c
+++ b/vswitchd/system-stats.c
@@ -387,9 +387,9 @@ get_process_stats(struct shash *stats)
     struct dirent *de;
     DIR *dir;
 
-    dir = opendir(ovs_rundir);
+    dir = opendir(ovs_rundir());
     if (!dir) {
-        VLOG_ERR_ONCE("%s: open failed (%s)", ovs_rundir, strerror(errno));
+        VLOG_ERR_ONCE("%s: open failed (%s)", ovs_rundir(), strerror(errno));
         return;
     }
 
@@ -411,7 +411,7 @@ get_process_stats(struct shash *stats)
             continue;
         }
 
-        file_name = xasprintf("%s/%s", ovs_rundir, de->d_name);
+        file_name = xasprintf("%s/%s", ovs_rundir(), de->d_name);
         pid = read_pidfile(file_name);
         free(file_name);
         if (pid < 0 || kill(pid, 0)) {
-- 
1.7.1





More information about the dev mailing list