2 * Copyright © 2013 Canonical Ltd.
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License version 3,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 * Authored by: Thomas Voß <thomas.voss@canonical.com>
19 #include <core/posix/this_process.h>
20 #include <core/posix/process.h>
22 #include <boost/algorithm/string.hpp>
32 #if defined(_GNU_SOURCE)
35 extern char** environ;
42 namespace this_process
48 std::mutex& env_guard()
55 void for_each(const std::function<void(const std::string&, const std::string&)>& functor) noexcept(true)
57 std::lock_guard<std::mutex> lg(env_guard());
59 while (it != nullptr && *it != nullptr)
61 std::string line(*it);
62 functor(line.substr(0,line.find_first_of('=')),
63 line.substr(line.find_first_of('=')+1));
68 std::string get_or_throw(const std::string& key)
70 std::lock_guard<std::mutex> lg(env_guard());
72 auto result = ::getenv(key.c_str());
74 if (result == nullptr)
77 ss << "Variable with name " << key << " is not defined in the environment";
78 throw std::runtime_error(ss.str());
81 return std::string{result};
84 std::string get(const std::string& key,
85 const std::string& default_value) noexcept(true)
87 std::lock_guard<std::mutex> lg(env_guard());
89 auto result = ::getenv(key.c_str());
90 return std::string{result ? result : default_value};
93 void unset_or_throw(const std::string& key)
95 std::lock_guard<std::mutex> lg(env_guard());
97 auto rc = ::unsetenv(key.c_str());
100 throw std::system_error(errno, std::system_category());
103 bool unset(const std::string& key,
104 std::error_code& se) noexcept(true)
106 std::lock_guard<std::mutex> lg(env_guard());
108 auto rc = ::unsetenv(key.c_str());
112 se = std::error_code(errno, std::system_category());
119 void set_or_throw(const std::string& key,
120 const std::string& value)
122 std::lock_guard<std::mutex> lg(env_guard());
124 static const int overwrite = 0;
125 auto rc = ::setenv(key.c_str(), value.c_str(), overwrite);
128 throw std::system_error(errno, std::system_category());
131 bool set(const std::string &key,
132 const std::string &value,
133 std::error_code& se) noexcept(true)
135 std::lock_guard<std::mutex> lg(env_guard());
137 static const int overwrite = 0;
138 auto rc = ::setenv(key.c_str(), value.c_str(), overwrite);
142 se = std::error_code(errno, std::system_category());
150 Process instance() noexcept(true)
152 static const Process self{getpid()};
156 Process parent() noexcept(true)
158 return Process(getppid());
161 std::istream& cin() noexcept(true)
166 std::ostream& cout() noexcept(true)
171 std::ostream& cerr() noexcept(true)