1 // Copyright 2010 The Kyua Authors. 2 // All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are 6 // met: 7 // 8 // * Redistributions of source code must retain the above copyright 9 // notice, this list of conditions and the following disclaimer. 10 // * Redistributions in binary form must reproduce the above copyright 11 // notice, this list of conditions and the following disclaimer in the 12 // documentation and/or other materials provided with the distribution. 13 // * Neither the name of Google Inc. nor the names of its contributors 14 // may be used to endorse or promote products derived from this software 15 // without specific prior written permission. 16 // 17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29 #include "utils/env.hpp" 30 31 #if defined(HAVE_CONFIG_H) 32 # include "config.h" 33 #endif 34 35 #include <cerrno> 36 #include <cstdlib> 37 #include <cstring> 38 #include <stdexcept> 39 40 #include "utils/format/macros.hpp" 41 #include "utils/fs/exceptions.hpp" 42 #include "utils/fs/path.hpp" 43 #include "utils/logging/macros.hpp" 44 #include "utils/optional.ipp" 45 46 namespace fs = utils::fs; 47 48 using utils::none; 49 using utils::optional; 50 51 52 extern "C" { 53 extern char** environ; 54 } 55 56 57 /// Gets all environment variables. 58 /// 59 /// \return A mapping of (name, value) pairs describing the environment 60 /// variables. 61 std::map< std::string, std::string > 62 utils::getallenv(void) 63 { 64 std::map< std::string, std::string > allenv; 65 for (char** envp = environ; *envp != NULL; envp++) { 66 const std::string oneenv = *envp; 67 const std::string::size_type pos = oneenv.find('='); 68 const std::string name = oneenv.substr(0, pos); 69 const std::string value = oneenv.substr(pos + 1); 70 71 PRE(allenv.find(name) == allenv.end()); 72 allenv[name] = value; 73 } 74 return allenv; 75 } 76 77 78 /// Gets the value of an environment variable. 79 /// 80 /// \param name The name of the environment variable to query. 81 /// 82 /// \return The value of the environment variable if it is defined, or none 83 /// otherwise. 84 optional< std::string > 85 utils::getenv(const std::string& name) 86 { 87 const char* value = std::getenv(name.c_str()); 88 if (value == NULL) { 89 LD(F("Environment variable '%s' is not defined") % name); 90 return none; 91 } else { 92 LD(F("Environment variable '%s' is '%s'") % name % value); 93 return utils::make_optional(std::string(value)); 94 } 95 } 96 97 98 /// Gets the value of an environment variable with a default fallback. 99 /// 100 /// \param name The name of the environment variable to query. 101 /// \param default_value The value to return if the variable is not defined. 102 /// 103 /// \return The value of the environment variable. 104 std::string 105 utils::getenv_with_default(const std::string& name, 106 const std::string& default_value) 107 { 108 const char* value = std::getenv(name.c_str()); 109 if (value == NULL) { 110 LD(F("Environment variable '%s' is not defined; using default '%s'") % 111 name % default_value); 112 return default_value; 113 } else { 114 LD(F("Environment variable '%s' is '%s'") % name % value); 115 return value; 116 } 117 } 118 119 120 /// Gets the value of the HOME environment variable with path validation. 121 /// 122 /// \return The value of the HOME environment variable if it is a valid path; 123 /// none if it is not defined or if it contains an invalid path. 124 optional< fs::path > 125 utils::get_home(void) 126 { 127 const optional< std::string > home = utils::getenv("HOME"); 128 if (home) { 129 try { 130 return utils::make_optional(fs::path(home.get())); 131 } catch (const fs::error& e) { 132 LW(F("Invalid value '%s' in HOME environment variable: %s") % 133 home.get() % e.what()); 134 return none; 135 } 136 } else { 137 return none; 138 } 139 } 140 141 142 /// Sets the value of an environment variable. 143 /// 144 /// \param name The name of the environment variable to set. 145 /// \param val The value to set the environment variable to. May be empty. 146 /// 147 /// \throw std::runtime_error If there is an error setting the environment 148 /// variable. 149 void 150 utils::setenv(const std::string& name, const std::string& val) 151 { 152 LD(F("Setting environment variable '%s' to '%s'") % name % val); 153 #if defined(HAVE_SETENV) 154 if (::setenv(name.c_str(), val.c_str(), 1) == -1) { 155 const int original_errno = errno; 156 throw std::runtime_error(F("Failed to set environment variable '%s' to " 157 "'%s': %s") % 158 name % val % std::strerror(original_errno)); 159 } 160 #elif defined(HAVE_PUTENV) 161 if (::putenv((F("%s=%s") % name % val).c_str()) == -1) { 162 const int original_errno = errno; 163 throw std::runtime_error(F("Failed to set environment variable '%s' to " 164 "'%s': %s") % 165 name % val % std::strerror(original_errno)); 166 } 167 #else 168 # error "Don't know how to set an environment variable." 169 #endif 170 } 171 172 173 /// Unsets an environment variable. 174 /// 175 /// \param name The name of the environment variable to unset. 176 /// 177 /// \throw std::runtime_error If there is an error unsetting the environment 178 /// variable. 179 void 180 utils::unsetenv(const std::string& name) 181 { 182 LD(F("Unsetting environment variable '%s'") % name); 183 #if defined(HAVE_UNSETENV) 184 if (::unsetenv(name.c_str()) == -1) { 185 const int original_errno = errno; 186 throw std::runtime_error(F("Failed to unset environment variable " 187 "'%s'") % 188 name % std::strerror(original_errno)); 189 } 190 #elif defined(HAVE_PUTENV) 191 if (::putenv((F("%s=") % name).c_str()) == -1) { 192 const int original_errno = errno; 193 throw std::runtime_error(F("Failed to unset environment variable " 194 "'%s'") % 195 name % std::strerror(original_errno)); 196 } 197 #else 198 # error "Don't know how to unset an environment variable." 199 #endif 200 } 201