xref: /openbmc/estoraged/src/main.cpp (revision 439f0fd8)
1 
2 #include "estoraged.hpp"
3 #include "getConfig.hpp"
4 #include "util.hpp"
5 
6 #include <boost/asio/deadline_timer.hpp>
7 #include <boost/asio/io_context.hpp>
8 #include <boost/asio/post.hpp>
9 #include <boost/container/flat_map.hpp>
10 #include <boost/container/throw_exception.hpp>
11 #include <phosphor-logging/lg2.hpp>
12 #include <sdbusplus/asio/connection.hpp>
13 #include <sdbusplus/asio/object_server.hpp>
14 #include <sdbusplus/bus.hpp>
15 #include <sdbusplus/bus/match.hpp>
16 #include <util.hpp>
17 
18 #include <cstdlib>
19 #include <filesystem>
20 #include <iostream>
21 #include <memory>
22 #include <string>
23 
24 /*
25  * Get the configuration objects from Entity Manager and create new D-Bus
26  * objects for each one. This function can be called multiple times, in case
27  * new configuration objects show up later.
28  *
29  * Note: Currently, eStoraged can only support 1 eMMC device.
30  * Additional changes will be needed to support more than 1 eMMC, or to support
31  * more types of storage devices.
32  */
33 void createStorageObjects(
34     sdbusplus::asio::object_server& objectServer,
35     boost::container::flat_map<
36         std::string, std::unique_ptr<estoraged::EStoraged>>& storageObjects,
37     std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
38 {
39     auto getter = std::make_shared<estoraged::GetStorageConfiguration>(
40         dbusConnection,
41         [&objectServer, &storageObjects](
42             const estoraged::ManagedStorageType& storageConfigurations) {
43         size_t numConfigObj = storageConfigurations.size();
44         if (numConfigObj > 1)
45         {
46             lg2::error("eStoraged can only manage 1 eMMC device; found {NUM}",
47                        "NUM", numConfigObj, "REDFISH_MESSAGE_ID",
48                        std::string("OpenBMC.0.1.CreateStorageObjectsFail"));
49             return;
50         }
51 
52         for (const std::pair<sdbusplus::message::object_path,
53                              estoraged::StorageData>& storage :
54              storageConfigurations)
55         {
56             const std::string& path = storage.first.str;
57 
58             if (storageObjects.find(path) != storageObjects.end())
59             {
60                 /*
61                  * We've already created this object, or at least
62                  * attempted to.
63                  */
64                 continue;
65             }
66 
67             /* Get the properties from the config object. */
68             const estoraged::StorageData& data = storage.second;
69 
70             /* Look for the device file. */
71             const std::filesystem::path blockDevDir{"/sys/block"};
72             std::filesystem::path deviceFile, sysfsDir;
73             std::string luksName;
74             bool found = estoraged::util::findDevice(
75                 data, blockDevDir, deviceFile, sysfsDir, luksName);
76             if (!found)
77             {
78                 lg2::error("Device not found for path {PATH}", "PATH", path,
79                            "REDFISH_MESSAGE_ID",
80                            std::string("OpenBMC.0.1.CreateStorageObjectsFail"));
81                 /*
82                  * Set a NULL pointer as a placeholder, so that we don't
83                  * try and fail again later.
84                  */
85                 storageObjects[path] = nullptr;
86                 continue;
87             }
88 
89             uint64_t size = estoraged::util::findSizeOfBlockDevice(deviceFile);
90 
91             uint8_t lifeleft =
92                 estoraged::util::findPredictedMediaLifeLeftPercent(sysfsDir);
93             std::string partNumber = estoraged::util::getPartNumber(sysfsDir);
94             std::string serialNumber =
95                 estoraged::util::getSerialNumber(sysfsDir);
96             /* Create the storage object. */
97             storageObjects[path] = std::make_unique<estoraged::EStoraged>(
98                 objectServer, path, deviceFile, luksName, size, lifeleft,
99                 partNumber, serialNumber);
100             lg2::info("Created eStoraged object for path {PATH}", "PATH", path,
101                       "REDFISH_MESSAGE_ID",
102                       std::string("OpenBMC.0.1.CreateStorageObjects"));
103         }
104         });
105     getter->getConfiguration();
106 }
107 
108 int main(void)
109 {
110     try
111     {
112         // setup connection to dbus
113         boost::asio::io_context io;
114         auto conn = std::make_shared<sdbusplus::asio::connection>(io);
115         // request D-Bus server name.
116         conn->request_name("xyz.openbmc_project.eStoraged");
117         sdbusplus::asio::object_server server(conn);
118         boost::container::flat_map<std::string,
119                                    std::unique_ptr<estoraged::EStoraged>>
120             storageObjects;
121 
122         boost::asio::post(
123             io, [&]() { createStorageObjects(server, storageObjects, conn); });
124 
125         /*
126          * Set up an event handler to process any new configuration objects
127          * that show up later.
128          */
129         boost::asio::deadline_timer filterTimer(io);
130         std::function<void(sdbusplus::message_t&)> eventHandler =
131             [&](sdbusplus::message_t& message) {
132             if (message.is_method_error())
133             {
134                 lg2::error("eventHandler callback method error");
135                 return;
136             }
137             /*
138              * This implicitly cancels the timer, if it's already pending.
139              * If there's a burst of events within a short period, we want
140              * to handle them all at once. So, we will wait this long for no
141              * more events to occur, before processing them.
142              */
143             filterTimer.expires_from_now(boost::posix_time::seconds(1));
144 
145             filterTimer.async_wait([&](const boost::system::error_code& ec) {
146                 if (ec == boost::asio::error::operation_aborted)
147                 {
148                     /* we were canceled */
149                     return;
150                 }
151                 if (ec)
152                 {
153                     lg2::error("timer error");
154                     return;
155                 }
156                 createStorageObjects(server, storageObjects, conn);
157             });
158         };
159 
160         auto match = std::make_unique<sdbusplus::bus::match_t>(
161             static_cast<sdbusplus::bus_t&>(*conn),
162             "type='signal',member='PropertiesChanged',path_namespace='" +
163                 std::string("/xyz/openbmc_project/inventory") +
164                 "',arg0namespace='" + estoraged::emmcConfigInterface + "'",
165             eventHandler);
166 
167         lg2::info("Storage management service is running", "REDFISH_MESSAGE_ID",
168                   std::string("OpenBMC.1.0.ServiceStarted"));
169 
170         io.run();
171         return 0;
172     }
173     catch (const std::exception& e)
174     {
175         lg2::error(e.what(), "REDFISH_MESSAGE_ID",
176                    std::string("OpenBMC.1.0.ServiceException"));
177 
178         return 2;
179     }
180     return 1;
181 }
182