2 * Copyright 2017 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <folly/experimental/EnvUtil.h>
19 #include <folly/String.h>
20 #include <folly/portability/Stdlib.h>
21 #include <folly/portability/Unistd.h>
23 using namespace folly;
24 using namespace folly::experimental;
26 EnvironmentState EnvironmentState::fromCurrentEnvironment() {
27 std::unordered_map<std::string, std::string> data;
28 for (auto it = environ; it && *it; ++it) {
29 std::string key, value;
30 folly::StringPiece entry(*it);
31 auto equalsPosition = entry.find('=');
32 if (equalsPosition == entry.npos) {
33 throw MalformedEnvironment{to<std::string>(
34 "Environment contains an non key-value-pair string \"", entry, "\"")};
36 key = entry.subpiece(0, equalsPosition).toString();
37 value = entry.subpiece(equalsPosition + 1).toString();
38 if (data.count(key)) {
39 throw MalformedEnvironment{to<std::string>(
40 "Environment contains duplicate value for \"", key, "\"")};
42 data.emplace(std::move(key), std::move(value));
44 return EnvironmentState{std::move(data)};
47 #if __linux__ && !FOLLY_MOBILE
48 void EnvironmentState::setAsCurrentEnvironment() {
49 PCHECK(0 == clearenv());
50 for (const auto& kvp : env_) {
51 PCHECK(0 == setenv(kvp.first.c_str(), kvp.second.c_str(), (int)true));
56 std::vector<std::string> EnvironmentState::toVector() const {
57 std::vector<std::string> result;
58 for (auto const& pair : env_) {
59 result.emplace_back(to<std::string>(pair.first, "=", pair.second));
64 std::unique_ptr<char*, void (*)(char**)> EnvironmentState::toPointerArray()
66 size_t totalStringLength{};
67 for (auto const& pair : env_) {
68 totalStringLength += pair.first.size() + pair.second.size() +
69 2 /* intermediate '=' and the terminating NUL */;
71 size_t allocationRequired =
72 (totalStringLength / sizeof(char*) + 1) + env_.size() + 1;
73 char** raw = new char*[allocationRequired];
75 char* stringBase = reinterpret_cast<char*>(&raw[env_.size() + 1]);
76 char* const stringEnd = reinterpret_cast<char*>(&raw[allocationRequired]);
77 for (auto const& pair : env_) {
78 std::string const& key = pair.first;
79 std::string const& value = pair.second;
80 *ptrBase = stringBase;
81 size_t lengthIncludingNullTerminator = key.size() + 1 + value.size() + 1;
82 CHECK_GT(stringEnd - lengthIncludingNullTerminator, stringBase);
83 memcpy(stringBase, key.c_str(), key.size());
84 stringBase += key.size();
86 memcpy(stringBase, value.c_str(), value.size() + 1);
87 stringBase += value.size() + 1;
91 CHECK_EQ(env_.size(), ptrBase - raw);
92 return {raw, [](char** ptr) { delete[] ptr; }};