Make installation directories overridable at runtime.
authorBen Pfaff <blp@nicira.com>
Mon, 29 Nov 2010 20:28:26 +0000 (12:28 -0800)
committerBen Pfaff <blp@nicira.com>
Tue, 30 Nov 2010 00:29:11 +0000 (16:29 -0800)
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.

16 files changed:
lib/automake.mk
lib/daemon.c
lib/dirs.c.in [new file with mode: 0644]
lib/dirs.h
lib/unixctl.c
lib/vlog.c
python/ovs/automake.mk
python/ovs/dirs.py
utilities/ovs-appctl.c
utilities/ovs-discover.c
utilities/ovs-ofctl.c
utilities/ovs-openflowd.c
utilities/ovs-vsctl.c
vswitchd/bridge.c
vswitchd/ovs-brcompatd.c
vswitchd/system-stats.c

index 5cd4722..0750910 100644 (file)
@@ -208,7 +208,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 \
@@ -230,12 +231,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
index c6489cd..c0f1682 100644 (file)
@@ -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 (file)
index 0000000..a174ab3
--- /dev/null
@@ -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; NULL if not yet determined. */
+    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);
+}
index ea5e3b5..3035305 100644 (file)
@@ -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 */
index e10de49..161374e 100644 (file)
@@ -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)
 \f
 /* 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++);
 
index 377aaa6..2598111 100644 (file)
@@ -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);
 }
index 5c10c2a..4a1e7c1 100644 (file)
@@ -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
index f8e7308..5b006cc 100644 (file)
@@ -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")
index 021ac84..dc45742 100644 (file)
@@ -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);
     }
index 0feaa0f..a89ebc5 100644 (file)
@@ -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);
 }
index 7ed159a..65e2a9f 100644 (file)
@@ -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);
index 75413f3..da3a260 100644 (file)
@@ -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]);
index 56cb745..477bdb0 100644 (file)
@@ -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;
 }
index 7dbd378..0103eb9 100644 (file)
@@ -1735,7 +1735,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)) {
@@ -1755,7 +1755,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;
index b61dc10..ecf00b3 100644 (file)
@@ -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;
 
index 45b8cce..9c5a25c 100644 (file)
@@ -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)) {