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 |
Patrick Venture | b5754fd | 2018-09-10 13:13:58 -0700 | [diff] [blame] | 20 | #include "lpcsnoop/snoop.hpp" |
| 21 | |
William A. Kennington III | 6dac4c5 | 2019-12-13 15:05:00 -0800 | [diff] [blame] | 22 | #include <endian.h> |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 23 | #include <fcntl.h> |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 24 | #include <getopt.h> |
Patrick Venture | b5754fd | 2018-09-10 13:13:58 -0700 | [diff] [blame] | 25 | #include <sys/epoll.h> |
| 26 | #include <systemd/sd-event.h> |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 27 | #include <unistd.h> |
| 28 | |
Kun Yi | 1c16ad8 | 2018-09-12 10:01:49 -0700 | [diff] [blame] | 29 | #include <sdeventplus/event.hpp> |
| 30 | #include <sdeventplus/source/event.hpp> |
| 31 | #include <sdeventplus/source/io.hpp> |
William A. Kennington III | 6a5e0a1 | 2021-12-19 20:47:34 -0800 | [diff] [blame] | 32 | #include <sdeventplus/source/signal.hpp> |
Jonathan Doman | 38b0946 | 2023-04-26 11:45:39 -0700 | [diff] [blame] | 33 | #include <sdeventplus/source/time.hpp> |
William A. Kennington III | 30751ec | 2022-11-21 18:38:36 -0800 | [diff] [blame] | 34 | #include <sdeventplus/utility/sdbus.hpp> |
William A. Kennington III | 6a5e0a1 | 2021-12-19 20:47:34 -0800 | [diff] [blame] | 35 | #include <stdplus/signal.hpp> |
Patrick Williams | 0ea7357 | 2023-05-10 07:50:44 -0500 | [diff] [blame] | 36 | |
| 37 | #include <chrono> |
| 38 | #include <cstdint> |
| 39 | #include <exception> |
| 40 | #include <functional> |
| 41 | #include <iostream> |
| 42 | #include <optional> |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 43 | #include <thread> |
| 44 | |
Benjamin Fair | f69ad7e | 2018-07-13 13:41:10 -0700 | [diff] [blame] | 45 | static size_t codeSize = 1; /* Size of each POST code in bytes */ |
Jonathan Doman | 38b0946 | 2023-04-26 11:45:39 -0700 | [diff] [blame] | 46 | static bool verbose = false; |
cchoux | 72d5507 | 2023-12-05 14:05:23 +0800 | [diff] [blame] | 47 | static std::function<bool(uint64_t&, ssize_t)> procPostCode; |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 48 | |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 49 | static void usage(const char* name) |
| 50 | { |
| 51 | fprintf(stderr, |
Jonathan Doman | de7a6dd | 2023-05-03 10:54:08 -0700 | [diff] [blame] | 52 | "Usage: %s\n" |
| 53 | #ifdef ENABLE_IPMI_SNOOP |
| 54 | " -h, --host <host instances> Default is '0'\n" |
| 55 | #else |
Manojkiran Eda | aade4ad | 2021-02-19 11:20:33 +0530 | [diff] [blame] | 56 | " -d, --device <DEVICE> use <DEVICE> file.\n" |
Jonathan Doman | fd2430d | 2023-05-03 11:44:01 -0700 | [diff] [blame] | 57 | " -r, --rate-limit=<N> Only process N POST codes from the " |
| 58 | "device per second.\n" |
Jonathan Doman | de7a6dd | 2023-05-03 10:54:08 -0700 | [diff] [blame] | 59 | " -b, --bytes <SIZE> set POST code length to <SIZE> bytes. " |
| 60 | "Default is 1\n" |
| 61 | #endif |
William A. Kennington III | 66efa63 | 2020-04-16 18:46:20 -0700 | [diff] [blame] | 62 | " -v, --verbose Prints verbose information while running\n\n", |
Jonathan Doman | de7a6dd | 2023-05-03 10:54:08 -0700 | [diff] [blame] | 63 | name); |
Benjamin Fair | f69ad7e | 2018-07-13 13:41:10 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Jonathan Doman | 38b0946 | 2023-04-26 11:45:39 -0700 | [diff] [blame] | 66 | /** |
| 67 | * Call once for each POST code received. If the number of POST codes exceeds |
| 68 | * the configured rate limit, this function will disable the snoop device IO |
| 69 | * source until the end of the 1 second interval, then re-enable it. |
| 70 | * |
| 71 | * @return Whether the rate limit is exceeded. |
| 72 | */ |
| 73 | bool rateLimit(PostReporter& reporter, sdeventplus::source::IO& ioSource) |
| 74 | { |
| 75 | if (reporter.rateLimit == 0) |
| 76 | { |
| 77 | // Rate limiting is disabled. |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | using Clock = sdeventplus::Clock<sdeventplus::ClockId::Monotonic>; |
| 82 | |
| 83 | static constexpr std::chrono::seconds rateLimitInterval(1); |
| 84 | static unsigned int rateLimitCount = 0; |
| 85 | static Clock::time_point rateLimitEndTime; |
| 86 | |
| 87 | const sdeventplus::Event& event = ioSource.get_event(); |
| 88 | |
| 89 | if (rateLimitCount == 0) |
| 90 | { |
| 91 | // Initialize the end time when we start a new interval |
| 92 | rateLimitEndTime = Clock(event).now() + rateLimitInterval; |
| 93 | } |
| 94 | |
| 95 | if (++rateLimitCount < reporter.rateLimit) |
| 96 | { |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | rateLimitCount = 0; |
| 101 | |
| 102 | if (rateLimitEndTime < Clock(event).now()) |
| 103 | { |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | if (verbose) |
| 108 | { |
| 109 | fprintf(stderr, "Hit POST code rate limit - disabling temporarily\n"); |
| 110 | } |
| 111 | |
| 112 | ioSource.set_enabled(sdeventplus::source::Enabled::Off); |
| 113 | sdeventplus::source::Time<sdeventplus::ClockId::Monotonic>( |
| 114 | event, rateLimitEndTime, std::chrono::milliseconds(100), |
| 115 | [&ioSource](auto&, auto) { |
Patrick Williams | d62ef55 | 2024-08-16 15:20:42 -0400 | [diff] [blame] | 116 | if (verbose) |
| 117 | { |
| 118 | fprintf(stderr, "Reenabling POST code handler\n"); |
| 119 | } |
| 120 | ioSource.set_enabled(sdeventplus::source::Enabled::On); |
| 121 | }) |
| 122 | .set_floating(true); |
Jonathan Doman | 38b0946 | 2023-04-26 11:45:39 -0700 | [diff] [blame] | 123 | return true; |
| 124 | } |
| 125 | |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 126 | /* |
cchoux | 72d5507 | 2023-12-05 14:05:23 +0800 | [diff] [blame] | 127 | * Split input code into multiple 2 bytes PCC code, If the PCC code prefix |
| 128 | * matches the check code, store each PCC code in aspeedPCCBuffer, or clear |
| 129 | * aspeedPCCBuffer if the prefix does not match. |
| 130 | * |
| 131 | * Each PCC code contains one byte of port number (MSB) and another byte of |
| 132 | * partial postcode (LSB). To get a complete postcode, the PCC code should |
| 133 | * followed the sequence of 0x40AA, 0x41BB, 0x42CC & 0x43DD. When |
| 134 | * aspeedPCCBuffer contains enough PCC codes, the postcode will be assigned as |
| 135 | * 0xDDCCBBAA. |
| 136 | */ |
| 137 | bool aspeedPCC(uint64_t& code, ssize_t readb) |
| 138 | { |
| 139 | // Size of data coming from the PCC hardware |
| 140 | constexpr size_t pccSize = sizeof(uint16_t); |
| 141 | // Required PCC count of a full postcode, if codeSize is 8 bytes, it means |
| 142 | // it require 4 PCC codes in correct sequence to get a complete postcode. |
| 143 | const size_t fullPostPCCCount = codeSize / pccSize; |
| 144 | // A PCC buffer for storing PCC code in sequence. |
| 145 | static std::vector<uint16_t> aspeedPCCBuffer; |
| 146 | constexpr uint16_t firstPCCPortNumber = 0x4000; |
| 147 | constexpr uint16_t pccPortNumberMask = 0xFF00; |
| 148 | constexpr uint16_t pccPostCodeMask = 0x00FF; |
| 149 | constexpr uint8_t byteShift = 8; |
| 150 | |
| 151 | uint16_t* codePtr = reinterpret_cast<uint16_t*>(&code); |
| 152 | |
| 153 | for (size_t i = 0; i < (readb / pccSize); i++) |
| 154 | { |
Patrick Williams | d62ef55 | 2024-08-16 15:20:42 -0400 | [diff] [blame] | 155 | uint16_t checkCode = |
| 156 | firstPCCPortNumber + |
| 157 | ((aspeedPCCBuffer.size() % fullPostPCCCount) << byteShift); |
cchoux | 72d5507 | 2023-12-05 14:05:23 +0800 | [diff] [blame] | 158 | |
| 159 | if (checkCode == (codePtr[i] & pccPortNumberMask)) |
| 160 | { |
| 161 | aspeedPCCBuffer.emplace_back(codePtr[i]); |
| 162 | } |
| 163 | else |
| 164 | { |
| 165 | aspeedPCCBuffer.clear(); |
| 166 | |
| 167 | // keep the PCC code if codePtr[i] matches with 0x40XX as first PCC |
| 168 | // code in buffer. |
| 169 | if ((codePtr[i] & pccPortNumberMask) == firstPCCPortNumber) |
| 170 | { |
| 171 | aspeedPCCBuffer.emplace_back(codePtr[i]); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | if (aspeedPCCBuffer.size() < fullPostPCCCount) |
| 177 | { |
| 178 | // not receive full postcode yet. |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | // Remove the prefix bytes and combine the partial postcodes together. |
| 183 | code = 0; |
| 184 | for (size_t i = 0; i < fullPostPCCCount; i++) |
| 185 | { |
| 186 | code |= static_cast<uint64_t>(aspeedPCCBuffer[i] & pccPostCodeMask) |
| 187 | << (byteShift * i); |
| 188 | } |
| 189 | aspeedPCCBuffer.erase(aspeedPCCBuffer.begin(), |
| 190 | aspeedPCCBuffer.begin() + fullPostPCCCount); |
| 191 | |
| 192 | return true; |
| 193 | } |
| 194 | |
| 195 | /* |
Kun Yi | eb31231 | 2018-06-13 09:20:50 -0700 | [diff] [blame] | 196 | * Callback handling IO event from the POST code fd. i.e. there is new |
| 197 | * POST code available to read. |
| 198 | */ |
Jonathan Doman | 38b0946 | 2023-04-26 11:45:39 -0700 | [diff] [blame] | 199 | void PostCodeEventHandler(PostReporter* reporter, sdeventplus::source::IO& s, |
| 200 | int postFd, uint32_t) |
Kun Yi | eb31231 | 2018-06-13 09:20:50 -0700 | [diff] [blame] | 201 | { |
William A. Kennington III | 6dac4c5 | 2019-12-13 15:05:00 -0800 | [diff] [blame] | 202 | uint64_t code = 0; |
William A. Kennington III | 0f964b4 | 2020-04-16 18:46:03 -0700 | [diff] [blame] | 203 | ssize_t readb; |
Jonathan Doman | 38b0946 | 2023-04-26 11:45:39 -0700 | [diff] [blame] | 204 | |
William A. Kennington III | 0f964b4 | 2020-04-16 18:46:03 -0700 | [diff] [blame] | 205 | while ((readb = read(postFd, &code, codeSize)) > 0) |
Kun Yi | 1c16ad8 | 2018-09-12 10:01:49 -0700 | [diff] [blame] | 206 | { |
cchoux | 72d5507 | 2023-12-05 14:05:23 +0800 | [diff] [blame] | 207 | if (procPostCode && procPostCode(code, readb) == false) |
| 208 | { |
| 209 | return; |
| 210 | } |
| 211 | |
William A. Kennington III | f21475a | 2019-12-13 17:21:24 -0800 | [diff] [blame] | 212 | code = le64toh(code); |
William A. Kennington III | 66efa63 | 2020-04-16 18:46:20 -0700 | [diff] [blame] | 213 | if (verbose) |
| 214 | { |
| 215 | fprintf(stderr, "Code: 0x%" PRIx64 "\n", code); |
| 216 | } |
William A. Kennington III | f21475a | 2019-12-13 17:21:24 -0800 | [diff] [blame] | 217 | // HACK: Always send property changed signal even for the same code |
| 218 | // since we are single threaded, external users will never see the |
| 219 | // first value. |
Manojkiran Eda | ba5258f | 2021-02-25 13:23:33 +0530 | [diff] [blame] | 220 | reporter->value(std::make_tuple(~code, secondary_post_code_t{}), true); |
| 221 | reporter->value(std::make_tuple(code, secondary_post_code_t{})); |
William A. Kennington III | 0f964b4 | 2020-04-16 18:46:03 -0700 | [diff] [blame] | 222 | |
Manojkiran Eda | dd3928b | 2024-06-17 12:03:29 +0530 | [diff] [blame] | 223 | // read depends on old data being cleared since it doesn't always read |
William A. Kennington III | 0f964b4 | 2020-04-16 18:46:03 -0700 | [diff] [blame] | 224 | // the full code size |
| 225 | code = 0; |
Jonathan Doman | 38b0946 | 2023-04-26 11:45:39 -0700 | [diff] [blame] | 226 | |
| 227 | if (rateLimit(*reporter, s)) |
| 228 | { |
| 229 | return; |
| 230 | } |
Kun Yi | 1c16ad8 | 2018-09-12 10:01:49 -0700 | [diff] [blame] | 231 | } |
William A. Kennington III | 0f964b4 | 2020-04-16 18:46:03 -0700 | [diff] [blame] | 232 | |
| 233 | if (readb < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) |
| 234 | { |
| 235 | return; |
| 236 | } |
| 237 | |
| 238 | /* Read failure. */ |
| 239 | if (readb == 0) |
| 240 | { |
| 241 | fprintf(stderr, "Unexpected EOF reading postcode\n"); |
| 242 | } |
| 243 | else |
| 244 | { |
| 245 | fprintf(stderr, "Failed to read postcode: %s\n", strerror(errno)); |
| 246 | } |
| 247 | s.get_event().exit(1); |
Kun Yi | eb31231 | 2018-06-13 09:20:50 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | /* |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 251 | * TODO(venture): this only listens one of the possible snoop ports, but |
| 252 | * doesn't share the namespace. |
| 253 | * |
| 254 | * This polls() the lpc snoop character device and it owns the dbus object |
| 255 | * whose value is the latest port 80h value. |
| 256 | */ |
| 257 | int main(int argc, char* argv[]) |
| 258 | { |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 259 | int postFd = -1; |
Jonathan Doman | 38b0946 | 2023-04-26 11:45:39 -0700 | [diff] [blame] | 260 | unsigned int rateLimit = 0; |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 261 | |
| 262 | int opt; |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 263 | |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 264 | std::vector<std::string> host; |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 265 | |
Patrick Venture | 1ceb21b | 2018-08-08 11:29:36 -0700 | [diff] [blame] | 266 | // clang-format off |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 267 | static const struct option long_options[] = { |
Jonathan Doman | de7a6dd | 2023-05-03 10:54:08 -0700 | [diff] [blame] | 268 | #ifdef ENABLE_IPMI_SNOOP |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 269 | {"host", optional_argument, NULL, 'h'}, |
Jonathan Doman | de7a6dd | 2023-05-03 10:54:08 -0700 | [diff] [blame] | 270 | #else |
Manojkiran Eda | aade4ad | 2021-02-19 11:20:33 +0530 | [diff] [blame] | 271 | {"device", optional_argument, NULL, 'd'}, |
Jonathan Doman | 38b0946 | 2023-04-26 11:45:39 -0700 | [diff] [blame] | 272 | {"rate-limit", optional_argument, NULL, 'r'}, |
Jonathan Doman | de7a6dd | 2023-05-03 10:54:08 -0700 | [diff] [blame] | 273 | {"bytes", required_argument, NULL, 'b'}, |
| 274 | #endif |
William A. Kennington III | 66efa63 | 2020-04-16 18:46:20 -0700 | [diff] [blame] | 275 | {"verbose", no_argument, NULL, 'v'}, |
Patrick Venture | 1ceb21b | 2018-08-08 11:29:36 -0700 | [diff] [blame] | 276 | {0, 0, 0, 0} |
| 277 | }; |
| 278 | // clang-format on |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 279 | |
Jonathan Doman | de7a6dd | 2023-05-03 10:54:08 -0700 | [diff] [blame] | 280 | constexpr const char* optstring = |
| 281 | #ifdef ENABLE_IPMI_SNOOP |
| 282 | "h:" |
| 283 | #else |
| 284 | "d:r:b:" |
| 285 | #endif |
| 286 | "v"; |
| 287 | |
| 288 | while ((opt = getopt_long(argc, argv, optstring, long_options, NULL)) != -1) |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 289 | { |
| 290 | switch (opt) |
| 291 | { |
| 292 | case 0: |
| 293 | break; |
Patrick Williams | 0ea7357 | 2023-05-10 07:50:44 -0500 | [diff] [blame] | 294 | case 'h': |
| 295 | { |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 296 | std::string_view instances = optarg; |
| 297 | size_t pos = 0; |
| 298 | |
| 299 | while ((pos = instances.find(" ")) != std::string::npos) |
| 300 | { |
| 301 | host.emplace_back(instances.substr(0, pos)); |
| 302 | instances.remove_prefix(pos + 1); |
| 303 | } |
| 304 | host.emplace_back(instances); |
| 305 | break; |
| 306 | } |
Patrick Williams | 0ea7357 | 2023-05-10 07:50:44 -0500 | [diff] [blame] | 307 | case 'b': |
| 308 | { |
Benjamin Fair | f69ad7e | 2018-07-13 13:41:10 -0700 | [diff] [blame] | 309 | codeSize = atoi(optarg); |
| 310 | |
| 311 | if (codeSize < 1 || codeSize > 8) |
| 312 | { |
| 313 | fprintf(stderr, |
| 314 | "Invalid POST code size '%s'. Must be " |
| 315 | "an integer from 1 to 8.\n", |
| 316 | optarg); |
| 317 | exit(EXIT_FAILURE); |
| 318 | } |
| 319 | break; |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 320 | } |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 321 | case 'd': |
Rajaganesh Rathinasabapathi | 95aa444 | 2024-04-08 01:25:20 -0500 | [diff] [blame] | 322 | if (std::string(optarg).starts_with("/dev/aspeed-lpc-pcc")) |
cchoux | 72d5507 | 2023-12-05 14:05:23 +0800 | [diff] [blame] | 323 | { |
| 324 | procPostCode = aspeedPCC; |
| 325 | } |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 326 | |
Manojkiran Eda | aade4ad | 2021-02-19 11:20:33 +0530 | [diff] [blame] | 327 | postFd = open(optarg, O_NONBLOCK); |
| 328 | if (postFd < 0) |
| 329 | { |
| 330 | fprintf(stderr, "Unable to open: %s\n", optarg); |
| 331 | return -1; |
| 332 | } |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 333 | break; |
Patrick Williams | 0ea7357 | 2023-05-10 07:50:44 -0500 | [diff] [blame] | 334 | case 'r': |
| 335 | { |
Jonathan Doman | 38b0946 | 2023-04-26 11:45:39 -0700 | [diff] [blame] | 336 | int argVal = -1; |
| 337 | try |
| 338 | { |
| 339 | argVal = std::stoi(optarg); |
| 340 | } |
| 341 | catch (...) |
Patrick Williams | 0ea7357 | 2023-05-10 07:50:44 -0500 | [diff] [blame] | 342 | {} |
Jonathan Doman | 38b0946 | 2023-04-26 11:45:39 -0700 | [diff] [blame] | 343 | |
| 344 | if (argVal < 1) |
| 345 | { |
| 346 | fprintf(stderr, "Invalid rate limit '%s'. Must be >= 1.\n", |
| 347 | optarg); |
| 348 | return EXIT_FAILURE; |
| 349 | } |
| 350 | |
| 351 | rateLimit = static_cast<unsigned int>(argVal); |
| 352 | fprintf(stderr, "Rate limiting to %d POST codes per second.\n", |
| 353 | argVal); |
| 354 | break; |
| 355 | } |
William A. Kennington III | 66efa63 | 2020-04-16 18:46:20 -0700 | [diff] [blame] | 356 | case 'v': |
| 357 | verbose = true; |
| 358 | break; |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 359 | default: |
| 360 | usage(argv[0]); |
Jonathan Doman | 38b0946 | 2023-04-26 11:45:39 -0700 | [diff] [blame] | 361 | return EXIT_FAILURE; |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 365 | auto bus = sdbusplus::bus::new_default(); |
| 366 | |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 367 | #ifdef ENABLE_IPMI_SNOOP |
| 368 | std::cout << "Verbose = " << verbose << std::endl; |
| 369 | int ret = postCodeIpmiHandler(ipmiSnoopObject, snoopDbus, bus, host); |
| 370 | if (ret < 0) |
| 371 | { |
| 372 | fprintf(stderr, "Error in postCodeIpmiHandler\n"); |
| 373 | return ret; |
| 374 | } |
| 375 | return 0; |
| 376 | #endif |
| 377 | |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 378 | bool deferSignals = true; |
| 379 | |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 380 | // Add systemd object manager. |
Patrick Williams | aebf87c | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 381 | sdbusplus::server::manager_t snoopdManager(bus, snoopObject); |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 382 | |
| 383 | PostReporter reporter(bus, snoopObject, deferSignals); |
Kun Yi | eb31231 | 2018-06-13 09:20:50 -0700 | [diff] [blame] | 384 | reporter.emit_object_added(); |
| 385 | bus.request_name(snoopDbus); |
Kun Yi | eb31231 | 2018-06-13 09:20:50 -0700 | [diff] [blame] | 386 | |
Kun Yi | 1c16ad8 | 2018-09-12 10:01:49 -0700 | [diff] [blame] | 387 | // Create sdevent and add IO source |
| 388 | try |
| 389 | { |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame] | 390 | sdeventplus::Event event = sdeventplus::Event::get_default(); |
Harvey.Wu | 4f26b3e | 2022-05-12 08:54:30 +0800 | [diff] [blame] | 391 | std::optional<sdeventplus::source::IO> reporterSource; |
Manojkiran Eda | aade4ad | 2021-02-19 11:20:33 +0530 | [diff] [blame] | 392 | if (postFd > 0) |
| 393 | { |
Jonathan Doman | 38b0946 | 2023-04-26 11:45:39 -0700 | [diff] [blame] | 394 | reporter.rateLimit = rateLimit; |
Harvey.Wu | 4f26b3e | 2022-05-12 08:54:30 +0800 | [diff] [blame] | 395 | reporterSource.emplace( |
Jonathan Doman | 38b0946 | 2023-04-26 11:45:39 -0700 | [diff] [blame] | 396 | event, postFd, EPOLLIN, |
| 397 | std::bind_front(PostCodeEventHandler, &reporter)); |
Manojkiran Eda | aade4ad | 2021-02-19 11:20:33 +0530 | [diff] [blame] | 398 | } |
Kun Yi | 1c16ad8 | 2018-09-12 10:01:49 -0700 | [diff] [blame] | 399 | // Enable bus to handle incoming IO and bus events |
William A. Kennington III | 30751ec | 2022-11-21 18:38:36 -0800 | [diff] [blame] | 400 | auto intCb = [](sdeventplus::source::Signal& source, |
| 401 | const struct signalfd_siginfo*) { |
William A. Kennington III | 6a5e0a1 | 2021-12-19 20:47:34 -0800 | [diff] [blame] | 402 | source.get_event().exit(0); |
| 403 | }; |
| 404 | stdplus::signal::block(SIGINT); |
| 405 | sdeventplus::source::Signal(event, SIGINT, intCb).set_floating(true); |
| 406 | stdplus::signal::block(SIGTERM); |
| 407 | sdeventplus::source::Signal(event, SIGTERM, std::move(intCb)) |
| 408 | .set_floating(true); |
William A. Kennington III | 30751ec | 2022-11-21 18:38:36 -0800 | [diff] [blame] | 409 | return sdeventplus::utility::loopWithBus(event, bus); |
Kun Yi | 1c16ad8 | 2018-09-12 10:01:49 -0700 | [diff] [blame] | 410 | } |
| 411 | catch (const std::exception& e) |
| 412 | { |
| 413 | fprintf(stderr, "%s\n", e.what()); |
| 414 | } |
Kun Yi | eb31231 | 2018-06-13 09:20:50 -0700 | [diff] [blame] | 415 | |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 416 | if (postFd > -1) |
| 417 | { |
| 418 | close(postFd); |
| 419 | } |
| 420 | |
Sunita Kumari | 56127ef | 2022-10-12 05:01:20 +0000 | [diff] [blame] | 421 | return 0; |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 422 | } |