Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2018 Intel Corporation |
| 3 | * |
| 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 |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 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. |
| 15 | */ |
| 16 | #include "config.h" |
| 17 | |
| 18 | #include "settings.hpp" |
| 19 | |
| 20 | #include <dlfcn.h> |
| 21 | |
| 22 | #include <algorithm> |
| 23 | #include <any> |
Vernon Mauery | 735ee95 | 2019-02-15 13:38:52 -0800 | [diff] [blame] | 24 | #include <boost/algorithm/string.hpp> |
Vernon Mauery | e8d4323 | 2019-03-26 16:23:43 -0700 | [diff] [blame] | 25 | #include <dcmihandler.hpp> |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 26 | #include <exception> |
Vernon Mauery | bdda800 | 2019-02-26 10:18:51 -0800 | [diff] [blame] | 27 | #include <filesystem> |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 28 | #include <forward_list> |
| 29 | #include <host-cmd-manager.hpp> |
| 30 | #include <ipmid-host/cmd.hpp> |
| 31 | #include <ipmid/api.hpp> |
| 32 | #include <ipmid/handler.hpp> |
| 33 | #include <ipmid/message.hpp> |
| 34 | #include <ipmid/oemrouter.hpp> |
Vernon Mauery | 3325024 | 2019-03-12 16:49:26 -0700 | [diff] [blame] | 35 | #include <ipmid/types.hpp> |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 36 | #include <map> |
| 37 | #include <memory> |
| 38 | #include <optional> |
| 39 | #include <phosphor-logging/log.hpp> |
| 40 | #include <sdbusplus/asio/connection.hpp> |
| 41 | #include <sdbusplus/asio/object_server.hpp> |
| 42 | #include <sdbusplus/asio/sd_event.hpp> |
| 43 | #include <sdbusplus/bus.hpp> |
| 44 | #include <sdbusplus/bus/match.hpp> |
| 45 | #include <sdbusplus/timer.hpp> |
| 46 | #include <tuple> |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 47 | #include <unordered_map> |
| 48 | #include <utility> |
| 49 | #include <vector> |
| 50 | |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 51 | namespace fs = std::filesystem; |
| 52 | |
| 53 | using namespace phosphor::logging; |
| 54 | |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 55 | // IPMI Spec, shared Reservation ID. |
| 56 | static unsigned short selReservationID = 0xFFFF; |
| 57 | static bool selReservationValid = false; |
| 58 | |
| 59 | unsigned short reserveSel(void) |
| 60 | { |
| 61 | // IPMI spec, Reservation ID, the value simply increases against each |
| 62 | // execution of the Reserve SEL command. |
| 63 | if (++selReservationID == 0) |
| 64 | { |
| 65 | selReservationID = 1; |
| 66 | } |
| 67 | selReservationValid = true; |
| 68 | return selReservationID; |
| 69 | } |
| 70 | |
| 71 | bool checkSELReservation(unsigned short id) |
| 72 | { |
| 73 | return (selReservationValid && selReservationID == id); |
| 74 | } |
| 75 | |
| 76 | void cancelSELReservation(void) |
| 77 | { |
| 78 | selReservationValid = false; |
| 79 | } |
| 80 | |
| 81 | EInterfaceIndex getInterfaceIndex(void) |
| 82 | { |
| 83 | return interfaceKCS; |
| 84 | } |
| 85 | |
| 86 | sd_bus* bus; |
| 87 | sd_event* events = nullptr; |
| 88 | sd_event* ipmid_get_sd_event_connection(void) |
| 89 | { |
| 90 | return events; |
| 91 | } |
| 92 | sd_bus* ipmid_get_sd_bus_connection(void) |
| 93 | { |
| 94 | return bus; |
| 95 | } |
| 96 | |
| 97 | namespace ipmi |
| 98 | { |
| 99 | |
| 100 | static inline unsigned int makeCmdKey(unsigned int cluster, unsigned int cmd) |
| 101 | { |
| 102 | return (cluster << 8) | cmd; |
| 103 | } |
| 104 | |
| 105 | using HandlerTuple = std::tuple<int, /* prio */ |
| 106 | Privilege, HandlerBase::ptr /* handler */ |
| 107 | >; |
| 108 | |
| 109 | /* map to handle standard registered commands */ |
| 110 | static std::unordered_map<unsigned int, /* key is NetFn/Cmd */ |
| 111 | HandlerTuple> |
| 112 | handlerMap; |
| 113 | |
Vernon Mauery | f984a01 | 2018-10-08 12:05:18 -0700 | [diff] [blame] | 114 | /* special map for decoding Group registered commands (NetFn 2Ch) */ |
| 115 | static std::unordered_map<unsigned int, /* key is Group/Cmd (NetFn is 2Ch) */ |
| 116 | HandlerTuple> |
| 117 | groupHandlerMap; |
| 118 | |
| 119 | /* special map for decoding OEM registered commands (NetFn 2Eh) */ |
| 120 | static std::unordered_map<unsigned int, /* key is Iana/Cmd (NetFn is 2Eh) */ |
| 121 | HandlerTuple> |
| 122 | oemHandlerMap; |
| 123 | |
Vernon Mauery | 08a70aa | 2018-11-07 09:36:22 -0800 | [diff] [blame] | 124 | using FilterTuple = std::tuple<int, /* prio */ |
| 125 | FilterBase::ptr /* filter */ |
| 126 | >; |
| 127 | |
| 128 | /* list to hold all registered ipmi command filters */ |
| 129 | static std::forward_list<FilterTuple> filterList; |
| 130 | |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 131 | namespace impl |
| 132 | { |
| 133 | /* common function to register all standard IPMI handlers */ |
| 134 | bool registerHandler(int prio, NetFn netFn, Cmd cmd, Privilege priv, |
| 135 | HandlerBase::ptr handler) |
| 136 | { |
| 137 | // check for valid NetFn: even; 00-0Ch, 30-3Eh |
| 138 | if (netFn & 1 || (netFn > netFnTransport && netFn < netFnGroup) || |
| 139 | netFn > netFnOemEight) |
| 140 | { |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | // create key and value for this handler |
| 145 | unsigned int netFnCmd = makeCmdKey(netFn, cmd); |
| 146 | HandlerTuple item(prio, priv, handler); |
| 147 | |
| 148 | // consult the handler map and look for a match |
| 149 | auto& mapCmd = handlerMap[netFnCmd]; |
| 150 | if (!std::get<HandlerBase::ptr>(mapCmd) || std::get<int>(mapCmd) <= prio) |
| 151 | { |
| 152 | mapCmd = item; |
| 153 | return true; |
| 154 | } |
| 155 | return false; |
| 156 | } |
| 157 | |
Vernon Mauery | f984a01 | 2018-10-08 12:05:18 -0700 | [diff] [blame] | 158 | /* common function to register all Group IPMI handlers */ |
| 159 | bool registerGroupHandler(int prio, Group group, Cmd cmd, Privilege priv, |
| 160 | HandlerBase::ptr handler) |
| 161 | { |
| 162 | // create key and value for this handler |
| 163 | unsigned int netFnCmd = makeCmdKey(group, cmd); |
| 164 | HandlerTuple item(prio, priv, handler); |
| 165 | |
| 166 | // consult the handler map and look for a match |
| 167 | auto& mapCmd = groupHandlerMap[netFnCmd]; |
| 168 | if (!std::get<HandlerBase::ptr>(mapCmd) || std::get<int>(mapCmd) <= prio) |
| 169 | { |
| 170 | mapCmd = item; |
| 171 | return true; |
| 172 | } |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | /* common function to register all OEM IPMI handlers */ |
| 177 | bool registerOemHandler(int prio, Iana iana, Cmd cmd, Privilege priv, |
| 178 | HandlerBase::ptr handler) |
| 179 | { |
| 180 | // create key and value for this handler |
| 181 | unsigned int netFnCmd = makeCmdKey(iana, cmd); |
| 182 | HandlerTuple item(prio, priv, handler); |
| 183 | |
| 184 | // consult the handler map and look for a match |
| 185 | auto& mapCmd = oemHandlerMap[netFnCmd]; |
| 186 | if (!std::get<HandlerBase::ptr>(mapCmd) || std::get<int>(mapCmd) <= prio) |
| 187 | { |
| 188 | mapCmd = item; |
| 189 | return true; |
| 190 | } |
| 191 | return false; |
| 192 | } |
| 193 | |
Vernon Mauery | 08a70aa | 2018-11-07 09:36:22 -0800 | [diff] [blame] | 194 | /* common function to register all IPMI filter handlers */ |
| 195 | void registerFilter(int prio, FilterBase::ptr filter) |
| 196 | { |
| 197 | // check for initial placement |
| 198 | if (filterList.empty() || std::get<int>(filterList.front()) < prio) |
| 199 | { |
| 200 | filterList.emplace_front(std::make_tuple(prio, filter)); |
| 201 | } |
| 202 | // walk the list and put it in the right place |
| 203 | auto j = filterList.begin(); |
| 204 | for (auto i = j; i != filterList.end() && std::get<int>(*i) > prio; i++) |
| 205 | { |
| 206 | j = i; |
| 207 | } |
| 208 | filterList.emplace_after(j, std::make_tuple(prio, filter)); |
| 209 | } |
| 210 | |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 211 | } // namespace impl |
| 212 | |
Vernon Mauery | 08a70aa | 2018-11-07 09:36:22 -0800 | [diff] [blame] | 213 | message::Response::ptr filterIpmiCommand(message::Request::ptr request) |
| 214 | { |
| 215 | // pass the command through the filter mechanism |
| 216 | // This can be the firmware firewall or any OEM mechanism like |
| 217 | // whitelist filtering based on operational mode |
| 218 | for (auto& item : filterList) |
| 219 | { |
| 220 | FilterBase::ptr filter = std::get<FilterBase::ptr>(item); |
| 221 | ipmi::Cc cc = filter->call(request); |
| 222 | if (ipmi::ccSuccess != cc) |
| 223 | { |
| 224 | return errorResponse(request, cc); |
| 225 | } |
| 226 | } |
| 227 | return message::Response::ptr(); |
| 228 | } |
| 229 | |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 230 | message::Response::ptr executeIpmiCommandCommon( |
| 231 | std::unordered_map<unsigned int, HandlerTuple>& handlers, |
| 232 | unsigned int keyCommon, message::Request::ptr request) |
| 233 | { |
Vernon Mauery | 08a70aa | 2018-11-07 09:36:22 -0800 | [diff] [blame] | 234 | // filter the command first; a non-null message::Response::ptr |
| 235 | // means that the message has been rejected for some reason |
Vernon Mauery | 51f7814 | 2020-01-13 16:28:59 -0800 | [diff] [blame] | 236 | message::Response::ptr filterResponse = filterIpmiCommand(request); |
Vernon Mauery | 08a70aa | 2018-11-07 09:36:22 -0800 | [diff] [blame] | 237 | |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 238 | Cmd cmd = request->ctx->cmd; |
| 239 | unsigned int key = makeCmdKey(keyCommon, cmd); |
| 240 | auto cmdIter = handlers.find(key); |
| 241 | if (cmdIter != handlers.end()) |
| 242 | { |
Vernon Mauery | 51f7814 | 2020-01-13 16:28:59 -0800 | [diff] [blame] | 243 | // only return the filter response if the command is found |
| 244 | if (filterResponse) |
| 245 | { |
| 246 | return filterResponse; |
| 247 | } |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 248 | HandlerTuple& chosen = cmdIter->second; |
| 249 | if (request->ctx->priv < std::get<Privilege>(chosen)) |
| 250 | { |
| 251 | return errorResponse(request, ccInsufficientPrivilege); |
| 252 | } |
| 253 | return std::get<HandlerBase::ptr>(chosen)->call(request); |
| 254 | } |
| 255 | else |
| 256 | { |
| 257 | unsigned int wildcard = makeCmdKey(keyCommon, cmdWildcard); |
| 258 | cmdIter = handlers.find(wildcard); |
| 259 | if (cmdIter != handlers.end()) |
| 260 | { |
Vernon Mauery | 51f7814 | 2020-01-13 16:28:59 -0800 | [diff] [blame] | 261 | // only return the filter response if the command is found |
| 262 | if (filterResponse) |
| 263 | { |
| 264 | return filterResponse; |
| 265 | } |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 266 | HandlerTuple& chosen = cmdIter->second; |
| 267 | if (request->ctx->priv < std::get<Privilege>(chosen)) |
| 268 | { |
| 269 | return errorResponse(request, ccInsufficientPrivilege); |
| 270 | } |
| 271 | return std::get<HandlerBase::ptr>(chosen)->call(request); |
| 272 | } |
| 273 | } |
| 274 | return errorResponse(request, ccInvalidCommand); |
| 275 | } |
| 276 | |
Vernon Mauery | f984a01 | 2018-10-08 12:05:18 -0700 | [diff] [blame] | 277 | message::Response::ptr executeIpmiGroupCommand(message::Request::ptr request) |
| 278 | { |
| 279 | // look up the group for this request |
William A. Kennington III | d10d905 | 2019-04-24 01:57:36 -0700 | [diff] [blame] | 280 | uint8_t bytes; |
| 281 | if (0 != request->payload.unpack(bytes)) |
Vernon Mauery | f984a01 | 2018-10-08 12:05:18 -0700 | [diff] [blame] | 282 | { |
| 283 | return errorResponse(request, ccReqDataLenInvalid); |
| 284 | } |
William A. Kennington III | d10d905 | 2019-04-24 01:57:36 -0700 | [diff] [blame] | 285 | auto group = static_cast<Group>(bytes); |
Vernon Mauery | f984a01 | 2018-10-08 12:05:18 -0700 | [diff] [blame] | 286 | message::Response::ptr response = |
| 287 | executeIpmiCommandCommon(groupHandlerMap, group, request); |
William A. Kennington III | da31f9a | 2019-04-25 01:36:32 -0700 | [diff] [blame] | 288 | ipmi::message::Payload prefix; |
| 289 | prefix.pack(bytes); |
| 290 | response->prepend(prefix); |
Vernon Mauery | f984a01 | 2018-10-08 12:05:18 -0700 | [diff] [blame] | 291 | return response; |
| 292 | } |
| 293 | |
| 294 | message::Response::ptr executeIpmiOemCommand(message::Request::ptr request) |
| 295 | { |
| 296 | // look up the iana for this request |
William A. Kennington III | d10d905 | 2019-04-24 01:57:36 -0700 | [diff] [blame] | 297 | uint24_t bytes; |
| 298 | if (0 != request->payload.unpack(bytes)) |
Vernon Mauery | f984a01 | 2018-10-08 12:05:18 -0700 | [diff] [blame] | 299 | { |
| 300 | return errorResponse(request, ccReqDataLenInvalid); |
| 301 | } |
William A. Kennington III | d10d905 | 2019-04-24 01:57:36 -0700 | [diff] [blame] | 302 | auto iana = static_cast<Iana>(bytes); |
Vernon Mauery | f984a01 | 2018-10-08 12:05:18 -0700 | [diff] [blame] | 303 | message::Response::ptr response = |
| 304 | executeIpmiCommandCommon(oemHandlerMap, iana, request); |
William A. Kennington III | da31f9a | 2019-04-25 01:36:32 -0700 | [diff] [blame] | 305 | ipmi::message::Payload prefix; |
| 306 | prefix.pack(bytes); |
| 307 | response->prepend(prefix); |
Vernon Mauery | f984a01 | 2018-10-08 12:05:18 -0700 | [diff] [blame] | 308 | return response; |
| 309 | } |
| 310 | |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 311 | message::Response::ptr executeIpmiCommand(message::Request::ptr request) |
| 312 | { |
| 313 | NetFn netFn = request->ctx->netFn; |
Vernon Mauery | f984a01 | 2018-10-08 12:05:18 -0700 | [diff] [blame] | 314 | if (netFnGroup == netFn) |
| 315 | { |
| 316 | return executeIpmiGroupCommand(request); |
| 317 | } |
| 318 | else if (netFnOem == netFn) |
| 319 | { |
| 320 | return executeIpmiOemCommand(request); |
| 321 | } |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 322 | return executeIpmiCommandCommon(handlerMap, netFn, request); |
| 323 | } |
| 324 | |
Vernon Mauery | 735ee95 | 2019-02-15 13:38:52 -0800 | [diff] [blame] | 325 | namespace utils |
| 326 | { |
| 327 | template <typename AssocContainer, typename UnaryPredicate> |
| 328 | void assoc_erase_if(AssocContainer& c, UnaryPredicate p) |
| 329 | { |
| 330 | typename AssocContainer::iterator next = c.begin(); |
| 331 | typename AssocContainer::iterator last = c.end(); |
| 332 | while ((next = std::find_if(next, last, p)) != last) |
| 333 | { |
| 334 | c.erase(next++); |
| 335 | } |
| 336 | } |
| 337 | } // namespace utils |
| 338 | |
| 339 | namespace |
| 340 | { |
| 341 | std::unordered_map<std::string, uint8_t> uniqueNameToChannelNumber; |
| 342 | |
| 343 | // sdbusplus::bus::match::rules::arg0namespace() wants the prefix |
| 344 | // to match without any trailing '.' |
| 345 | constexpr const char ipmiDbusChannelMatch[] = |
| 346 | "xyz.openbmc_project.Ipmi.Channel"; |
| 347 | void updateOwners(sdbusplus::asio::connection& conn, const std::string& name) |
| 348 | { |
| 349 | conn.async_method_call( |
| 350 | [name](const boost::system::error_code ec, |
| 351 | const std::string& nameOwner) { |
| 352 | if (ec) |
| 353 | { |
| 354 | log<level::ERR>("Error getting dbus owner", |
| 355 | entry("INTERFACE=%s", name.c_str())); |
| 356 | return; |
| 357 | } |
| 358 | // start after ipmiDbusChannelPrefix (after the '.') |
| 359 | std::string chName = |
| 360 | name.substr(std::strlen(ipmiDbusChannelMatch) + 1); |
| 361 | try |
| 362 | { |
| 363 | uint8_t channel = getChannelByName(chName); |
| 364 | uniqueNameToChannelNumber[nameOwner] = channel; |
| 365 | log<level::INFO>("New interface mapping", |
| 366 | entry("INTERFACE=%s", name.c_str()), |
| 367 | entry("CHANNEL=%u", channel)); |
| 368 | } |
| 369 | catch (const std::exception& e) |
| 370 | { |
| 371 | log<level::INFO>("Failed interface mapping, no such name", |
| 372 | entry("INTERFACE=%s", name.c_str())); |
| 373 | } |
| 374 | }, |
| 375 | "org.freedesktop.DBus", "/", "org.freedesktop.DBus", "GetNameOwner", |
| 376 | name); |
| 377 | } |
| 378 | |
| 379 | void doListNames(boost::asio::io_service& io, sdbusplus::asio::connection& conn) |
| 380 | { |
| 381 | conn.async_method_call( |
| 382 | [&io, &conn](const boost::system::error_code ec, |
| 383 | std::vector<std::string> busNames) { |
| 384 | if (ec) |
| 385 | { |
| 386 | log<level::ERR>("Error getting dbus names"); |
| 387 | std::exit(EXIT_FAILURE); |
| 388 | return; |
| 389 | } |
| 390 | // Try to make startup consistent |
| 391 | std::sort(busNames.begin(), busNames.end()); |
| 392 | |
| 393 | const std::string channelPrefix = |
| 394 | std::string(ipmiDbusChannelMatch) + "."; |
| 395 | for (const std::string& busName : busNames) |
| 396 | { |
| 397 | if (busName.find(channelPrefix) == 0) |
| 398 | { |
| 399 | updateOwners(conn, busName); |
| 400 | } |
| 401 | } |
| 402 | }, |
| 403 | "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", |
| 404 | "ListNames"); |
| 405 | } |
| 406 | |
| 407 | void nameChangeHandler(sdbusplus::message::message& message) |
| 408 | { |
| 409 | std::string name; |
| 410 | std::string oldOwner; |
| 411 | std::string newOwner; |
| 412 | |
| 413 | message.read(name, oldOwner, newOwner); |
| 414 | |
| 415 | if (!oldOwner.empty()) |
| 416 | { |
| 417 | if (boost::starts_with(oldOwner, ":")) |
| 418 | { |
| 419 | // Connection removed |
| 420 | auto it = uniqueNameToChannelNumber.find(oldOwner); |
| 421 | if (it != uniqueNameToChannelNumber.end()) |
| 422 | { |
| 423 | uniqueNameToChannelNumber.erase(it); |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | if (!newOwner.empty()) |
| 428 | { |
| 429 | // start after ipmiDbusChannelMatch (and after the '.') |
| 430 | std::string chName = name.substr(std::strlen(ipmiDbusChannelMatch) + 1); |
| 431 | try |
| 432 | { |
| 433 | uint8_t channel = getChannelByName(chName); |
| 434 | uniqueNameToChannelNumber[newOwner] = channel; |
| 435 | log<level::INFO>("New interface mapping", |
| 436 | entry("INTERFACE=%s", name.c_str()), |
| 437 | entry("CHANNEL=%u", channel)); |
| 438 | } |
| 439 | catch (const std::exception& e) |
| 440 | { |
| 441 | log<level::INFO>("Failed interface mapping, no such name", |
| 442 | entry("INTERFACE=%s", name.c_str())); |
| 443 | } |
| 444 | } |
| 445 | }; |
| 446 | |
| 447 | } // anonymous namespace |
| 448 | |
| 449 | static constexpr const char intraBmcName[] = "INTRABMC"; |
| 450 | uint8_t channelFromMessage(sdbusplus::message::message& msg) |
| 451 | { |
| 452 | // channel name for ipmitool to resolve to |
| 453 | std::string sender = msg.get_sender(); |
| 454 | auto chIter = uniqueNameToChannelNumber.find(sender); |
| 455 | if (chIter != uniqueNameToChannelNumber.end()) |
| 456 | { |
| 457 | return chIter->second; |
| 458 | } |
| 459 | // FIXME: currently internal connections are ephemeral and hard to pin down |
| 460 | try |
| 461 | { |
| 462 | return getChannelByName(intraBmcName); |
| 463 | } |
| 464 | catch (const std::exception& e) |
| 465 | { |
| 466 | return invalidChannel; |
| 467 | } |
| 468 | } // namespace ipmi |
| 469 | |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 470 | /* called from sdbus async server context */ |
Vernon Mauery | 735ee95 | 2019-02-15 13:38:52 -0800 | [diff] [blame] | 471 | auto executionEntry(boost::asio::yield_context yield, |
| 472 | sdbusplus::message::message& m, NetFn netFn, uint8_t lun, |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 473 | Cmd cmd, std::vector<uint8_t>& data, |
| 474 | std::map<std::string, ipmi::Value>& options) |
| 475 | { |
Vernon Mauery | 735ee95 | 2019-02-15 13:38:52 -0800 | [diff] [blame] | 476 | const auto dbusResponse = |
| 477 | [netFn, lun, cmd](Cc cc, const std::vector<uint8_t>& data = {}) { |
| 478 | constexpr uint8_t netFnResponse = 0x01; |
| 479 | uint8_t retNetFn = netFn | netFnResponse; |
| 480 | return std::make_tuple(retNetFn, lun, cmd, cc, data); |
| 481 | }; |
| 482 | std::string sender = m.get_sender(); |
| 483 | Privilege privilege = Privilege::None; |
Vernon Mauery | d6a2da0 | 2019-04-09 16:00:46 -0700 | [diff] [blame] | 484 | int rqSA = 0; |
Vernon Mauery | 735ee95 | 2019-02-15 13:38:52 -0800 | [diff] [blame] | 485 | uint8_t userId = 0; // undefined user |
Rajashekar Gade Reddy | 4d22640 | 2019-11-13 17:13:05 +0530 | [diff] [blame] | 486 | uint32_t sessionId = 0; |
Vernon Mauery | 735ee95 | 2019-02-15 13:38:52 -0800 | [diff] [blame] | 487 | |
| 488 | // figure out what channel the request came in on |
| 489 | uint8_t channel = channelFromMessage(m); |
| 490 | if (channel == invalidChannel) |
| 491 | { |
| 492 | // unknown sender channel; refuse to service the request |
| 493 | log<level::ERR>("ERROR determining source IPMI channel", |
| 494 | entry("SENDER=%s", sender.c_str()), |
| 495 | entry("NETFN=0x%X", netFn), entry("CMD=0x%X", cmd)); |
| 496 | return dbusResponse(ipmi::ccDestinationUnavailable); |
| 497 | } |
| 498 | |
Rajashekar Gade Reddy | 4d22640 | 2019-11-13 17:13:05 +0530 | [diff] [blame] | 499 | // session-based channels are required to provide userId, privilege and |
| 500 | // sessionId |
Vernon Mauery | 735ee95 | 2019-02-15 13:38:52 -0800 | [diff] [blame] | 501 | if (getChannelSessionSupport(channel) != EChannelSessSupported::none) |
| 502 | { |
| 503 | try |
| 504 | { |
| 505 | Value requestPriv = options.at("privilege"); |
| 506 | Value requestUserId = options.at("userId"); |
Rajashekar Gade Reddy | 4d22640 | 2019-11-13 17:13:05 +0530 | [diff] [blame] | 507 | Value requestSessionId = options.at("currentSessionId"); |
Vernon Mauery | 735ee95 | 2019-02-15 13:38:52 -0800 | [diff] [blame] | 508 | privilege = static_cast<Privilege>(std::get<int>(requestPriv)); |
| 509 | userId = static_cast<uint8_t>(std::get<int>(requestUserId)); |
Rajashekar Gade Reddy | 4d22640 | 2019-11-13 17:13:05 +0530 | [diff] [blame] | 510 | sessionId = |
| 511 | static_cast<uint32_t>(std::get<uint32_t>(requestSessionId)); |
Vernon Mauery | 735ee95 | 2019-02-15 13:38:52 -0800 | [diff] [blame] | 512 | } |
| 513 | catch (const std::exception& e) |
| 514 | { |
| 515 | log<level::ERR>("ERROR determining IPMI session credentials", |
| 516 | entry("CHANNEL=%u", channel), |
| 517 | entry("NETFN=0x%X", netFn), entry("CMD=0x%X", cmd)); |
| 518 | return dbusResponse(ipmi::ccUnspecifiedError); |
| 519 | } |
| 520 | } |
| 521 | else |
| 522 | { |
| 523 | // get max privilege for session-less channels |
| 524 | // For now, there is not a way to configure this, default to Admin |
| 525 | privilege = Privilege::Admin; |
Vernon Mauery | d6a2da0 | 2019-04-09 16:00:46 -0700 | [diff] [blame] | 526 | |
| 527 | // ipmb should supply rqSA |
| 528 | ChannelInfo chInfo; |
| 529 | getChannelInfo(channel, chInfo); |
| 530 | if (static_cast<EChannelMediumType>(chInfo.mediumType) == |
| 531 | EChannelMediumType::ipmb) |
| 532 | { |
| 533 | const auto iter = options.find("rqSA"); |
| 534 | if (iter != options.end()) |
| 535 | { |
| 536 | if (std::holds_alternative<int>(iter->second)) |
| 537 | { |
| 538 | rqSA = std::get<int>(iter->second); |
| 539 | } |
| 540 | } |
| 541 | } |
Vernon Mauery | 735ee95 | 2019-02-15 13:38:52 -0800 | [diff] [blame] | 542 | } |
| 543 | // check to see if the requested priv/username is valid |
| 544 | log<level::DEBUG>("Set up ipmi context", entry("SENDER=%s", sender.c_str()), |
Johnathan Mantey | c11cc5c | 2020-07-22 13:52:33 -0700 | [diff] [blame] | 545 | entry("NETFN=0x%X", netFn), entry("LUN=0x%X", lun), |
| 546 | entry("CMD=0x%X", cmd), entry("CHANNEL=%u", channel), |
| 547 | entry("USERID=%u", userId), |
Rajashekar Gade Reddy | 4d22640 | 2019-11-13 17:13:05 +0530 | [diff] [blame] | 548 | entry("SESSIONID=0x%X", sessionId), |
Vernon Mauery | d6a2da0 | 2019-04-09 16:00:46 -0700 | [diff] [blame] | 549 | entry("PRIVILEGE=%u", static_cast<uint8_t>(privilege)), |
| 550 | entry("RQSA=%x", rqSA)); |
Vernon Mauery | 735ee95 | 2019-02-15 13:38:52 -0800 | [diff] [blame] | 551 | |
Johnathan Mantey | c11cc5c | 2020-07-22 13:52:33 -0700 | [diff] [blame] | 552 | auto ctx = std::make_shared<ipmi::Context>(getSdBus(), netFn, lun, cmd, |
| 553 | channel, userId, sessionId, |
| 554 | privilege, rqSA, yield); |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 555 | auto request = std::make_shared<ipmi::message::Request>( |
| 556 | ctx, std::forward<std::vector<uint8_t>>(data)); |
| 557 | message::Response::ptr response = executeIpmiCommand(request); |
| 558 | |
Vernon Mauery | 735ee95 | 2019-02-15 13:38:52 -0800 | [diff] [blame] | 559 | return dbusResponse(response->cc, response->payload.raw); |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | /** @struct IpmiProvider |
| 563 | * |
| 564 | * RAII wrapper for dlopen so that dlclose gets called on exit |
| 565 | */ |
| 566 | struct IpmiProvider |
| 567 | { |
| 568 | public: |
| 569 | /** @brief address of the opened library */ |
| 570 | void* addr; |
| 571 | std::string name; |
| 572 | |
| 573 | IpmiProvider() = delete; |
| 574 | IpmiProvider(const IpmiProvider&) = delete; |
| 575 | IpmiProvider& operator=(const IpmiProvider&) = delete; |
| 576 | IpmiProvider(IpmiProvider&&) = delete; |
| 577 | IpmiProvider& operator=(IpmiProvider&&) = delete; |
| 578 | |
| 579 | /** @brief dlopen a shared object file by path |
| 580 | * @param[in] filename - path of shared object to open |
| 581 | */ |
| 582 | explicit IpmiProvider(const char* fname) : addr(nullptr), name(fname) |
| 583 | { |
| 584 | log<level::DEBUG>("Open IPMI provider library", |
| 585 | entry("PROVIDER=%s", name.c_str())); |
| 586 | try |
| 587 | { |
| 588 | addr = dlopen(name.c_str(), RTLD_NOW); |
| 589 | } |
| 590 | catch (std::exception& e) |
| 591 | { |
| 592 | log<level::ERR>("ERROR opening IPMI provider", |
| 593 | entry("PROVIDER=%s", name.c_str()), |
| 594 | entry("ERROR=%s", e.what())); |
| 595 | } |
| 596 | catch (...) |
| 597 | { |
| 598 | std::exception_ptr eptr = std::current_exception(); |
| 599 | try |
| 600 | { |
| 601 | std::rethrow_exception(eptr); |
| 602 | } |
| 603 | catch (std::exception& e) |
| 604 | { |
| 605 | log<level::ERR>("ERROR opening IPMI provider", |
| 606 | entry("PROVIDER=%s", name.c_str()), |
| 607 | entry("ERROR=%s", e.what())); |
| 608 | } |
| 609 | } |
| 610 | if (!isOpen()) |
| 611 | { |
| 612 | log<level::ERR>("ERROR opening IPMI provider", |
| 613 | entry("PROVIDER=%s", name.c_str()), |
| 614 | entry("ERROR=%s", dlerror())); |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | ~IpmiProvider() |
| 619 | { |
| 620 | if (isOpen()) |
| 621 | { |
| 622 | dlclose(addr); |
| 623 | } |
| 624 | } |
| 625 | bool isOpen() const |
| 626 | { |
| 627 | return (nullptr != addr); |
| 628 | } |
| 629 | }; |
| 630 | |
| 631 | // Plugin libraries need to contain .so either at the end or in the middle |
| 632 | constexpr const char ipmiPluginExtn[] = ".so"; |
| 633 | |
| 634 | /* return a list of self-closing library handles */ |
| 635 | std::forward_list<IpmiProvider> loadProviders(const fs::path& ipmiLibsPath) |
| 636 | { |
| 637 | std::vector<fs::path> libs; |
| 638 | for (const auto& libPath : fs::directory_iterator(ipmiLibsPath)) |
| 639 | { |
Vernon Mauery | b0ab5fe | 2019-03-06 14:03:00 -0800 | [diff] [blame] | 640 | std::error_code ec; |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 641 | fs::path fname = libPath.path(); |
Vernon Mauery | b0ab5fe | 2019-03-06 14:03:00 -0800 | [diff] [blame] | 642 | if (fs::is_symlink(fname, ec) || ec) |
| 643 | { |
| 644 | // it's a symlink or some other error; skip it |
| 645 | continue; |
| 646 | } |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 647 | while (fname.has_extension()) |
| 648 | { |
| 649 | fs::path extn = fname.extension(); |
| 650 | if (extn == ipmiPluginExtn) |
| 651 | { |
| 652 | libs.push_back(libPath.path()); |
| 653 | break; |
| 654 | } |
| 655 | fname.replace_extension(); |
| 656 | } |
| 657 | } |
| 658 | std::sort(libs.begin(), libs.end()); |
| 659 | |
| 660 | std::forward_list<IpmiProvider> handles; |
| 661 | for (auto& lib : libs) |
| 662 | { |
| 663 | #ifdef __IPMI_DEBUG__ |
| 664 | log<level::DEBUG>("Registering handler", |
| 665 | entry("HANDLER=%s", lib.c_str())); |
| 666 | #endif |
| 667 | handles.emplace_front(lib.c_str()); |
| 668 | } |
| 669 | return handles; |
| 670 | } |
| 671 | |
| 672 | } // namespace ipmi |
| 673 | |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 674 | #ifdef ALLOW_DEPRECATED_API |
| 675 | /* legacy registration */ |
| 676 | void ipmi_register_callback(ipmi_netfn_t netFn, ipmi_cmd_t cmd, |
| 677 | ipmi_context_t context, ipmid_callback_t handler, |
| 678 | ipmi_cmd_privilege_t priv) |
| 679 | { |
Vernon Mauery | be37630 | 2019-03-21 13:02:05 -0700 | [diff] [blame] | 680 | auto h = ipmi::makeLegacyHandler(handler, context); |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 681 | // translate priv from deprecated enum to current |
| 682 | ipmi::Privilege realPriv; |
| 683 | switch (priv) |
| 684 | { |
| 685 | case PRIVILEGE_CALLBACK: |
| 686 | realPriv = ipmi::Privilege::Callback; |
| 687 | break; |
| 688 | case PRIVILEGE_USER: |
| 689 | realPriv = ipmi::Privilege::User; |
| 690 | break; |
| 691 | case PRIVILEGE_OPERATOR: |
| 692 | realPriv = ipmi::Privilege::Operator; |
| 693 | break; |
| 694 | case PRIVILEGE_ADMIN: |
| 695 | realPriv = ipmi::Privilege::Admin; |
| 696 | break; |
| 697 | case PRIVILEGE_OEM: |
| 698 | realPriv = ipmi::Privilege::Oem; |
| 699 | break; |
| 700 | case SYSTEM_INTERFACE: |
| 701 | realPriv = ipmi::Privilege::Admin; |
| 702 | break; |
| 703 | default: |
| 704 | realPriv = ipmi::Privilege::Admin; |
| 705 | break; |
| 706 | } |
Vernon Mauery | e8d4323 | 2019-03-26 16:23:43 -0700 | [diff] [blame] | 707 | // The original ipmi_register_callback allowed for group OEM handlers |
| 708 | // to be registered via this same interface. It just so happened that |
| 709 | // all the handlers were part of the DCMI group, so default to that. |
| 710 | if (netFn == NETFUN_GRPEXT) |
| 711 | { |
| 712 | ipmi::impl::registerGroupHandler(ipmi::prioOpenBmcBase, |
| 713 | dcmi::groupExtId, cmd, realPriv, h); |
| 714 | } |
| 715 | else |
| 716 | { |
| 717 | ipmi::impl::registerHandler(ipmi::prioOpenBmcBase, netFn, cmd, realPriv, |
| 718 | h); |
| 719 | } |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 720 | } |
| 721 | |
Vernon Mauery | f984a01 | 2018-10-08 12:05:18 -0700 | [diff] [blame] | 722 | namespace oem |
| 723 | { |
| 724 | |
| 725 | class LegacyRouter : public oem::Router |
| 726 | { |
| 727 | public: |
| 728 | virtual ~LegacyRouter() |
| 729 | { |
| 730 | } |
| 731 | |
| 732 | /// Enable message routing to begin. |
| 733 | void activate() override |
| 734 | { |
| 735 | } |
| 736 | |
| 737 | void registerHandler(Number oen, ipmi_cmd_t cmd, Handler handler) override |
| 738 | { |
| 739 | auto h = ipmi::makeLegacyHandler(std::forward<Handler>(handler)); |
| 740 | ipmi::impl::registerOemHandler(ipmi::prioOpenBmcBase, oen, cmd, |
| 741 | ipmi::Privilege::Admin, h); |
| 742 | } |
| 743 | }; |
| 744 | static LegacyRouter legacyRouter; |
| 745 | |
| 746 | Router* mutableRouter() |
| 747 | { |
| 748 | return &legacyRouter; |
| 749 | } |
| 750 | |
| 751 | } // namespace oem |
| 752 | |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 753 | /* legacy alternative to executionEntry */ |
| 754 | void handleLegacyIpmiCommand(sdbusplus::message::message& m) |
| 755 | { |
Vernon Mauery | 23b7021 | 2019-05-08 15:19:05 -0700 | [diff] [blame] | 756 | // make a copy so the next two moves don't wreak havoc on the stack |
| 757 | sdbusplus::message::message b{m}; |
| 758 | boost::asio::spawn(*getIoContext(), [b = std::move(b)]( |
| 759 | boost::asio::yield_context yield) { |
| 760 | sdbusplus::message::message m{std::move(b)}; |
| 761 | unsigned char seq, netFn, lun, cmd; |
| 762 | std::vector<uint8_t> data; |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 763 | |
Vernon Mauery | 23b7021 | 2019-05-08 15:19:05 -0700 | [diff] [blame] | 764 | m.read(seq, netFn, lun, cmd, data); |
Vernon Mauery | 33298af | 2019-05-13 15:32:37 -0700 | [diff] [blame] | 765 | std::shared_ptr<sdbusplus::asio::connection> bus = getSdBus(); |
Vernon Mauery | 23b7021 | 2019-05-08 15:19:05 -0700 | [diff] [blame] | 766 | auto ctx = std::make_shared<ipmi::Context>( |
Johnathan Mantey | c11cc5c | 2020-07-22 13:52:33 -0700 | [diff] [blame] | 767 | bus, netFn, lun, cmd, 0, 0, 0, ipmi::Privilege::Admin, 0, yield); |
Vernon Mauery | 23b7021 | 2019-05-08 15:19:05 -0700 | [diff] [blame] | 768 | auto request = std::make_shared<ipmi::message::Request>( |
| 769 | ctx, std::forward<std::vector<uint8_t>>(data)); |
| 770 | ipmi::message::Response::ptr response = |
| 771 | ipmi::executeIpmiCommand(request); |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 772 | |
Vernon Mauery | 23b7021 | 2019-05-08 15:19:05 -0700 | [diff] [blame] | 773 | // Responses in IPMI require a bit set. So there ya go... |
| 774 | netFn |= 0x01; |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 775 | |
Vernon Mauery | 23b7021 | 2019-05-08 15:19:05 -0700 | [diff] [blame] | 776 | const char *dest, *path; |
| 777 | constexpr const char* DBUS_INTF = "org.openbmc.HostIpmi"; |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 778 | |
Vernon Mauery | 23b7021 | 2019-05-08 15:19:05 -0700 | [diff] [blame] | 779 | dest = m.get_sender(); |
| 780 | path = m.get_path(); |
| 781 | boost::system::error_code ec; |
Vernon Mauery | 33298af | 2019-05-13 15:32:37 -0700 | [diff] [blame] | 782 | bus->yield_method_call(yield, ec, dest, path, DBUS_INTF, "sendMessage", |
| 783 | seq, netFn, lun, cmd, response->cc, |
| 784 | response->payload.raw); |
Vernon Mauery | 23b7021 | 2019-05-08 15:19:05 -0700 | [diff] [blame] | 785 | if (ec) |
| 786 | { |
| 787 | log<level::ERR>("Failed to send response to requestor", |
| 788 | entry("ERROR=%s", ec.message().c_str()), |
| 789 | entry("SENDER=%s", dest), |
| 790 | entry("NETFN=0x%X", netFn), entry("CMD=0x%X", cmd)); |
| 791 | } |
| 792 | }); |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | #endif /* ALLOW_DEPRECATED_API */ |
| 796 | |
| 797 | // Calls host command manager to do the right thing for the command |
| 798 | using CommandHandler = phosphor::host::command::CommandHandler; |
| 799 | std::unique_ptr<phosphor::host::command::Manager> cmdManager; |
| 800 | void ipmid_send_cmd_to_host(CommandHandler&& cmd) |
| 801 | { |
Vernon Mauery | 1b7f6f2 | 2019-03-13 13:11:25 -0700 | [diff] [blame] | 802 | return cmdManager->execute(std::forward<CommandHandler>(cmd)); |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | std::unique_ptr<phosphor::host::command::Manager>& ipmid_get_host_cmd_manager() |
| 806 | { |
| 807 | return cmdManager; |
| 808 | } |
| 809 | |
Vernon Mauery | 20ff333 | 2019-03-01 16:52:25 -0800 | [diff] [blame] | 810 | // These are symbols that are present in libipmid, but not expected |
| 811 | // to be used except here (or maybe a unit test), so declare them here |
| 812 | extern void setIoContext(std::shared_ptr<boost::asio::io_context>& newIo); |
| 813 | extern void setSdBus(std::shared_ptr<sdbusplus::asio::connection>& newBus); |
| 814 | |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 815 | int main(int argc, char* argv[]) |
| 816 | { |
| 817 | // Connect to system bus |
Vernon Mauery | 20ff333 | 2019-03-01 16:52:25 -0800 | [diff] [blame] | 818 | auto io = std::make_shared<boost::asio::io_context>(); |
| 819 | setIoContext(io); |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 820 | if (argc > 1 && std::string(argv[1]) == "-session") |
| 821 | { |
| 822 | sd_bus_default_user(&bus); |
| 823 | } |
| 824 | else |
| 825 | { |
| 826 | sd_bus_default_system(&bus); |
| 827 | } |
Vernon Mauery | 20ff333 | 2019-03-01 16:52:25 -0800 | [diff] [blame] | 828 | auto sdbusp = std::make_shared<sdbusplus::asio::connection>(*io, bus); |
| 829 | setSdBus(sdbusp); |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 830 | |
| 831 | // TODO: Hack to keep the sdEvents running.... Not sure why the sd_event |
| 832 | // queue stops running if we don't have a timer that keeps re-arming |
| 833 | phosphor::Timer t2([]() { ; }); |
| 834 | t2.start(std::chrono::microseconds(500000), true); |
| 835 | |
| 836 | // TODO: Remove all vestiges of sd_event from phosphor-host-ipmid |
| 837 | // until that is done, add the sd_event wrapper to the io object |
| 838 | sdbusplus::asio::sd_event_wrapper sdEvents(*io); |
| 839 | |
| 840 | cmdManager = std::make_unique<phosphor::host::command::Manager>(*sdbusp); |
| 841 | |
| 842 | // Register all command providers and filters |
Vernon Mauery | 4ec4e40 | 2019-03-20 13:09:27 -0700 | [diff] [blame] | 843 | std::forward_list<ipmi::IpmiProvider> providers = |
| 844 | ipmi::loadProviders(HOST_IPMI_LIB_PATH); |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 845 | |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 846 | #ifdef ALLOW_DEPRECATED_API |
| 847 | // listen on deprecated signal interface for kcs/bt commands |
| 848 | constexpr const char* FILTER = "type='signal',interface='org.openbmc." |
| 849 | "HostIpmi',member='ReceivedMessage'"; |
| 850 | sdbusplus::bus::match::match oldIpmiInterface(*sdbusp, FILTER, |
| 851 | handleLegacyIpmiCommand); |
| 852 | #endif /* ALLOW_DEPRECATED_API */ |
| 853 | |
Vernon Mauery | 735ee95 | 2019-02-15 13:38:52 -0800 | [diff] [blame] | 854 | // set up bus name watching to match channels with bus names |
| 855 | sdbusplus::bus::match::match nameOwnerChanged( |
| 856 | *sdbusp, |
| 857 | sdbusplus::bus::match::rules::nameOwnerChanged() + |
| 858 | sdbusplus::bus::match::rules::arg0namespace( |
| 859 | ipmi::ipmiDbusChannelMatch), |
| 860 | ipmi::nameChangeHandler); |
| 861 | ipmi::doListNames(*io, *sdbusp); |
| 862 | |
James Feist | b0094a7 | 2019-11-26 09:07:15 -0800 | [diff] [blame] | 863 | int exitCode = 0; |
Vernon Mauery | 1b7f6f2 | 2019-03-13 13:11:25 -0700 | [diff] [blame] | 864 | // set up boost::asio signal handling |
| 865 | std::function<SignalResponse(int)> stopAsioRunLoop = |
James Feist | b0094a7 | 2019-11-26 09:07:15 -0800 | [diff] [blame] | 866 | [&io, &exitCode](int signalNumber) { |
Vernon Mauery | 1b7f6f2 | 2019-03-13 13:11:25 -0700 | [diff] [blame] | 867 | log<level::INFO>("Received signal; quitting", |
| 868 | entry("SIGNAL=%d", signalNumber)); |
| 869 | io->stop(); |
James Feist | b0094a7 | 2019-11-26 09:07:15 -0800 | [diff] [blame] | 870 | exitCode = signalNumber; |
Vernon Mauery | 1b7f6f2 | 2019-03-13 13:11:25 -0700 | [diff] [blame] | 871 | return SignalResponse::breakExecution; |
| 872 | }; |
| 873 | registerSignalHandler(ipmi::prioOpenBmcBase, SIGINT, stopAsioRunLoop); |
| 874 | registerSignalHandler(ipmi::prioOpenBmcBase, SIGTERM, stopAsioRunLoop); |
| 875 | |
Richard Marian Thomaiyar | 369406e | 2020-01-09 14:56:54 +0530 | [diff] [blame] | 876 | sdbusp->request_name("xyz.openbmc_project.Ipmi.Host"); |
| 877 | // Add bindings for inbound IPMI requests |
| 878 | auto server = sdbusplus::asio::object_server(sdbusp); |
| 879 | auto iface = server.add_interface("/xyz/openbmc_project/Ipmi", |
| 880 | "xyz.openbmc_project.Ipmi.Server"); |
| 881 | iface->register_method("execute", ipmi::executionEntry); |
| 882 | iface->initialize(); |
| 883 | |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 884 | io->run(); |
| 885 | |
Vernon Mauery | 1b7f6f2 | 2019-03-13 13:11:25 -0700 | [diff] [blame] | 886 | // destroy all the IPMI handlers so the providers can unload safely |
| 887 | ipmi::handlerMap.clear(); |
| 888 | ipmi::groupHandlerMap.clear(); |
| 889 | ipmi::oemHandlerMap.clear(); |
| 890 | ipmi::filterList.clear(); |
| 891 | // unload the provider libraries |
Vernon Mauery | 4ec4e40 | 2019-03-20 13:09:27 -0700 | [diff] [blame] | 892 | providers.clear(); |
Vernon Mauery | 1b7f6f2 | 2019-03-13 13:11:25 -0700 | [diff] [blame] | 893 | |
James Feist | b0094a7 | 2019-11-26 09:07:15 -0800 | [diff] [blame] | 894 | std::exit(exitCode); |
Vernon Mauery | 240b186 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 895 | } |