Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright 2017 Google Inc. |
| 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 | |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 17 | #ifdef ENABLE_IPMI_SNOOP |
| 18 | #include "ipmisnoop/ipmisnoop.hpp" |
| 19 | #endif |
| 20 | |
Patrick Venture | b5754fd | 2018-09-10 13:13:58 -0700 | [diff] [blame] | 21 | #include "lpcsnoop/snoop.hpp" |
| 22 | |
William A. Kennington III | 6dac4c5 | 2019-12-13 15:05:00 -0800 | [diff] [blame] | 23 | #include <endian.h> |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 24 | #include <fcntl.h> |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 25 | #include <getopt.h> |
Patrick Venture | b5754fd | 2018-09-10 13:13:58 -0700 | [diff] [blame] | 26 | #include <sys/epoll.h> |
| 27 | #include <systemd/sd-event.h> |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 28 | #include <unistd.h> |
| 29 | |
Kun Yi | 1c16ad8 | 2018-09-12 10:01:49 -0700 | [diff] [blame] | 30 | #include <sdeventplus/event.hpp> |
| 31 | #include <sdeventplus/source/event.hpp> |
| 32 | #include <sdeventplus/source/io.hpp> |
William A. Kennington III | 6a5e0a1 | 2021-12-19 20:47:34 -0800 | [diff] [blame] | 33 | #include <sdeventplus/source/signal.hpp> |
Jonathan Doman | 38b0946 | 2023-04-26 11:45:39 -0700 | [diff] [blame] | 34 | #include <sdeventplus/source/time.hpp> |
William A. Kennington III | 30751ec | 2022-11-21 18:38:36 -0800 | [diff] [blame] | 35 | #include <sdeventplus/utility/sdbus.hpp> |
William A. Kennington III | 6a5e0a1 | 2021-12-19 20:47:34 -0800 | [diff] [blame] | 36 | #include <stdplus/signal.hpp> |
Patrick Williams | 0ea7357 | 2023-05-10 07:50:44 -0500 | [diff] [blame^] | 37 | |
| 38 | #include <chrono> |
| 39 | #include <cstdint> |
| 40 | #include <exception> |
| 41 | #include <functional> |
| 42 | #include <iostream> |
| 43 | #include <optional> |
| 44 | #include <span> |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 45 | #include <thread> |
| 46 | |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 47 | #ifdef ENABLE_IPMI_SNOOP |
| 48 | #include <xyz/openbmc_project/State/Boot/Raw/server.hpp> |
| 49 | #endif |
| 50 | |
Benjamin Fair | f69ad7e | 2018-07-13 13:41:10 -0700 | [diff] [blame] | 51 | static size_t codeSize = 1; /* Size of each POST code in bytes */ |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 52 | const char* defaultHostInstances = "0"; |
Jonathan Doman | 38b0946 | 2023-04-26 11:45:39 -0700 | [diff] [blame] | 53 | static bool verbose = false; |
Brad Bishop | 8f6c0ce | 2022-05-03 17:29:05 -0400 | [diff] [blame] | 54 | #ifdef ENABLE_IPMI_SNOOP |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 55 | const uint8_t minPositionVal = 0; |
| 56 | const uint8_t maxPositionVal = 5; |
Brad Bishop | 8f6c0ce | 2022-05-03 17:29:05 -0400 | [diff] [blame] | 57 | #endif |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 58 | |
| 59 | #ifdef ENABLE_IPMI_SNOOP |
| 60 | std::vector<std::unique_ptr<IpmiPostReporter>> reporters; |
| 61 | #endif |
| 62 | |
| 63 | #ifdef ENABLE_IPMI_SNOOP |
Patrick Williams | aebf87c | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 64 | void IpmiPostReporter::getSelectorPositionSignal(sdbusplus::bus_t& bus) |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 65 | { |
| 66 | size_t posVal = 0; |
| 67 | |
| 68 | matchSignal = std::make_unique<sdbusplus::bus::match_t>( |
| 69 | bus, |
| 70 | sdbusplus::bus::match::rules::propertiesChanged(selectorObject, |
| 71 | selectorIface), |
Patrick Williams | aebf87c | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 72 | [&](sdbusplus::message_t& msg) { |
Patrick Williams | 0ea7357 | 2023-05-10 07:50:44 -0500 | [diff] [blame^] | 73 | std::string objectName; |
| 74 | std::map<std::string, Selector::PropertiesVariant> msgData; |
| 75 | msg.read(objectName, msgData); |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 76 | |
Patrick Williams | 0ea7357 | 2023-05-10 07:50:44 -0500 | [diff] [blame^] | 77 | auto valPropMap = msgData.find("Position"); |
| 78 | { |
| 79 | if (valPropMap == msgData.end()) |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 80 | { |
Patrick Williams | 0ea7357 | 2023-05-10 07:50:44 -0500 | [diff] [blame^] | 81 | std::cerr << "Position property not found " << std::endl; |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | posVal = std::get<size_t>(valPropMap->second); |
| 86 | |
| 87 | if (posVal > minPositionVal && posVal < maxPositionVal) |
| 88 | { |
| 89 | std::tuple<uint64_t, secondary_post_code_t> postcodes = |
| 90 | reporters[posVal - 1]->value(); |
| 91 | uint64_t postcode = std::get<uint64_t>(postcodes); |
| 92 | |
| 93 | // write postcode into seven segment display |
| 94 | if (postCodeDisplay(postcode) < 0) |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 95 | { |
Patrick Williams | 0ea7357 | 2023-05-10 07:50:44 -0500 | [diff] [blame^] | 96 | fprintf(stderr, "Error in display the postcode\n"); |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 97 | } |
| 98 | } |
Patrick Williams | 0ea7357 | 2023-05-10 07:50:44 -0500 | [diff] [blame^] | 99 | } |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 100 | }); |
| 101 | } |
| 102 | #endif |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 103 | |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 104 | static void usage(const char* name) |
| 105 | { |
| 106 | fprintf(stderr, |
| 107 | "Usage: %s [-d <DEVICE>]\n" |
Benjamin Fair | f69ad7e | 2018-07-13 13:41:10 -0700 | [diff] [blame] | 108 | " -b, --bytes <SIZE> set POST code length to <SIZE> bytes. " |
| 109 | "Default is %zu\n" |
Manojkiran Eda | aade4ad | 2021-02-19 11:20:33 +0530 | [diff] [blame] | 110 | " -d, --device <DEVICE> use <DEVICE> file.\n" |
Jonathan Doman | fd2430d | 2023-05-03 11:44:01 -0700 | [diff] [blame] | 111 | " -r, --rate-limit=<N> Only process N POST codes from the " |
| 112 | "device per second.\n" |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 113 | " -h, --host <host instances> . Default is '%s'\n" |
William A. Kennington III | 66efa63 | 2020-04-16 18:46:20 -0700 | [diff] [blame] | 114 | " -v, --verbose Prints verbose information while running\n\n", |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 115 | name, codeSize, defaultHostInstances); |
Benjamin Fair | f69ad7e | 2018-07-13 13:41:10 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Jonathan Doman | 38b0946 | 2023-04-26 11:45:39 -0700 | [diff] [blame] | 118 | /** |
| 119 | * Call once for each POST code received. If the number of POST codes exceeds |
| 120 | * the configured rate limit, this function will disable the snoop device IO |
| 121 | * source until the end of the 1 second interval, then re-enable it. |
| 122 | * |
| 123 | * @return Whether the rate limit is exceeded. |
| 124 | */ |
| 125 | bool rateLimit(PostReporter& reporter, sdeventplus::source::IO& ioSource) |
| 126 | { |
| 127 | if (reporter.rateLimit == 0) |
| 128 | { |
| 129 | // Rate limiting is disabled. |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | using Clock = sdeventplus::Clock<sdeventplus::ClockId::Monotonic>; |
| 134 | |
| 135 | static constexpr std::chrono::seconds rateLimitInterval(1); |
| 136 | static unsigned int rateLimitCount = 0; |
| 137 | static Clock::time_point rateLimitEndTime; |
| 138 | |
| 139 | const sdeventplus::Event& event = ioSource.get_event(); |
| 140 | |
| 141 | if (rateLimitCount == 0) |
| 142 | { |
| 143 | // Initialize the end time when we start a new interval |
| 144 | rateLimitEndTime = Clock(event).now() + rateLimitInterval; |
| 145 | } |
| 146 | |
| 147 | if (++rateLimitCount < reporter.rateLimit) |
| 148 | { |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | rateLimitCount = 0; |
| 153 | |
| 154 | if (rateLimitEndTime < Clock(event).now()) |
| 155 | { |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | if (verbose) |
| 160 | { |
| 161 | fprintf(stderr, "Hit POST code rate limit - disabling temporarily\n"); |
| 162 | } |
| 163 | |
| 164 | ioSource.set_enabled(sdeventplus::source::Enabled::Off); |
| 165 | sdeventplus::source::Time<sdeventplus::ClockId::Monotonic>( |
| 166 | event, rateLimitEndTime, std::chrono::milliseconds(100), |
| 167 | [&ioSource](auto&, auto) { |
Patrick Williams | 0ea7357 | 2023-05-10 07:50:44 -0500 | [diff] [blame^] | 168 | if (verbose) |
| 169 | { |
| 170 | fprintf(stderr, "Reenabling POST code handler\n"); |
| 171 | } |
| 172 | ioSource.set_enabled(sdeventplus::source::Enabled::On); |
Jonathan Doman | 38b0946 | 2023-04-26 11:45:39 -0700 | [diff] [blame] | 173 | }) |
| 174 | .set_floating(true); |
| 175 | return true; |
| 176 | } |
| 177 | |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 178 | /* |
Kun Yi | eb31231 | 2018-06-13 09:20:50 -0700 | [diff] [blame] | 179 | * Callback handling IO event from the POST code fd. i.e. there is new |
| 180 | * POST code available to read. |
| 181 | */ |
Jonathan Doman | 38b0946 | 2023-04-26 11:45:39 -0700 | [diff] [blame] | 182 | void PostCodeEventHandler(PostReporter* reporter, sdeventplus::source::IO& s, |
| 183 | int postFd, uint32_t) |
Kun Yi | eb31231 | 2018-06-13 09:20:50 -0700 | [diff] [blame] | 184 | { |
William A. Kennington III | 6dac4c5 | 2019-12-13 15:05:00 -0800 | [diff] [blame] | 185 | uint64_t code = 0; |
William A. Kennington III | 0f964b4 | 2020-04-16 18:46:03 -0700 | [diff] [blame] | 186 | ssize_t readb; |
Jonathan Doman | 38b0946 | 2023-04-26 11:45:39 -0700 | [diff] [blame] | 187 | |
William A. Kennington III | 0f964b4 | 2020-04-16 18:46:03 -0700 | [diff] [blame] | 188 | while ((readb = read(postFd, &code, codeSize)) > 0) |
Kun Yi | 1c16ad8 | 2018-09-12 10:01:49 -0700 | [diff] [blame] | 189 | { |
William A. Kennington III | f21475a | 2019-12-13 17:21:24 -0800 | [diff] [blame] | 190 | code = le64toh(code); |
William A. Kennington III | 66efa63 | 2020-04-16 18:46:20 -0700 | [diff] [blame] | 191 | if (verbose) |
| 192 | { |
| 193 | fprintf(stderr, "Code: 0x%" PRIx64 "\n", code); |
| 194 | } |
William A. Kennington III | f21475a | 2019-12-13 17:21:24 -0800 | [diff] [blame] | 195 | // HACK: Always send property changed signal even for the same code |
| 196 | // since we are single threaded, external users will never see the |
| 197 | // first value. |
Manojkiran Eda | ba5258f | 2021-02-25 13:23:33 +0530 | [diff] [blame] | 198 | reporter->value(std::make_tuple(~code, secondary_post_code_t{}), true); |
| 199 | reporter->value(std::make_tuple(code, secondary_post_code_t{})); |
William A. Kennington III | 0f964b4 | 2020-04-16 18:46:03 -0700 | [diff] [blame] | 200 | |
| 201 | // read depends on old data being cleared since it doens't always read |
| 202 | // the full code size |
| 203 | code = 0; |
Jonathan Doman | 38b0946 | 2023-04-26 11:45:39 -0700 | [diff] [blame] | 204 | |
| 205 | if (rateLimit(*reporter, s)) |
| 206 | { |
| 207 | return; |
| 208 | } |
Kun Yi | 1c16ad8 | 2018-09-12 10:01:49 -0700 | [diff] [blame] | 209 | } |
William A. Kennington III | 0f964b4 | 2020-04-16 18:46:03 -0700 | [diff] [blame] | 210 | |
| 211 | if (readb < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) |
| 212 | { |
| 213 | return; |
| 214 | } |
| 215 | |
| 216 | /* Read failure. */ |
| 217 | if (readb == 0) |
| 218 | { |
| 219 | fprintf(stderr, "Unexpected EOF reading postcode\n"); |
| 220 | } |
| 221 | else |
| 222 | { |
| 223 | fprintf(stderr, "Failed to read postcode: %s\n", strerror(errno)); |
| 224 | } |
| 225 | s.get_event().exit(1); |
Kun Yi | eb31231 | 2018-06-13 09:20:50 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 228 | #ifdef ENABLE_IPMI_SNOOP |
| 229 | // handle muti-host D-bus |
| 230 | int postCodeIpmiHandler(const std::string& snoopObject, |
Patrick Williams | aebf87c | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 231 | const std::string& snoopDbus, sdbusplus::bus_t& bus, |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 232 | std::span<std::string> host) |
| 233 | { |
| 234 | int ret = 0; |
| 235 | |
| 236 | try |
| 237 | { |
| 238 | for (size_t iteration = 0; iteration < host.size(); iteration++) |
| 239 | { |
| 240 | std::string objPathInst = snoopObject + host[iteration]; |
| 241 | |
| 242 | sdbusplus::server::manager_t m{bus, objPathInst.c_str()}; |
| 243 | |
| 244 | /* Create a monitor object and let it do all the rest */ |
| 245 | reporters.emplace_back( |
| 246 | std::make_unique<IpmiPostReporter>(bus, objPathInst.c_str())); |
| 247 | |
| 248 | reporters[iteration]->emit_object_added(); |
| 249 | } |
| 250 | |
| 251 | bus.request_name(snoopDbus.c_str()); |
Kumar Thangavel | aee6540 | 2022-08-09 17:21:48 +0530 | [diff] [blame] | 252 | |
| 253 | /* sevenSegmentLedEnabled flag is unset when GPIO pins are not there 7 |
| 254 | seg display for fewer platforms. So, the code for postcode dispay and |
| 255 | Get Selector position can be skipped in those platforms. |
| 256 | */ |
| 257 | if (sevenSegmentLedEnabled) |
| 258 | { |
| 259 | reporters[0]->getSelectorPositionSignal(bus); |
| 260 | } |
| 261 | else |
| 262 | { |
| 263 | reporters.clear(); |
| 264 | } |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 265 | } |
| 266 | catch (const std::exception& e) |
| 267 | { |
| 268 | fprintf(stderr, "%s\n", e.what()); |
| 269 | } |
| 270 | |
| 271 | // Configure seven segment dsiplay connected to GPIOs as output |
| 272 | ret = configGPIODirOutput(); |
| 273 | if (ret < 0) |
| 274 | { |
Kumar Thangavel | aee6540 | 2022-08-09 17:21:48 +0530 | [diff] [blame] | 275 | fprintf(stderr, "Failed find the gpio line. Cannot display postcodes " |
| 276 | "in seven segment display..\n"); |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | while (true) |
| 280 | { |
| 281 | bus.process_discard(); |
| 282 | bus.wait(); |
| 283 | } |
| 284 | exit(EXIT_SUCCESS); |
| 285 | } |
| 286 | #endif |
| 287 | |
Kun Yi | eb31231 | 2018-06-13 09:20:50 -0700 | [diff] [blame] | 288 | /* |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 289 | * TODO(venture): this only listens one of the possible snoop ports, but |
| 290 | * doesn't share the namespace. |
| 291 | * |
| 292 | * This polls() the lpc snoop character device and it owns the dbus object |
| 293 | * whose value is the latest port 80h value. |
| 294 | */ |
| 295 | int main(int argc, char* argv[]) |
| 296 | { |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 297 | #ifndef ENABLE_IPMI_SNOOP |
| 298 | int postFd = -1; |
Jonathan Doman | 38b0946 | 2023-04-26 11:45:39 -0700 | [diff] [blame] | 299 | unsigned int rateLimit = 0; |
Jonathan Doman | fd2430d | 2023-05-03 11:44:01 -0700 | [diff] [blame] | 300 | #endif |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 301 | |
| 302 | int opt; |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 303 | |
| 304 | #ifdef ENABLE_IPMI_SNOOP |
| 305 | std::vector<std::string> host; |
| 306 | #endif |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 307 | /* |
| 308 | * These string constants are only used in this method within this object |
| 309 | * and this object is the only object feeding into the final binary. |
| 310 | * |
| 311 | * If however, another object is added to this binary it would be proper |
| 312 | * to move these declarations to be global and extern to the other object. |
| 313 | */ |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 314 | |
Patrick Venture | 1ceb21b | 2018-08-08 11:29:36 -0700 | [diff] [blame] | 315 | // clang-format off |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 316 | static const struct option long_options[] = { |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 317 | #ifdef ENABLE_IPMI_SNOOP |
| 318 | {"host", optional_argument, NULL, 'h'}, |
| 319 | #endif |
Patrick Venture | 1ceb21b | 2018-08-08 11:29:36 -0700 | [diff] [blame] | 320 | {"bytes", required_argument, NULL, 'b'}, |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 321 | #ifndef ENABLE_IPMI_SNOOP |
Manojkiran Eda | aade4ad | 2021-02-19 11:20:33 +0530 | [diff] [blame] | 322 | {"device", optional_argument, NULL, 'd'}, |
Jonathan Doman | 38b0946 | 2023-04-26 11:45:39 -0700 | [diff] [blame] | 323 | {"rate-limit", optional_argument, NULL, 'r'}, |
Jonathan Doman | fd2430d | 2023-05-03 11:44:01 -0700 | [diff] [blame] | 324 | #endif |
William A. Kennington III | 66efa63 | 2020-04-16 18:46:20 -0700 | [diff] [blame] | 325 | {"verbose", no_argument, NULL, 'v'}, |
Patrick Venture | 1ceb21b | 2018-08-08 11:29:36 -0700 | [diff] [blame] | 326 | {0, 0, 0, 0} |
| 327 | }; |
| 328 | // clang-format on |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 329 | |
Jonathan Doman | 38b0946 | 2023-04-26 11:45:39 -0700 | [diff] [blame] | 330 | while ((opt = getopt_long(argc, argv, "h:b:d:r:v", long_options, NULL)) != |
| 331 | -1) |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 332 | { |
| 333 | switch (opt) |
| 334 | { |
| 335 | case 0: |
| 336 | break; |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 337 | #ifdef ENABLE_IPMI_SNOOP |
Patrick Williams | 0ea7357 | 2023-05-10 07:50:44 -0500 | [diff] [blame^] | 338 | case 'h': |
| 339 | { |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 340 | std::string_view instances = optarg; |
| 341 | size_t pos = 0; |
| 342 | |
| 343 | while ((pos = instances.find(" ")) != std::string::npos) |
| 344 | { |
| 345 | host.emplace_back(instances.substr(0, pos)); |
| 346 | instances.remove_prefix(pos + 1); |
| 347 | } |
| 348 | host.emplace_back(instances); |
| 349 | break; |
| 350 | } |
| 351 | #endif |
Patrick Williams | 0ea7357 | 2023-05-10 07:50:44 -0500 | [diff] [blame^] | 352 | case 'b': |
| 353 | { |
Benjamin Fair | f69ad7e | 2018-07-13 13:41:10 -0700 | [diff] [blame] | 354 | codeSize = atoi(optarg); |
| 355 | |
| 356 | if (codeSize < 1 || codeSize > 8) |
| 357 | { |
| 358 | fprintf(stderr, |
| 359 | "Invalid POST code size '%s'. Must be " |
| 360 | "an integer from 1 to 8.\n", |
| 361 | optarg); |
| 362 | exit(EXIT_FAILURE); |
| 363 | } |
| 364 | break; |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 365 | } |
| 366 | #ifndef ENABLE_IPMI_SNOOP |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 367 | case 'd': |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 368 | |
Manojkiran Eda | aade4ad | 2021-02-19 11:20:33 +0530 | [diff] [blame] | 369 | postFd = open(optarg, O_NONBLOCK); |
| 370 | if (postFd < 0) |
| 371 | { |
| 372 | fprintf(stderr, "Unable to open: %s\n", optarg); |
| 373 | return -1; |
| 374 | } |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 375 | break; |
Patrick Williams | 0ea7357 | 2023-05-10 07:50:44 -0500 | [diff] [blame^] | 376 | case 'r': |
| 377 | { |
Jonathan Doman | 38b0946 | 2023-04-26 11:45:39 -0700 | [diff] [blame] | 378 | int argVal = -1; |
| 379 | try |
| 380 | { |
| 381 | argVal = std::stoi(optarg); |
| 382 | } |
| 383 | catch (...) |
Patrick Williams | 0ea7357 | 2023-05-10 07:50:44 -0500 | [diff] [blame^] | 384 | {} |
Jonathan Doman | 38b0946 | 2023-04-26 11:45:39 -0700 | [diff] [blame] | 385 | |
| 386 | if (argVal < 1) |
| 387 | { |
| 388 | fprintf(stderr, "Invalid rate limit '%s'. Must be >= 1.\n", |
| 389 | optarg); |
| 390 | return EXIT_FAILURE; |
| 391 | } |
| 392 | |
| 393 | rateLimit = static_cast<unsigned int>(argVal); |
| 394 | fprintf(stderr, "Rate limiting to %d POST codes per second.\n", |
| 395 | argVal); |
| 396 | break; |
| 397 | } |
Jonathan Doman | fd2430d | 2023-05-03 11:44:01 -0700 | [diff] [blame] | 398 | #endif |
William A. Kennington III | 66efa63 | 2020-04-16 18:46:20 -0700 | [diff] [blame] | 399 | case 'v': |
| 400 | verbose = true; |
| 401 | break; |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 402 | default: |
| 403 | usage(argv[0]); |
Jonathan Doman | 38b0946 | 2023-04-26 11:45:39 -0700 | [diff] [blame] | 404 | return EXIT_FAILURE; |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 408 | auto bus = sdbusplus::bus::new_default(); |
| 409 | |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 410 | #ifdef ENABLE_IPMI_SNOOP |
| 411 | std::cout << "Verbose = " << verbose << std::endl; |
| 412 | int ret = postCodeIpmiHandler(ipmiSnoopObject, snoopDbus, bus, host); |
| 413 | if (ret < 0) |
| 414 | { |
| 415 | fprintf(stderr, "Error in postCodeIpmiHandler\n"); |
| 416 | return ret; |
| 417 | } |
| 418 | return 0; |
| 419 | #endif |
| 420 | |
| 421 | #ifndef ENABLE_IPMI_SNOOP |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 422 | |
| 423 | bool deferSignals = true; |
| 424 | |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 425 | // Add systemd object manager. |
Patrick Williams | aebf87c | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 426 | sdbusplus::server::manager_t snoopdManager(bus, snoopObject); |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 427 | |
| 428 | PostReporter reporter(bus, snoopObject, deferSignals); |
Kun Yi | eb31231 | 2018-06-13 09:20:50 -0700 | [diff] [blame] | 429 | reporter.emit_object_added(); |
| 430 | bus.request_name(snoopDbus); |
Kun Yi | eb31231 | 2018-06-13 09:20:50 -0700 | [diff] [blame] | 431 | |
Kun Yi | 1c16ad8 | 2018-09-12 10:01:49 -0700 | [diff] [blame] | 432 | // Create sdevent and add IO source |
| 433 | try |
| 434 | { |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 435 | sdeventplus::Event event = sdeventplus::Event::get_default(); |
Harvey.Wu | 4f26b3e | 2022-05-12 08:54:30 +0800 | [diff] [blame] | 436 | std::optional<sdeventplus::source::IO> reporterSource; |
Manojkiran Eda | aade4ad | 2021-02-19 11:20:33 +0530 | [diff] [blame] | 437 | if (postFd > 0) |
| 438 | { |
Jonathan Doman | 38b0946 | 2023-04-26 11:45:39 -0700 | [diff] [blame] | 439 | reporter.rateLimit = rateLimit; |
Harvey.Wu | 4f26b3e | 2022-05-12 08:54:30 +0800 | [diff] [blame] | 440 | reporterSource.emplace( |
Jonathan Doman | 38b0946 | 2023-04-26 11:45:39 -0700 | [diff] [blame] | 441 | event, postFd, EPOLLIN, |
| 442 | std::bind_front(PostCodeEventHandler, &reporter)); |
Manojkiran Eda | aade4ad | 2021-02-19 11:20:33 +0530 | [diff] [blame] | 443 | } |
Kun Yi | 1c16ad8 | 2018-09-12 10:01:49 -0700 | [diff] [blame] | 444 | // Enable bus to handle incoming IO and bus events |
William A. Kennington III | 30751ec | 2022-11-21 18:38:36 -0800 | [diff] [blame] | 445 | auto intCb = [](sdeventplus::source::Signal& source, |
| 446 | const struct signalfd_siginfo*) { |
William A. Kennington III | 6a5e0a1 | 2021-12-19 20:47:34 -0800 | [diff] [blame] | 447 | source.get_event().exit(0); |
| 448 | }; |
| 449 | stdplus::signal::block(SIGINT); |
| 450 | sdeventplus::source::Signal(event, SIGINT, intCb).set_floating(true); |
| 451 | stdplus::signal::block(SIGTERM); |
| 452 | sdeventplus::source::Signal(event, SIGTERM, std::move(intCb)) |
| 453 | .set_floating(true); |
William A. Kennington III | 30751ec | 2022-11-21 18:38:36 -0800 | [diff] [blame] | 454 | return sdeventplus::utility::loopWithBus(event, bus); |
Kun Yi | 1c16ad8 | 2018-09-12 10:01:49 -0700 | [diff] [blame] | 455 | } |
| 456 | catch (const std::exception& e) |
| 457 | { |
| 458 | fprintf(stderr, "%s\n", e.what()); |
| 459 | } |
Kun Yi | eb31231 | 2018-06-13 09:20:50 -0700 | [diff] [blame] | 460 | |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 461 | if (postFd > -1) |
| 462 | { |
| 463 | close(postFd); |
| 464 | } |
| 465 | |
Sunita Kumari | 56127ef | 2022-10-12 05:01:20 +0000 | [diff] [blame] | 466 | return 0; |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 467 | #endif |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 468 | } |