Andrew Geissler | b09463d | 2017-03-24 15:55:17 -0500 | [diff] [blame] | 1 | #include <cstdlib> |
Andrew Geissler | 0971dea | 2017-03-28 14:32:40 -0500 | [diff] [blame] | 2 | #include <unistd.h> |
Andrew Geissler | 07d0ed5 | 2017-03-28 15:44:18 -0500 | [diff] [blame] | 3 | #include <iostream> |
| 4 | #include <fstream> |
| 5 | #include <cstdio> |
Andrew Geissler | b09463d | 2017-03-24 15:55:17 -0500 | [diff] [blame] | 6 | #include <sdbusplus/bus.hpp> |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 7 | #include <sdbusplus/exception.hpp> |
Andrew Geissler | b09463d | 2017-03-24 15:55:17 -0500 | [diff] [blame] | 8 | #include <phosphor-logging/log.hpp> |
Andrew Geissler | 0971dea | 2017-03-28 14:32:40 -0500 | [diff] [blame] | 9 | #include <xyz/openbmc_project/Control/Host/server.hpp> |
Andrew Geissler | 769a62f | 2019-12-06 13:36:08 -0600 | [diff] [blame^] | 10 | #include "config.h" |
Andrew Geissler | b09463d | 2017-03-24 15:55:17 -0500 | [diff] [blame] | 11 | |
Andrew Geissler | eeaccf8 | 2017-03-28 15:23:27 -0500 | [diff] [blame] | 12 | using namespace std::literals; |
Andrew Geissler | b09463d | 2017-03-24 15:55:17 -0500 | [diff] [blame] | 13 | using namespace phosphor::logging; |
Andrew Geissler | 0971dea | 2017-03-28 14:32:40 -0500 | [diff] [blame] | 14 | using namespace sdbusplus::xyz::openbmc_project::Control::server; |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 15 | using sdbusplus::exception::SdBusError; |
Andrew Geissler | 0971dea | 2017-03-28 14:32:40 -0500 | [diff] [blame] | 16 | |
Andrew Geissler | eeaccf8 | 2017-03-28 15:23:27 -0500 | [diff] [blame] | 17 | // Required strings for sending the msg to check on host |
| 18 | constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper"; |
| 19 | constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper"; |
| 20 | constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper"; |
Andrew Geissler | 624161c | 2019-04-09 16:01:01 -0500 | [diff] [blame] | 21 | constexpr auto CONTROL_HOST_DEFAULT_SVC = "xyz.openbmc_project.Control.Host"; |
Andrew Geissler | eeaccf8 | 2017-03-28 15:23:27 -0500 | [diff] [blame] | 22 | constexpr auto CONTROL_HOST_PATH = "/xyz/openbmc_project/control/host0"; |
| 23 | constexpr auto CONTROL_HOST_INTERFACE = "xyz.openbmc_project.Control.Host"; |
Andrew Geissler | 0971dea | 2017-03-28 14:32:40 -0500 | [diff] [blame] | 24 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 25 | bool cmdDone = false; |
Andrew Geissler | 0971dea | 2017-03-28 14:32:40 -0500 | [diff] [blame] | 26 | bool hostRunning = false; |
| 27 | |
| 28 | // Function called on host control signals |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 29 | static int hostControlSignal(sd_bus_message* msg, void* userData, |
Andrew Geissler | 0971dea | 2017-03-28 14:32:40 -0500 | [diff] [blame] | 30 | sd_bus_error* retError) |
| 31 | { |
Andrew Geissler | 769a62f | 2019-12-06 13:36:08 -0600 | [diff] [blame^] | 32 | // retError and userData are not used |
| 33 | (void)(retError); |
| 34 | (void)(userData); |
Andrew Geissler | 0971dea | 2017-03-28 14:32:40 -0500 | [diff] [blame] | 35 | std::string cmdCompleted{}; |
| 36 | std::string cmdStatus{}; |
| 37 | |
| 38 | auto sdPlusMsg = sdbusplus::message::message(msg); |
| 39 | sdPlusMsg.read(cmdCompleted, cmdStatus); |
| 40 | |
| 41 | log<level::DEBUG>("Host control signal values", |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 42 | entry("COMMAND=%s", cmdCompleted.c_str()), |
| 43 | entry("STATUS=%s", cmdStatus.c_str())); |
Andrew Geissler | 0971dea | 2017-03-28 14:32:40 -0500 | [diff] [blame] | 44 | |
| 45 | // Verify it's the command this code is interested in and then check status |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 46 | if (Host::convertCommandFromString(cmdCompleted) == |
| 47 | Host::Command::Heartbeat) |
Andrew Geissler | 0971dea | 2017-03-28 14:32:40 -0500 | [diff] [blame] | 48 | { |
| 49 | cmdDone = true; |
| 50 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 51 | if (Host::convertResultFromString(cmdStatus) == Host::Result::Success) |
Andrew Geissler | 0971dea | 2017-03-28 14:32:40 -0500 | [diff] [blame] | 52 | { |
| 53 | hostRunning = true; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | return 0; |
| 58 | } |
Andrew Geissler | b09463d | 2017-03-24 15:55:17 -0500 | [diff] [blame] | 59 | |
Andrew Geissler | eeaccf8 | 2017-03-28 15:23:27 -0500 | [diff] [blame] | 60 | // Send hearbeat to host to determine if it's running |
| 61 | void sendHeartbeat(sdbusplus::bus::bus& bus) |
| 62 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 63 | auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, |
| 64 | MAPPER_INTERFACE, "GetObject"); |
Andrew Geissler | eeaccf8 | 2017-03-28 15:23:27 -0500 | [diff] [blame] | 65 | |
| 66 | mapper.append(CONTROL_HOST_PATH, |
| 67 | std::vector<std::string>({CONTROL_HOST_INTERFACE})); |
Andrew Geissler | eeaccf8 | 2017-03-28 15:23:27 -0500 | [diff] [blame] | 68 | |
| 69 | std::map<std::string, std::vector<std::string>> mapperResponse; |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 70 | |
| 71 | try |
| 72 | { |
| 73 | auto mapperResponseMsg = bus.call(mapper); |
| 74 | mapperResponseMsg.read(mapperResponse); |
| 75 | } |
| 76 | catch (const SdBusError& e) |
| 77 | { |
Andrew Geissler | 624161c | 2019-04-09 16:01:01 -0500 | [diff] [blame] | 78 | log<level::INFO>("Error in mapper call for control host, use default " |
| 79 | "service", |
| 80 | entry("ERROR=%s", e.what())); |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 81 | } |
| 82 | |
Andrew Geissler | 624161c | 2019-04-09 16:01:01 -0500 | [diff] [blame] | 83 | std::string host; |
| 84 | if (!mapperResponse.empty()) |
Andrew Geissler | eeaccf8 | 2017-03-28 15:23:27 -0500 | [diff] [blame] | 85 | { |
Andrew Geissler | 624161c | 2019-04-09 16:01:01 -0500 | [diff] [blame] | 86 | log<level::DEBUG>("Use mapper response"); |
| 87 | host = mapperResponse.begin()->first; |
Andrew Geissler | eeaccf8 | 2017-03-28 15:23:27 -0500 | [diff] [blame] | 88 | } |
Andrew Geissler | 624161c | 2019-04-09 16:01:01 -0500 | [diff] [blame] | 89 | else |
| 90 | { |
| 91 | log<level::DEBUG>("Use hard coded host"); |
| 92 | host = CONTROL_HOST_DEFAULT_SVC; |
| 93 | } |
Andrew Geissler | eeaccf8 | 2017-03-28 15:23:27 -0500 | [diff] [blame] | 94 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 95 | auto method = bus.new_method_call(host.c_str(), CONTROL_HOST_PATH, |
| 96 | CONTROL_HOST_INTERFACE, "Execute"); |
Andrew Geissler | eeaccf8 | 2017-03-28 15:23:27 -0500 | [diff] [blame] | 97 | method.append(convertForMessage(Host::Command::Heartbeat).c_str()); |
| 98 | |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 99 | try |
Andrew Geissler | eeaccf8 | 2017-03-28 15:23:27 -0500 | [diff] [blame] | 100 | { |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 101 | auto reply = bus.call(method); |
| 102 | } |
| 103 | catch (const SdBusError& e) |
| 104 | { |
| 105 | log<level::ERR>("Error in call to control host Execute", |
| 106 | entry("ERROR=%s", e.what())); |
| 107 | throw; |
Andrew Geissler | eeaccf8 | 2017-03-28 15:23:27 -0500 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | return; |
| 111 | } |
| 112 | |
Andrew Geissler | 769a62f | 2019-12-06 13:36:08 -0600 | [diff] [blame^] | 113 | int main() |
Andrew Geissler | b09463d | 2017-03-24 15:55:17 -0500 | [diff] [blame] | 114 | { |
| 115 | log<level::INFO>("Check if host is running"); |
| 116 | |
Andrew Geissler | 0971dea | 2017-03-28 14:32:40 -0500 | [diff] [blame] | 117 | auto bus = sdbusplus::bus::new_default(); |
Andrew Geissler | b09463d | 2017-03-24 15:55:17 -0500 | [diff] [blame] | 118 | |
Andrew Geissler | 66060bf | 2019-09-12 13:47:05 -0500 | [diff] [blame] | 119 | std::string s = "type='signal',member='CommandComplete',path='"s + |
| 120 | CONTROL_HOST_PATH + "',interface='" + |
| 121 | CONTROL_HOST_INTERFACE + "'"; |
Andrew Geissler | eeaccf8 | 2017-03-28 15:23:27 -0500 | [diff] [blame] | 122 | |
Andrew Geissler | 0971dea | 2017-03-28 14:32:40 -0500 | [diff] [blame] | 123 | // Setup Signal Handler |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 124 | sdbusplus::bus::match::match hostControlSignals(bus, s.c_str(), |
| 125 | hostControlSignal, nullptr); |
Andrew Geissler | 0971dea | 2017-03-28 14:32:40 -0500 | [diff] [blame] | 126 | |
Andrew Geissler | eeaccf8 | 2017-03-28 15:23:27 -0500 | [diff] [blame] | 127 | sendHeartbeat(bus); |
Andrew Geissler | b09463d | 2017-03-24 15:55:17 -0500 | [diff] [blame] | 128 | |
| 129 | // Wait for signal |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 130 | while (!cmdDone) |
Andrew Geissler | 0971dea | 2017-03-28 14:32:40 -0500 | [diff] [blame] | 131 | { |
| 132 | bus.process_discard(); |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 133 | if (cmdDone) |
| 134 | break; |
Andrew Geissler | 0971dea | 2017-03-28 14:32:40 -0500 | [diff] [blame] | 135 | bus.wait(); |
| 136 | } |
Andrew Geissler | b09463d | 2017-03-24 15:55:17 -0500 | [diff] [blame] | 137 | |
| 138 | // If host running then create file |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 139 | if (hostRunning) |
Andrew Geissler | 0971dea | 2017-03-28 14:32:40 -0500 | [diff] [blame] | 140 | { |
Andrew Geissler | 0971dea | 2017-03-28 14:32:40 -0500 | [diff] [blame] | 141 | log<level::INFO>("Host is running!"); |
Andrew Geissler | 07d0ed5 | 2017-03-28 15:44:18 -0500 | [diff] [blame] | 142 | // Create file for host instance and create in filesystem to indicate |
| 143 | // to services that host is running |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 144 | auto size = std::snprintf(nullptr, 0, HOST_RUNNING_FILE, 0); |
Andrew Geissler | 0135bd5 | 2017-05-09 13:54:10 -0500 | [diff] [blame] | 145 | size++; // null |
| 146 | std::unique_ptr<char[]> buf(new char[size]); |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 147 | std::snprintf(buf.get(), size, HOST_RUNNING_FILE, 0); |
Andrew Geissler | 07d0ed5 | 2017-03-28 15:44:18 -0500 | [diff] [blame] | 148 | std::ofstream outfile(buf.get()); |
| 149 | outfile.close(); |
| 150 | } |
| 151 | else |
| 152 | { |
| 153 | log<level::INFO>("Host is not running!"); |
Andrew Geissler | 0971dea | 2017-03-28 14:32:40 -0500 | [diff] [blame] | 154 | } |
Andrew Geissler | b09463d | 2017-03-24 15:55:17 -0500 | [diff] [blame] | 155 | |
| 156 | return 0; |
| 157 | } |