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