blob: ee25960c6ebd040ca0d8d1d861340058d526074a [file] [log] [blame]
Vernon Mauery9e801a22018-10-12 13:20:49 -07001#include "sd_event_loop.hpp"
2
3#include "main.hpp"
4#include "message_handler.hpp"
5
Dave Cobbley2c15f0c2017-11-13 16:19:09 -08006#include <netinet/in.h>
Tom Joseph7fd26dd2017-03-14 15:26:26 +05307#include <sys/ioctl.h>
Dave Cobbley2c15f0c2017-11-13 16:19:09 -08008#include <sys/socket.h>
Tom Joseph7fd26dd2017-03-14 15:26:26 +05309#include <systemd/sd-daemon.h>
Vernon Mauery9e801a22018-10-12 13:20:49 -070010
Vernon Mauerycbccb052018-10-24 13:52:22 -070011#include <boost/asio/io_context.hpp>
Tom Joseph7fd26dd2017-03-14 15:26:26 +053012#include <phosphor-logging/log.hpp>
Vernon Mauerycbccb052018-10-24 13:52:22 -070013#include <sdbusplus/asio/sd_event.hpp>
Tom Joseph7fd26dd2017-03-14 15:26:26 +053014
15namespace eventloop
16{
17using namespace phosphor::logging;
18
Vernon Mauery7a0142c2018-11-09 08:38:16 -080019void EventLoop::handleRmcpPacket()
Tom Joseph7fd26dd2017-03-14 15:26:26 +053020{
Tom Joseph7fd26dd2017-03-14 15:26:26 +053021 try
22 {
Vernon Mauery7a0142c2018-11-09 08:38:16 -080023 auto channelPtr = std::make_shared<udpsocket::Channel>(udpSocket);
Tom Joseph7fd26dd2017-03-14 15:26:26 +053024
25 // Initialize the Message Handler with the socket channel
Vernon Mauery8d6f2002018-11-07 09:55:53 -080026 auto msgHandler = std::make_shared<message::Handler>(channelPtr, io);
Tom Joseph7fd26dd2017-03-14 15:26:26 +053027
Vernon Mauery8d6f2002018-11-07 09:55:53 -080028 msgHandler->processIncoming();
Tom Joseph7fd26dd2017-03-14 15:26:26 +053029 }
Vernon Mauery7a0142c2018-11-09 08:38:16 -080030 catch (const std::exception& e)
Tom Joseph7fd26dd2017-03-14 15:26:26 +053031 {
Vernon Mauery7a0142c2018-11-09 08:38:16 -080032 log<level::ERR>("Executing the IPMI message failed",
33 entry("EXCEPTION=%s", e.what()));
Tom Joseph7fd26dd2017-03-14 15:26:26 +053034 }
Vernon Mauery7a0142c2018-11-09 08:38:16 -080035}
Tom Joseph7fd26dd2017-03-14 15:26:26 +053036
Vernon Mauery7a0142c2018-11-09 08:38:16 -080037void 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 Joseph7fd26dd2017-03-14 15:26:26 +053047}
48
Vernon Mauerycbccb052018-10-24 13:52:22 -070049int EventLoop::startEventLoop()
Tom Joseph7fd26dd2017-03-14 15:26:26 +053050{
Vernon Mauery22c8a212018-10-24 14:51:23 -070051 // set up boost::asio signal handling
52 boost::asio::signal_set signals(*io, SIGINT, SIGTERM);
Vernon Mauery7a0142c2018-11-09 08:38:16 -080053 signals.async_wait(
54 [this](const boost::system::error_code& error, int signalNumber) {
55 udpSocket->cancel();
56 udpSocket->close();
57 io->stop();
58 });
Tom Joseph7fd26dd2017-03-14 15:26:26 +053059
Vernon Mauery9e801a22018-10-12 13:20:49 -070060 // Create our own socket if SysD did not supply one.
Vernon Mauery7a0142c2018-11-09 08:38:16 -080061 int listensFdCount = sd_listen_fds(0);
62 if (listensFdCount == 1)
Tom Joseph7fd26dd2017-03-14 15:26:26 +053063 {
Vernon Mauery7a0142c2018-11-09 08:38:16 -080064 if (sd_is_socket(SD_LISTEN_FDS_START, AF_UNSPEC, SOCK_DGRAM, -1))
65 {
66 udpSocket = std::make_shared<boost::asio::ip::udp::socket>(
67 *io, boost::asio::ip::udp::v6(), SD_LISTEN_FDS_START);
68 }
Dave Cobbley2c15f0c2017-11-13 16:19:09 -080069 }
Vernon Mauery7a0142c2018-11-09 08:38:16 -080070 else if (listensFdCount > 1)
Dave Cobbley2c15f0c2017-11-13 16:19:09 -080071 {
72 log<level::ERR>("Too many file descriptors received");
Vernon Mauerycbccb052018-10-24 13:52:22 -070073 return EXIT_FAILURE;
Tom Joseph7fd26dd2017-03-14 15:26:26 +053074 }
Vernon Mauery7a0142c2018-11-09 08:38:16 -080075 if (!udpSocket)
76 {
77 udpSocket = std::make_shared<boost::asio::ip::udp::socket>(
78 *io, boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v6(),
79 IPMI_STD_PORT));
80 if (!udpSocket)
81 {
82 log<level::ERR>("Failed to start listening on RMCP socket");
83 return EXIT_FAILURE;
84 }
85 }
86 startRmcpReceive();
Tom Joseph7fd26dd2017-03-14 15:26:26 +053087
Vernon Mauerycbccb052018-10-24 13:52:22 -070088 io->run();
Tom Joseph7fd26dd2017-03-14 15:26:26 +053089
Vernon Mauerycbccb052018-10-24 13:52:22 -070090 return EXIT_SUCCESS;
Tom Joseph7fd26dd2017-03-14 15:26:26 +053091}
92
Tom Joseph7fd26dd2017-03-14 15:26:26 +053093} // namespace eventloop