summaryrefslogtreecommitdiffstats
path: root/o3d
diff options
context:
space:
mode:
authortschmelcher@chromium.org <tschmelcher@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-11-03 06:04:44 +0000
committertschmelcher@chromium.org <tschmelcher@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-11-03 06:04:44 +0000
commit72cc3de9c7bcb2112d4bd99d228fad97f966c706 (patch)
treeca11d673dec742c4c1652d6589869cf4196a193b /o3d
parent9697c5abfad683112288d367f07bf68d7eb9a05a (diff)
downloadchromium_src-72cc3de9c7bcb2112d4bd99d228fad97f966c706.zip
chromium_src-72cc3de9c7bcb2112d4bd99d228fad97f966c706.tar.gz
chromium_src-72cc3de9c7bcb2112d4bd99d228fad97f966c706.tar.bz2
Linux: Add support for loading environment variables at plugin start-up from a file on disk. This lets us set any needed environment variables in a cross-browser way (i.e., without trying to modify the browsers' program launcher scripts to set variables for us). This is needed on some 64-bit systems where an environment variable has to be set in order for MESA to know where to find its GPU drivers.
BUG=none TEST=built, created envvars file, and put something in it; verified that it was set successfully Review URL: http://codereview.chromium.org/340044 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@30799 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'o3d')
-rw-r--r--o3d/plugin/linux/envvars.cc83
-rw-r--r--o3d/plugin/linux/envvars.h43
-rw-r--r--o3d/plugin/linux/main_linux.cc14
-rw-r--r--o3d/plugin/plugin.gyp8
4 files changed, 148 insertions, 0 deletions
diff --git a/o3d/plugin/linux/envvars.cc b/o3d/plugin/linux/envvars.cc
new file mode 100644
index 0000000..684d1ee
--- /dev/null
+++ b/o3d/plugin/linux/envvars.cc
@@ -0,0 +1,83 @@
+/*
+ * Copyright 2009, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+#include "plugin/linux/envvars.h"
+
+#include "base/logging.h"
+
+namespace o3d {
+
+static const unsigned int kMaxEnvVarLength = 1024;
+
+void LoadEnvironmentVariablesFile(const char *path) {
+ FILE *env_vars_file = fopen(path, "r");
+ // It is not an error if the file is missing. We treat it the same as if it
+ // were empty.
+ if (env_vars_file) {
+ char buffer[kMaxEnvVarLength];
+ while (!feof(env_vars_file) ) {
+ if (fgets(buffer, sizeof(buffer), env_vars_file)) {
+ char *equals = strchr(buffer, '=');
+ if (!equals) {
+ LOG(ERROR) << "Malformed environment variables file";
+ continue;
+ }
+ char *value = equals + 1;
+ *equals = '\0';
+ int length = strlen(value);
+ if (value[length - 1] == '\n') {
+ value[length - 1] = '\0';
+ }
+ if (setenv(buffer, value, 1) != 0) {
+ LOG(ERROR) << "Couldn't add " << buffer << "=" << value
+ << " to environment";
+ } else {
+ // Custom environment variables could be relevant for debugging
+ // problem reports, so log this too.
+ LOG(INFO) << "Defined " << buffer << "=" << value;
+ }
+ } else if (ferror(env_vars_file)) {
+ PLOG(ERROR) << "Error reading from environment variables file";
+ break;
+ } else {
+ break;
+ }
+ }
+ if (fclose(env_vars_file) != 0) {
+ PLOG(ERROR) << "Unable to close environment variables file";
+ }
+ }
+}
+
+} // namespace o3d
diff --git a/o3d/plugin/linux/envvars.h b/o3d/plugin/linux/envvars.h
new file mode 100644
index 0000000..d1d3e30
--- /dev/null
+++ b/o3d/plugin/linux/envvars.h
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2009, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef O3D_PLUGIN_LINUX_ENVVARS_H_
+#define O3D_PLUGIN_LINUX_ENVVARS_H_
+
+namespace o3d {
+
+// Loads a list of environment variables into the current process from a text
+// file. Each line in the file must be a NAME=VALUE pair.
+void LoadEnvironmentVariablesFile(const char *path);
+
+} // namespace o3d
+
+#endif // O3D_PLUGIN_LINUX_ENVVARS_H_
diff --git a/o3d/plugin/linux/main_linux.cc b/o3d/plugin/linux/main_linux.cc
index 594d255..40f6c1b 100644
--- a/o3d/plugin/linux/main_linux.cc
+++ b/o3d/plugin/linux/main_linux.cc
@@ -42,6 +42,7 @@
#include "base/scoped_ptr.h"
#include "plugin/cross/main.h"
#include "plugin/cross/out_of_memory.h"
+#include "plugin/linux/envvars.h"
using glue::_o3d::PluginObject;
using glue::StreamManager;
@@ -59,6 +60,9 @@ base::AtExitManager g_at_exit_manager;
bool g_xembed_support = false;
+// This is a #define set on the build command-line for greater flexibility.
+static const char *kEnvVarsFilePath = O3D_PLUGIN_ENV_VARS_FILE;
+
static void DrawPlugin(PluginObject *obj) {
// Limit drawing to no more than once every timer tick.
if (!obj->draw_) return;
@@ -624,6 +628,16 @@ NPError InitializePlugin() {
DLOG(INFO) << "NP_Initialize";
+ // Before doing anything more, we first load our environment variables file.
+ // This file is a newline-delimited list of any system-specific environment
+ // variables that need to be set in the browser. Since we are a shared library
+ // and not an executable, we can't set them at browser start time, so we
+ // instead set them in every process that loads our shared library. It is
+ // important that we do this as early as possible so that any relevant
+ // variables are already set when we initialize our shared library
+ // dependencies.
+ o3d::LoadEnvironmentVariablesFile(kEnvVarsFilePath);
+
// Check for XEmbed support in the browser.
NPBool xembed_support = 0;
NPError err = NPN_GetValue(NULL, NPNVSupportsXEmbedBool, &xembed_support);
diff --git a/o3d/plugin/plugin.gyp b/o3d/plugin/plugin.gyp
index 78b29c3..15ebabd 100644
--- a/o3d/plugin/plugin.gyp
+++ b/o3d/plugin/plugin.gyp
@@ -194,12 +194,16 @@
'sources': [
'linux/main_linux.cc',
'linux/config.cc',
+ 'linux/envvars.cc',
],
'link_settings': {
'libraries': [
'-lGL',
],
},
+ 'defines': [
+ 'O3D_PLUGIN_ENV_VARS_FILE="/opt/google/o3d/envvars"',
+ ],
},
],
['OS == "win"',
@@ -335,12 +339,16 @@
'sources': [
'linux/main_linux.cc',
'linux/config.cc',
+ 'linux/envvars.cc',
],
'link_settings': {
'libraries': [
'-lGL',
],
},
+ 'defines': [
+ 'O3D_PLUGIN_ENV_VARS_FILE="/opt/google/o3d/envvars"',
+ ],
# On Linux, shared library targets aren't copied to the
# product dir automatically. Filed GYP issue #74 to address this.
# TODO(gspencer): Remove when issue #74 is resolved.