You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by ji...@apache.org on 2016/10/12 00:00:03 UTC
[2/4] mesos git commit: Added an abstraction for Envp pointer
expected by exec routines.
Added an abstraction for Envp pointer expected by exec routines.
Review: https://reviews.apache.org/r/52730
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/c43c54aa
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/c43c54aa
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/c43c54aa
Branch: refs/heads/master
Commit: c43c54aa97ebad486f04094ae79a59a4c95c10ca
Parents: 90ad84b
Author: Jie Yu <yu...@gmail.com>
Authored: Mon Oct 10 22:23:11 2016 -0700
Committer: Jie Yu <yu...@gmail.com>
Committed: Tue Oct 11 16:59:31 2016 -0700
----------------------------------------------------------------------
.../stout/include/stout/os/raw/environment.hpp | 96 ++++++++++++++++++++
1 file changed, 96 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mesos/blob/c43c54aa/3rdparty/stout/include/stout/os/raw/environment.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/stout/os/raw/environment.hpp b/3rdparty/stout/include/stout/os/raw/environment.hpp
index 80cc45b..b3e82ac 100644
--- a/3rdparty/stout/include/stout/os/raw/environment.hpp
+++ b/3rdparty/stout/include/stout/os/raw/environment.hpp
@@ -13,6 +13,14 @@
#ifndef __STOUT_OS_RAW_ENVIRONMENT_HPP__
#define __STOUT_OS_RAW_ENVIRONMENT_HPP__
+#include <string.h>
+
+#include <string>
+
+#include <stout/foreach.hpp>
+#include <stout/json.hpp>
+#include <stout/stringify.hpp>
+
#ifdef __APPLE__
#include <crt_externs.h> // For _NSGetEnviron().
#elif !defined(__WINDOWS__)
@@ -73,6 +81,94 @@ inline char*** environmentp()
#endif
}
+
+// Represents the environment variable list expected by 'exec'
+// routines. The environment variable list is an array of pointers
+// that point to null-terminated strings. The array of pointers must
+// be terminated by a nullptr. To use this abstraction, see the
+// following example:
+//
+// map<string, string> environment = {
+// {"key1", "value1"},
+// {"key2", "value2"}
+// };
+// os::raw::Envp envp(environment);
+// execle("/bin/sh", "sh", "-c", "echo hello", envp);
+class Envp
+{
+public:
+ Envp(Envp&& that)
+ : envp(that.envp),
+ size(that.size)
+ {
+ that.envp = nullptr;
+ that.size = 0;
+ }
+
+ template <typename Map>
+ explicit Envp(const Map& map)
+ {
+ size = map.size();
+
+ // NOTE: We add 1 to the size for a `nullptr` terminator.
+ envp = new char*[size + 1];
+ size_t index = 0;
+
+ for (auto it = map.begin(); it != map.end(); ++it) {
+ std::string entry = stringify(it->first) + "=" + stringify(it->second);
+ envp[index] = new char[entry.size() + 1];
+ ::memcpy(envp[index], entry.c_str(), entry.size() + 1);
+ ++index;
+ }
+
+ envp[index] = nullptr;
+ }
+
+ explicit Envp(const JSON::Object& object)
+ {
+ size = object.values.size();
+
+ // NOTE: We add 1 to the size for a `nullptr` terminator.
+ envp = new char*[size + 1];
+ size_t index = 0;
+
+ foreachpair (const std::string& key,
+ const JSON::Value& value,
+ object.values) {
+ std::string entry = key + "=" + value.as<JSON::String>().value;
+ envp[index] = new char[entry.size() + 1];
+ ::memcpy(envp[index], entry.c_str(), entry.size() + 1);
+ ++index;
+ }
+
+ envp[index] = nullptr;
+ }
+
+ ~Envp()
+ {
+ if (envp == nullptr) {
+ return;
+ }
+
+ for (size_t i = 0; i < size; i++) {
+ delete[] envp[i];
+ }
+ delete[] envp;
+ }
+
+ operator char**()
+ {
+ return envp;
+ }
+
+private:
+ Envp(const Envp&) = delete;
+ Envp& operator=(const Envp&) = delete;
+
+ char **envp;
+ size_t size;
+};
+
} // namespace raw {
} // namespace os {