1 #include "config.h" 2 3 #include "util.hpp" 4 5 #include "config_parser.hpp" 6 #include "types.hpp" 7 8 #include <fmt/compile.h> 9 #include <fmt/format.h> 10 #include <sys/wait.h> 11 12 #include <phosphor-logging/elog-errors.hpp> 13 #include <phosphor-logging/lg2.hpp> 14 #include <xyz/openbmc_project/Common/error.hpp> 15 16 #include <cctype> 17 #include <string> 18 #include <string_view> 19 20 namespace phosphor 21 { 22 namespace network 23 { 24 25 using std::literals::string_view_literals::operator""sv; 26 using namespace phosphor::logging; 27 using namespace sdbusplus::xyz::openbmc_project::Common::Error; 28 29 namespace internal 30 { 31 32 void executeCommandinChildProcess(stdplus::const_zstring path, char** args) 33 { 34 using namespace std::string_literals; 35 pid_t pid = fork(); 36 37 if (pid == 0) 38 { 39 execv(path.c_str(), args); 40 exit(255); 41 } 42 else if (pid < 0) 43 { 44 auto error = errno; 45 lg2::error("Error occurred during fork: {ERRNO}", "ERRNO", error); 46 elog<InternalFailure>(); 47 } 48 else if (pid > 0) 49 { 50 int status; 51 while (waitpid(pid, &status, 0) == -1) 52 { 53 if (errno != EINTR) 54 { 55 status = -1; 56 break; 57 } 58 } 59 60 if (status < 0) 61 { 62 fmt::memory_buffer buf; 63 fmt::format_to(fmt::appender(buf), "`{}`", path); 64 for (size_t i = 0; args[i] != nullptr; ++i) 65 { 66 fmt::format_to(fmt::appender(buf), " `{}`", args[i]); 67 } 68 buf.push_back('\0'); 69 lg2::error("Unable to execute the command {CMD}: {STATUS}", "CMD", 70 buf.data(), "STATUS", status); 71 elog<InternalFailure>(); 72 } 73 } 74 } 75 76 /** @brief Get ignored interfaces from environment */ 77 std::string_view getIgnoredInterfacesEnv() 78 { 79 auto r = std::getenv("IGNORED_INTERFACES"); 80 if (r == nullptr) 81 { 82 return ""; 83 } 84 return r; 85 } 86 87 /** @brief Parse the comma separated interface names */ 88 std::unordered_set<std::string_view> 89 parseInterfaces(std::string_view interfaces) 90 { 91 std::unordered_set<std::string_view> result; 92 while (true) 93 { 94 auto sep = interfaces.find(','); 95 auto interface = interfaces.substr(0, sep); 96 while (!interface.empty() && std::isspace(interface.front())) 97 { 98 interface.remove_prefix(1); 99 } 100 while (!interface.empty() && std::isspace(interface.back())) 101 { 102 interface.remove_suffix(1); 103 } 104 if (!interface.empty()) 105 { 106 result.insert(interface); 107 } 108 if (sep == interfaces.npos) 109 { 110 break; 111 } 112 interfaces = interfaces.substr(sep + 1); 113 } 114 return result; 115 } 116 117 /** @brief Get the ignored interfaces */ 118 const std::unordered_set<std::string_view>& getIgnoredInterfaces() 119 { 120 static auto ignoredInterfaces = parseInterfaces(getIgnoredInterfacesEnv()); 121 return ignoredInterfaces; 122 } 123 124 } // namespace internal 125 126 std::optional<std::string> interfaceToUbootEthAddr(std::string_view intf) 127 { 128 constexpr auto pfx = "eth"sv; 129 if (!intf.starts_with(pfx)) 130 { 131 return std::nullopt; 132 } 133 intf.remove_prefix(pfx.size()); 134 unsigned idx; 135 try 136 { 137 idx = DecodeInt<unsigned, 10>{}(intf); 138 } 139 catch (...) 140 { 141 return std::nullopt; 142 } 143 if (idx == 0) 144 { 145 return "ethaddr"; 146 } 147 return fmt::format(FMT_COMPILE("eth{}addr"), idx); 148 } 149 150 static std::optional<DHCPVal> systemdParseDHCP(std::string_view str) 151 { 152 if (config::icaseeq(str, "ipv4")) 153 { 154 return DHCPVal{.v4 = true, .v6 = false}; 155 } 156 if (config::icaseeq(str, "ipv6")) 157 { 158 return DHCPVal{.v4 = false, .v6 = true}; 159 } 160 if (auto b = config::parseBool(str); b) 161 { 162 return DHCPVal{.v4 = *b, .v6 = *b}; 163 } 164 return std::nullopt; 165 } 166 167 inline auto systemdParseLast(const config::Parser& config, 168 std::string_view section, std::string_view key, 169 auto&& fun) 170 { 171 if (!config.getFileExists()) 172 {} 173 else if (auto str = config.map.getLastValueString(section, key); 174 str == nullptr) 175 { 176 lg2::notice( 177 "Unable to get the value of {CFG_SEC}[{CFG_KEY}] from {CFG_FILE}", 178 "CFG_SEC", section, "CFG_KEY", key, "CFG_FILE", 179 config.getFilename()); 180 } 181 else if (auto val = fun(*str); !val) 182 { 183 lg2::notice( 184 "Invalid value of {CFG_SEC}[{CFG_KEY}] from {CFG_FILE}: {CFG_VAL}", 185 "CFG_SEC", section, "CFG_KEY", key, "CFG_FILE", 186 config.getFilename(), "CFG_VAL", *str); 187 } 188 else 189 { 190 return val; 191 } 192 return decltype(fun(std::string_view{}))(std::nullopt); 193 } 194 195 bool getIPv6AcceptRA(const config::Parser& config) 196 { 197 #ifdef ENABLE_IPV6_ACCEPT_RA 198 constexpr bool def = true; 199 #else 200 constexpr bool def = false; 201 #endif 202 return systemdParseLast(config, "Network", "IPv6AcceptRA", 203 config::parseBool) 204 .value_or(def); 205 } 206 207 DHCPVal getDHCPValue(const config::Parser& config) 208 { 209 return systemdParseLast(config, "Network", "DHCP", systemdParseDHCP) 210 .value_or(DHCPVal{.v4 = true, .v6 = true}); 211 } 212 213 bool getDHCPProp(const config::Parser& config, std::string_view key) 214 { 215 return systemdParseLast(config, "DHCP", key, config::parseBool) 216 .value_or(true); 217 } 218 219 } // namespace network 220 } // namespace phosphor 221