Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 1 | #include "sd_event_loop.hpp" |
| 2 | |
| 3 | #include "main.hpp" |
| 4 | #include "message_handler.hpp" |
| 5 | |
Dave Cobbley | 2c15f0c | 2017-11-13 16:19:09 -0800 | [diff] [blame] | 6 | #include <netinet/in.h> |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 7 | #include <sys/ioctl.h> |
Dave Cobbley | 2c15f0c | 2017-11-13 16:19:09 -0800 | [diff] [blame] | 8 | #include <sys/socket.h> |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 9 | #include <systemd/sd-daemon.h> |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 10 | |
Vernon Mauery | cbccb05 | 2018-10-24 13:52:22 -0700 | [diff] [blame] | 11 | #include <boost/asio/io_context.hpp> |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 12 | #include <phosphor-logging/log.hpp> |
Vernon Mauery | cbccb05 | 2018-10-24 13:52:22 -0700 | [diff] [blame] | 13 | #include <sdbusplus/asio/sd_event.hpp> |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 14 | |
| 15 | namespace eventloop |
| 16 | { |
| 17 | using namespace phosphor::logging; |
| 18 | |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame] | 19 | void EventLoop::handleRmcpPacket() |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 20 | { |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 21 | try |
| 22 | { |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame] | 23 | auto channelPtr = std::make_shared<udpsocket::Channel>(udpSocket); |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 24 | |
| 25 | // Initialize the Message Handler with the socket channel |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame^] | 26 | auto msgHandler = std::make_shared<message::Handler>(channelPtr, io); |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 27 | |
Vernon Mauery | 8d6f200 | 2018-11-07 09:55:53 -0800 | [diff] [blame^] | 28 | msgHandler->processIncoming(); |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 29 | } |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame] | 30 | catch (const std::exception& e) |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 31 | { |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame] | 32 | log<level::ERR>("Executing the IPMI message failed", |
| 33 | entry("EXCEPTION=%s", e.what())); |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 34 | } |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame] | 35 | } |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 36 | |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame] | 37 | void EventLoop::startRmcpReceive() |
| 38 | { |
| 39 | udpSocket->async_wait(boost::asio::socket_base::wait_read, |
| 40 | [this](const boost::system::error_code& ec) { |
| 41 | if (!ec) |
| 42 | { |
| 43 | io->post([this]() { startRmcpReceive(); }); |
| 44 | handleRmcpPacket(); |
| 45 | } |
| 46 | }); |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 47 | } |
| 48 | |
Vernon Mauery | cbccb05 | 2018-10-24 13:52:22 -0700 | [diff] [blame] | 49 | int EventLoop::startEventLoop() |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 50 | { |
Vernon Mauery | cbccb05 | 2018-10-24 13:52:22 -0700 | [diff] [blame] | 51 | sdbusplus::asio::sd_event_wrapper sdEvents(*io); |
| 52 | event = sdEvents.get(); |
Marri Devender Rao | 3ecf0a1 | 2017-07-13 08:07:22 -0500 | [diff] [blame] | 53 | |
Vernon Mauery | 22c8a21 | 2018-10-24 14:51:23 -0700 | [diff] [blame] | 54 | // set up boost::asio signal handling |
| 55 | boost::asio::signal_set signals(*io, SIGINT, SIGTERM); |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame] | 56 | signals.async_wait( |
| 57 | [this](const boost::system::error_code& error, int signalNumber) { |
| 58 | udpSocket->cancel(); |
| 59 | udpSocket->close(); |
| 60 | io->stop(); |
| 61 | }); |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 62 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 63 | // Create our own socket if SysD did not supply one. |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame] | 64 | int listensFdCount = sd_listen_fds(0); |
| 65 | if (listensFdCount == 1) |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 66 | { |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame] | 67 | if (sd_is_socket(SD_LISTEN_FDS_START, AF_UNSPEC, SOCK_DGRAM, -1)) |
| 68 | { |
| 69 | udpSocket = std::make_shared<boost::asio::ip::udp::socket>( |
| 70 | *io, boost::asio::ip::udp::v6(), SD_LISTEN_FDS_START); |
| 71 | } |
Dave Cobbley | 2c15f0c | 2017-11-13 16:19:09 -0800 | [diff] [blame] | 72 | } |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame] | 73 | else if (listensFdCount > 1) |
Dave Cobbley | 2c15f0c | 2017-11-13 16:19:09 -0800 | [diff] [blame] | 74 | { |
| 75 | log<level::ERR>("Too many file descriptors received"); |
Vernon Mauery | cbccb05 | 2018-10-24 13:52:22 -0700 | [diff] [blame] | 76 | return EXIT_FAILURE; |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 77 | } |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame] | 78 | if (!udpSocket) |
| 79 | { |
| 80 | udpSocket = std::make_shared<boost::asio::ip::udp::socket>( |
| 81 | *io, boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v6(), |
| 82 | IPMI_STD_PORT)); |
| 83 | if (!udpSocket) |
| 84 | { |
| 85 | log<level::ERR>("Failed to start listening on RMCP socket"); |
| 86 | return EXIT_FAILURE; |
| 87 | } |
| 88 | } |
| 89 | startRmcpReceive(); |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 90 | |
Vernon Mauery | cbccb05 | 2018-10-24 13:52:22 -0700 | [diff] [blame] | 91 | io->run(); |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 92 | |
Vernon Mauery | cbccb05 | 2018-10-24 13:52:22 -0700 | [diff] [blame] | 93 | return EXIT_SUCCESS; |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 94 | } |
| 95 | |
Tom Joseph | 7fd26dd | 2017-03-14 15:26:26 +0530 | [diff] [blame] | 96 | } // namespace eventloop |