process-cpp  3.0.0
A simple convenience library for handling processes in C++11.
this_process.cpp
Go to the documentation of this file.
1 /*
2  * Copyright © 2013 Canonical Ltd.
3  *
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.
7  *
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.
12  *
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/>.
15  *
16  * Authored by: Thomas Voß <thomas.voss@canonical.com>
17  */
18 
20 #include <core/posix/process.h>
21 
22 #include <boost/algorithm/string.hpp>
23 
24 #include <iostream>
25 #include <mutex>
26 #include <sstream>
27 #include <vector>
28 
29 #include <cerrno>
30 #include <cstdlib>
31 
32 #if defined(_GNU_SOURCE)
33 #include <unistd.h>
34 #else
35 extern char** environ;
36 #endif
37 
38 namespace core
39 {
40 namespace posix
41 {
42 namespace this_process
43 {
44 namespace env
45 {
46 namespace
47 {
48 std::mutex& env_guard()
49 {
50  static std::mutex m;
51  return m;
52 }
53 }
54 
55 void for_each(const std::function<void(const std::string&, const std::string&)>& functor) noexcept(true)
56 {
57  std::lock_guard<std::mutex> lg(env_guard());
58  auto it = ::environ;
59  while (it != nullptr && *it != nullptr)
60  {
61  std::string line(*it);
62  functor(line.substr(0,line.find_first_of('=')),
63  line.substr(line.find_first_of('=')+1));
64  ++it;
65  }
66 }
67 
68 std::string get_or_throw(const std::string& key)
69 {
70  std::lock_guard<std::mutex> lg(env_guard());
71 
72  auto result = ::getenv(key.c_str());
73 
74  if (result == nullptr)
75  {
76  std::stringstream ss;
77  ss << "Variable with name " << key << " is not defined in the environment";
78  throw std::runtime_error(ss.str());
79  }
80 
81  return std::string{result};
82 }
83 
84 std::string get(const std::string& key,
85  const std::string& default_value) noexcept(true)
86 {
87  std::lock_guard<std::mutex> lg(env_guard());
88 
89  auto result = ::getenv(key.c_str());
90  return std::string{result ? result : default_value};
91 }
92 
93 void unset_or_throw(const std::string& key)
94 {
95  std::lock_guard<std::mutex> lg(env_guard());
96 
97  auto rc = ::unsetenv(key.c_str());
98 
99  if (rc == -1)
100  throw std::system_error(errno, std::system_category());
101 }
102 
103 bool unset(const std::string& key,
104  std::error_code& se) noexcept(true)
105 {
106  std::lock_guard<std::mutex> lg(env_guard());
107 
108  auto rc = ::unsetenv(key.c_str());
109 
110  if (rc == -1)
111  {
112  se = std::error_code(errno, std::system_category());
113  return false;
114  }
115 
116  return true;
117 }
118 
119 void set_or_throw(const std::string& key,
120  const std::string& value)
121 {
122  std::lock_guard<std::mutex> lg(env_guard());
123 
124  static const int overwrite = 0;
125  auto rc = ::setenv(key.c_str(), value.c_str(), overwrite);
126 
127  if (rc == -1)
128  throw std::system_error(errno, std::system_category());
129 }
130 
131 bool set(const std::string &key,
132  const std::string &value,
133  std::error_code& se) noexcept(true)
134 {
135  std::lock_guard<std::mutex> lg(env_guard());
136 
137  static const int overwrite = 0;
138  auto rc = ::setenv(key.c_str(), value.c_str(), overwrite);
139 
140  if (rc == -1)
141  {
142  se = std::error_code(errno, std::system_category());
143  return false;
144  }
145 
146  return true;
147 }
148 }
149 
150 Process instance() noexcept(true)
151 {
152  static const Process self{getpid()};
153  return self;
154 }
155 
156 Process parent() noexcept(true)
157 {
158  return Process(getppid());
159 }
160 
161 std::istream& cin() noexcept(true)
162 {
163  return std::cin;
164 }
165 
166 std::ostream& cout() noexcept(true)
167 {
168  return std::cout;
169 }
170 
171 std::ostream& cerr() noexcept(true)
172 {
173  return std::cerr;
174 }
175 }
176 }
177 }
The Process class models a process and possible operations on it.
Definition: process.h:44
char ** environ
CORE_POSIX_DLL_PUBLIC std::istream & cin() noexcept(true)
Access this process&#39;s stdin.
CORE_POSIX_DLL_PUBLIC void for_each(const std::function< void(const std::string &, const std::string &)> &functor) noexcept(true)
for_each invokes a functor for every key-value pair in the environment.
CORE_POSIX_DLL_PUBLIC std::ostream & cerr() noexcept(true)
Access this process&#39;s stderr.
CORE_POSIX_DLL_PUBLIC void set_or_throw(const std::string &key, const std::string &value)
set_or_throw will adjust the contents of the variable identified by key to the provided value...
CORE_POSIX_DLL_PUBLIC std::string get_or_throw(const std::string &key)
get queries the value of an environment variable.
CORE_POSIX_DLL_PUBLIC Process instance() noexcept(true)
Returns a Process instance corresponding to this process.
CORE_POSIX_DLL_PUBLIC void unset_or_throw(const std::string &key)
unset_or_throw removes the variable with name key from the environment.
CORE_POSIX_DLL_PUBLIC std::ostream & cout() noexcept(true)
Access this process&#39;s stdout.
CORE_POSIX_DLL_PUBLIC bool unset(const std::string &key, std::error_code &se) noexcept(true)
unset removes the variable with name key from the environment.
CORE_POSIX_DLL_PUBLIC Process parent() noexcept(true)
Query the parent of the process.