You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by id...@apache.org on 2014/05/21 01:02:37 UTC

[2/5] git commit: Add ExecEnv for use with execle to safely exec.

Add ExecEnv for use with execle to safely exec.

execle() should be used with envp to set a child's environment variables
in an async safe manner.

Review: https://reviews.apache.org/r/20956


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/2482788c
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/2482788c
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/2482788c

Branch: refs/heads/master
Commit: 2482788c4f01bd0df1574d8689ba265f5b6c7322
Parents: c647c4c
Author: Ian Downes <id...@twitter.com>
Authored: Wed Apr 30 15:22:44 2014 -0700
Committer: Ian Downes <id...@twitter.com>
Committed: Tue May 20 15:25:23 2014 -0700

----------------------------------------------------------------------
 3rdparty/libprocess/3rdparty/stout/Makefile.am  |  1 +
 .../3rdparty/stout/include/stout/os/execenv.hpp | 98 ++++++++++++++++++++
 2 files changed, 99 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/2482788c/3rdparty/libprocess/3rdparty/stout/Makefile.am
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/Makefile.am b/3rdparty/libprocess/3rdparty/stout/Makefile.am
index a9269ad..ea3049f 100644
--- a/3rdparty/libprocess/3rdparty/stout/Makefile.am
+++ b/3rdparty/libprocess/3rdparty/stout/Makefile.am
@@ -13,6 +13,7 @@ EXTRA_DIST =					\
   include/stout/duration.hpp			\
   include/stout/dynamiclibrary.hpp		\
   include/stout/error.hpp			\
+  include/stout/execenv.hpp			\
   include/stout/exit.hpp			\
   include/stout/fatal.hpp			\
   include/stout/flags.hpp			\

http://git-wip-us.apache.org/repos/asf/mesos/blob/2482788c/3rdparty/libprocess/3rdparty/stout/include/stout/os/execenv.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/os/execenv.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/os/execenv.hpp
new file mode 100644
index 0000000..1dd6c90
--- /dev/null
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/os/execenv.hpp
@@ -0,0 +1,98 @@
+/**
+ * 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.
+ */
+#ifndef __STOUT_OS_ENVP_HPP__
+#define __STOUT_OS_ENVP_HPP__
+
+#include <map>
+
+#include <stout/hashmap.hpp>
+#include <stout/os.hpp>
+
+namespace os {
+
+// Used to build an environment suitable for execle()
+class ExecEnv
+{
+public:
+  explicit ExecEnv(const std::map<std::string, std::string>& environment);
+  ~ExecEnv();
+
+  ExecEnv(const ExecEnv&);
+
+  char** operator () () const { return envp; }
+
+private:
+  // Not default constructable and not assignable.
+  ExecEnv();
+  ExecEnv& operator = (const ExecEnv&);
+
+  char** envp;
+  size_t size;
+};
+
+
+inline ExecEnv::ExecEnv(const std::map<std::string, std::string>& _environment)
+  : envp(NULL),
+    size(0)
+{
+  // Merge passed environment with OS environment, overriding where necessary.
+  hashmap<std::string, std::string> environment = os::environment();
+
+  foreachpair (const std::string& key, const std::string& value, _environment) {
+    environment[key] = value;
+  }
+
+  size = environment.size();
+
+  // Convert environment to internal representation.
+  // Add 1 to the size for a NULL terminator.
+  envp = new char*[size + 1];
+  int index = 0;
+  foreachpair (const std::string& key, const std::string& value, environment) {
+    std::string entry = key + "=" + value;
+    envp[index] = new char[entry.size() + 1];
+    strncpy(envp[index], entry.c_str(), entry.size() + 1);
+    ++index;
+  }
+
+  envp[index] = NULL;
+}
+
+
+inline ExecEnv::~ExecEnv()
+{
+  for (size_t i = 0; i < size; ++i) {
+    delete[] envp[i];
+  }
+  delete[] envp;
+  envp = NULL;
+}
+
+
+inline ExecEnv::ExecEnv(const ExecEnv& other)
+{
+  size = other.size;
+
+  envp = new char*[size + 1];
+  for (size_t i = 0; i < size; ++i) {
+    envp[i] = new char[strlen(other.envp[i]) + 1];
+    strncpy(envp[i], other.envp[i], strlen(other.envp[i]) + 1);
+  }
+
+  envp[size] = NULL;
+}
+
+}  // namespace os {
+
+#endif // __STOUT_OS_ENVP_HPP__