Andrew Geissler | a80a3af | 2019-02-04 14:01:49 -0600 | [diff] [blame] | 1 | #include "associations.hpp" |
Willy Tu | aba14d3 | 2023-01-31 14:19:59 -0800 | [diff] [blame^] | 2 | #include "handler.hpp" |
Andrew Geissler | 3b025e6 | 2019-02-01 10:33:54 -0600 | [diff] [blame] | 3 | #include "processing.hpp" |
Matt Spinler | 35396c1 | 2019-04-05 11:46:57 -0500 | [diff] [blame] | 4 | #include "types.hpp" |
Matt Spinler | dd94586 | 2018-09-07 12:41:05 -0500 | [diff] [blame] | 5 | |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 6 | #include <tinyxml2.h> |
| 7 | |
Ed Tanous | 21c6059 | 2020-08-17 23:43:46 -0700 | [diff] [blame] | 8 | #include <boost/asio/io_context.hpp> |
| 9 | #include <boost/asio/signal_set.hpp> |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 10 | #include <boost/container/flat_map.hpp> |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 11 | #include <sdbusplus/asio/connection.hpp> |
| 12 | #include <sdbusplus/asio/object_server.hpp> |
Konstantin Aladyshev | b15df6b | 2022-01-11 14:50:55 +0300 | [diff] [blame] | 13 | #include <xyz/openbmc_project/Common/error.hpp> |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 14 | |
Brad Bishop | 2352088 | 2022-05-26 21:39:53 -0400 | [diff] [blame] | 15 | #include <atomic> |
| 16 | #include <chrono> |
Brad Bishop | ea11946 | 2022-05-31 20:32:04 -0400 | [diff] [blame] | 17 | #include <exception> |
Brad Bishop | 2352088 | 2022-05-26 21:39:53 -0400 | [diff] [blame] | 18 | #include <iomanip> |
| 19 | #include <iostream> |
Brandon Kim | f6ebfc7 | 2022-07-07 12:53:44 -0700 | [diff] [blame] | 20 | #include <string> |
| 21 | #include <string_view> |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame] | 22 | #include <utility> |
Brad Bishop | 2352088 | 2022-05-26 21:39:53 -0400 | [diff] [blame] | 23 | |
Matt Spinler | e2359fb | 2019-04-05 14:11:33 -0500 | [diff] [blame] | 24 | AssociationMaps associationMaps; |
Matt Spinler | 937a232 | 2019-01-23 13:54:22 -0600 | [diff] [blame] | 25 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 26 | void updateOwners(sdbusplus::asio::connection* conn, |
| 27 | boost::container::flat_map<std::string, std::string>& owners, |
| 28 | const std::string& newObject) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 29 | { |
Brad Bishop | 86d2880 | 2022-07-11 15:49:31 -0400 | [diff] [blame] | 30 | if (newObject.starts_with(":")) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 31 | { |
| 32 | return; |
| 33 | } |
| 34 | conn->async_method_call( |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 35 | [&, newObject](const boost::system::error_code ec, |
| 36 | const std::string& nameOwner) { |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 37 | if (ec) |
| 38 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 39 | std::cerr << "Error getting owner of " << newObject << " : " |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 40 | << ec << "\n"; |
| 41 | return; |
| 42 | } |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 43 | owners[nameOwner] = newObject; |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 44 | }, |
| 45 | "org.freedesktop.DBus", "/", "org.freedesktop.DBus", "GetNameOwner", |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 46 | newObject); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 49 | void sendIntrospectionCompleteSignal(sdbusplus::asio::connection* systemBus, |
| 50 | const std::string& processName) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 51 | { |
| 52 | // TODO(ed) This signal doesn't get exposed properly in the |
| 53 | // introspect right now. Find out how to register signals in |
| 54 | // sdbusplus |
Patrick Williams | cc8070b | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 55 | sdbusplus::message_t m = systemBus->new_signal( |
Brad Bishop | a02cd54 | 2021-10-12 19:12:42 -0400 | [diff] [blame] | 56 | "/xyz/openbmc_project/object_mapper", |
| 57 | "xyz.openbmc_project.ObjectMapper.Private", "IntrospectionComplete"); |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 58 | m.append(processName); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 59 | m.signal_send(); |
| 60 | } |
| 61 | |
| 62 | struct InProgressIntrospect |
| 63 | { |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame] | 64 | InProgressIntrospect() = delete; |
| 65 | InProgressIntrospect(const InProgressIntrospect&) = delete; |
| 66 | InProgressIntrospect(InProgressIntrospect&&) = delete; |
| 67 | InProgressIntrospect& operator=(const InProgressIntrospect&) = delete; |
| 68 | InProgressIntrospect& operator=(InProgressIntrospect&&) = delete; |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 69 | InProgressIntrospect( |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 70 | sdbusplus::asio::connection* systemBus, boost::asio::io_context& io, |
| 71 | const std::string& processName, AssociationMaps& am |
Brad Bishop | d6aa552 | 2022-05-31 19:23:48 -0400 | [diff] [blame] | 72 | #ifdef MAPPER_ENABLE_DEBUG |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 73 | , |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 74 | std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>> |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 75 | globalStartTime |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 76 | #endif |
| 77 | ) : |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 78 | systemBus(systemBus), |
| 79 | io(io), processName(processName), assocMaps(am) |
Brad Bishop | d6aa552 | 2022-05-31 19:23:48 -0400 | [diff] [blame] | 80 | #ifdef MAPPER_ENABLE_DEBUG |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 81 | , |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame] | 82 | globalStartTime(std::move(globalStartTime)), |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 83 | processStartTime(std::chrono::steady_clock::now()) |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 84 | #endif |
Brad Bishop | 2352088 | 2022-05-26 21:39:53 -0400 | [diff] [blame] | 85 | {} |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 86 | ~InProgressIntrospect() |
| 87 | { |
Brad Bishop | ea11946 | 2022-05-31 20:32:04 -0400 | [diff] [blame] | 88 | try |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 89 | { |
Brad Bishop | ea11946 | 2022-05-31 20:32:04 -0400 | [diff] [blame] | 90 | sendIntrospectionCompleteSignal(systemBus, processName); |
Brad Bishop | d6aa552 | 2022-05-31 19:23:48 -0400 | [diff] [blame] | 91 | #ifdef MAPPER_ENABLE_DEBUG |
Brad Bishop | ea11946 | 2022-05-31 20:32:04 -0400 | [diff] [blame] | 92 | std::chrono::duration<float> diff = |
| 93 | std::chrono::steady_clock::now() - processStartTime; |
| 94 | std::cout << std::setw(50) << processName << " scan took " |
| 95 | << diff.count() << " seconds\n"; |
| 96 | |
| 97 | // If we're the last outstanding caller globally, calculate the |
| 98 | // time it took |
| 99 | if (globalStartTime != nullptr && globalStartTime.use_count() == 1) |
| 100 | { |
| 101 | diff = std::chrono::steady_clock::now() - *globalStartTime; |
| 102 | std::cout << "Total scan took " << diff.count() |
| 103 | << " seconds to complete\n"; |
| 104 | } |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 105 | #endif |
Brad Bishop | ea11946 | 2022-05-31 20:32:04 -0400 | [diff] [blame] | 106 | } |
| 107 | catch (const std::exception& e) |
| 108 | { |
| 109 | std::cerr |
| 110 | << "Terminating, unhandled exception while introspecting: " |
| 111 | << e.what() << "\n"; |
| 112 | std::terminate(); |
| 113 | } |
| 114 | catch (...) |
| 115 | { |
| 116 | std::cerr |
| 117 | << "Terminating, unhandled exception while introspecting\n"; |
| 118 | std::terminate(); |
| 119 | } |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 120 | } |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 121 | sdbusplus::asio::connection* systemBus; |
Ed Tanous | 21c6059 | 2020-08-17 23:43:46 -0700 | [diff] [blame] | 122 | boost::asio::io_context& io; |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 123 | std::string processName; |
Matt Spinler | 11401e2 | 2019-04-08 13:13:25 -0500 | [diff] [blame] | 124 | AssociationMaps& assocMaps; |
Brad Bishop | d6aa552 | 2022-05-31 19:23:48 -0400 | [diff] [blame] | 125 | #ifdef MAPPER_ENABLE_DEBUG |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 126 | std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>> |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 127 | globalStartTime; |
| 128 | std::chrono::time_point<std::chrono::steady_clock> processStartTime; |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 129 | #endif |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 130 | }; |
| 131 | |
Kallas, Pawel | 5b4357d | 2022-10-12 15:36:37 +0200 | [diff] [blame] | 132 | void doAssociations(boost::asio::io_context& io, |
| 133 | sdbusplus::asio::connection* systemBus, |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 134 | InterfaceMapType& interfaceMap, |
| 135 | sdbusplus::asio::object_server& objectServer, |
| 136 | const std::string& processName, const std::string& path, |
| 137 | int timeoutRetries = 0) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 138 | { |
Adrian Ambrożewicz | bb40bd3 | 2021-02-12 13:36:26 +0100 | [diff] [blame] | 139 | constexpr int maxTimeoutRetries = 3; |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 140 | systemBus->async_method_call( |
Kallas, Pawel | 5b4357d | 2022-10-12 15:36:37 +0200 | [diff] [blame] | 141 | [&io, &objectServer, path, processName, &interfaceMap, systemBus, |
Adrian Ambrożewicz | bb40bd3 | 2021-02-12 13:36:26 +0100 | [diff] [blame] | 142 | timeoutRetries]( |
Matt Spinler | 937a232 | 2019-01-23 13:54:22 -0600 | [diff] [blame] | 143 | const boost::system::error_code ec, |
Patrick Williams | 2bb2d6b | 2020-05-13 17:59:02 -0500 | [diff] [blame] | 144 | const std::variant<std::vector<Association>>& variantAssociations) { |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 145 | if (ec) |
| 146 | { |
Adrian Ambrożewicz | bb40bd3 | 2021-02-12 13:36:26 +0100 | [diff] [blame] | 147 | if (ec.value() == boost::system::errc::timed_out && |
| 148 | timeoutRetries < maxTimeoutRetries) |
| 149 | { |
Kallas, Pawel | 5b4357d | 2022-10-12 15:36:37 +0200 | [diff] [blame] | 150 | doAssociations(io, systemBus, interfaceMap, objectServer, |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 151 | processName, path, timeoutRetries + 1); |
Adrian Ambrożewicz | bb40bd3 | 2021-02-12 13:36:26 +0100 | [diff] [blame] | 152 | return; |
| 153 | } |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 154 | std::cerr << "Error getting associations from " << path << "\n"; |
| 155 | } |
| 156 | std::vector<Association> associations = |
Patrick Williams | b05bc12 | 2020-05-13 12:21:00 -0500 | [diff] [blame] | 157 | std::get<std::vector<Association>>(variantAssociations); |
Kallas, Pawel | 5b4357d | 2022-10-12 15:36:37 +0200 | [diff] [blame] | 158 | associationChanged(io, objectServer, associations, path, |
| 159 | processName, interfaceMap, associationMaps); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 160 | }, |
| 161 | processName, path, "org.freedesktop.DBus.Properties", "Get", |
John Wang | d0cf942 | 2019-09-17 16:01:34 +0800 | [diff] [blame] | 162 | assocDefsInterface, assocDefsProperty); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Kallas, Pawel | 5b4357d | 2022-10-12 15:36:37 +0200 | [diff] [blame] | 165 | void doIntrospect(boost::asio::io_context& io, |
| 166 | sdbusplus::asio::connection* systemBus, |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame] | 167 | const std::shared_ptr<InProgressIntrospect>& transaction, |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 168 | InterfaceMapType& interfaceMap, |
| 169 | sdbusplus::asio::object_server& objectServer, |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame] | 170 | const std::string& path, int timeoutRetries = 0) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 171 | { |
Vernon Mauery | c52be0d | 2020-01-14 11:14:25 -0800 | [diff] [blame] | 172 | constexpr int maxTimeoutRetries = 3; |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 173 | systemBus->async_method_call( |
Kallas, Pawel | 5b4357d | 2022-10-12 15:36:37 +0200 | [diff] [blame] | 174 | [&io, &interfaceMap, &objectServer, transaction, path, systemBus, |
Vernon Mauery | c52be0d | 2020-01-14 11:14:25 -0800 | [diff] [blame] | 175 | timeoutRetries](const boost::system::error_code ec, |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 176 | const std::string& introspectXml) { |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 177 | if (ec) |
| 178 | { |
Vernon Mauery | c52be0d | 2020-01-14 11:14:25 -0800 | [diff] [blame] | 179 | if (ec.value() == boost::system::errc::timed_out && |
| 180 | timeoutRetries < maxTimeoutRetries) |
| 181 | { |
Kallas, Pawel | 5b4357d | 2022-10-12 15:36:37 +0200 | [diff] [blame] | 182 | doIntrospect(io, systemBus, transaction, interfaceMap, |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 183 | objectServer, path, timeoutRetries + 1); |
Vernon Mauery | c52be0d | 2020-01-14 11:14:25 -0800 | [diff] [blame] | 184 | return; |
| 185 | } |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 186 | std::cerr << "Introspect call failed with error: " << ec << ", " |
| 187 | << ec.message() |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 188 | << " on process: " << transaction->processName |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 189 | << " path: " << path << "\n"; |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | tinyxml2::XMLDocument doc; |
| 194 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 195 | tinyxml2::XMLError e = doc.Parse(introspectXml.c_str()); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 196 | if (e != tinyxml2::XMLError::XML_SUCCESS) |
| 197 | { |
| 198 | std::cerr << "XML parsing failed\n"; |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node"); |
| 203 | if (pRoot == nullptr) |
| 204 | { |
| 205 | std::cerr << "XML document did not contain any data\n"; |
| 206 | return; |
| 207 | } |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 208 | auto& thisPathMap = interfaceMap[path]; |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 209 | tinyxml2::XMLElement* pElement = |
| 210 | pRoot->FirstChildElement("interface"); |
| 211 | while (pElement != nullptr) |
| 212 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 213 | const char* ifaceName = pElement->Attribute("name"); |
| 214 | if (ifaceName == nullptr) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 215 | { |
| 216 | continue; |
| 217 | } |
| 218 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 219 | thisPathMap[transaction->processName].emplace(ifaceName); |
Ed Tanous | d4dd96a | 2018-11-12 11:37:44 -0800 | [diff] [blame] | 220 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 221 | if (std::strcmp(ifaceName, assocDefsInterface) == 0) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 222 | { |
Kallas, Pawel | 5b4357d | 2022-10-12 15:36:37 +0200 | [diff] [blame] | 223 | doAssociations(io, systemBus, interfaceMap, objectServer, |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 224 | transaction->processName, path); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 225 | } |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 226 | |
| 227 | pElement = pElement->NextSiblingElement("interface"); |
| 228 | } |
| 229 | |
Matt Spinler | 11401e2 | 2019-04-08 13:13:25 -0500 | [diff] [blame] | 230 | // Check if this new path has a pending association that can |
| 231 | // now be completed. |
Kallas, Pawel | 5b4357d | 2022-10-12 15:36:37 +0200 | [diff] [blame] | 232 | checkIfPendingAssociation(io, path, interfaceMap, |
Matt Spinler | 11401e2 | 2019-04-08 13:13:25 -0500 | [diff] [blame] | 233 | transaction->assocMaps, objectServer); |
| 234 | |
Ed Tanous | 50232cd | 2018-11-12 11:34:43 -0800 | [diff] [blame] | 235 | pElement = pRoot->FirstChildElement("node"); |
| 236 | while (pElement != nullptr) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 237 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 238 | const char* childPath = pElement->Attribute("name"); |
| 239 | if (childPath != nullptr) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 240 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 241 | std::string parentPath(path); |
| 242 | if (parentPath == "/") |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 243 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 244 | parentPath.clear(); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 245 | } |
Ed Tanous | 50232cd | 2018-11-12 11:34:43 -0800 | [diff] [blame] | 246 | |
Kallas, Pawel | 5b4357d | 2022-10-12 15:36:37 +0200 | [diff] [blame] | 247 | doIntrospect(io, systemBus, transaction, interfaceMap, |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 248 | objectServer, parentPath + "/" + childPath); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 249 | } |
Ed Tanous | 50232cd | 2018-11-12 11:34:43 -0800 | [diff] [blame] | 250 | pElement = pElement->NextSiblingElement("node"); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 251 | } |
| 252 | }, |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 253 | transaction->processName, path, "org.freedesktop.DBus.Introspectable", |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 254 | "Introspect"); |
| 255 | } |
| 256 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 257 | void startNewIntrospect( |
| 258 | sdbusplus::asio::connection* systemBus, boost::asio::io_context& io, |
| 259 | InterfaceMapType& interfaceMap, const std::string& processName, |
Matt Spinler | 11401e2 | 2019-04-08 13:13:25 -0500 | [diff] [blame] | 260 | AssociationMaps& assocMaps, |
Brad Bishop | d6aa552 | 2022-05-31 19:23:48 -0400 | [diff] [blame] | 261 | #ifdef MAPPER_ENABLE_DEBUG |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 262 | std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>> |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 263 | globalStartTime, |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 264 | #endif |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 265 | sdbusplus::asio::object_server& objectServer) |
| 266 | { |
Brad Bishop | 1e94e60 | 2022-06-02 19:47:53 -0400 | [diff] [blame] | 267 | if (needToIntrospect(processName)) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 268 | { |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 269 | std::shared_ptr<InProgressIntrospect> transaction = |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 270 | std::make_shared<InProgressIntrospect>(systemBus, io, processName, |
Matt Spinler | 11401e2 | 2019-04-08 13:13:25 -0500 | [diff] [blame] | 271 | assocMaps |
Brad Bishop | d6aa552 | 2022-05-31 19:23:48 -0400 | [diff] [blame] | 272 | #ifdef MAPPER_ENABLE_DEBUG |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 273 | , |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 274 | globalStartTime |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 275 | #endif |
| 276 | ); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 277 | |
Kallas, Pawel | 5b4357d | 2022-10-12 15:36:37 +0200 | [diff] [blame] | 278 | doIntrospect(io, systemBus, transaction, interfaceMap, objectServer, |
| 279 | "/"); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 283 | void doListNames( |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 284 | boost::asio::io_context& io, InterfaceMapType& interfaceMap, |
| 285 | sdbusplus::asio::connection* systemBus, |
| 286 | boost::container::flat_map<std::string, std::string>& nameOwners, |
Matt Spinler | 11401e2 | 2019-04-08 13:13:25 -0500 | [diff] [blame] | 287 | AssociationMaps& assocMaps, sdbusplus::asio::object_server& objectServer) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 288 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 289 | systemBus->async_method_call( |
| 290 | [&io, &interfaceMap, &nameOwners, &objectServer, systemBus, |
Matt Spinler | 11401e2 | 2019-04-08 13:13:25 -0500 | [diff] [blame] | 291 | &assocMaps](const boost::system::error_code ec, |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 292 | std::vector<std::string> processNames) { |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 293 | if (ec) |
| 294 | { |
| 295 | std::cerr << "Error getting names: " << ec << "\n"; |
| 296 | std::exit(EXIT_FAILURE); |
| 297 | return; |
| 298 | } |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 299 | // Try to make startup consistent |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 300 | std::sort(processNames.begin(), processNames.end()); |
Brad Bishop | d6aa552 | 2022-05-31 19:23:48 -0400 | [diff] [blame] | 301 | #ifdef MAPPER_ENABLE_DEBUG |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 302 | std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>> |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 303 | globalStartTime = std::make_shared< |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 304 | std::chrono::time_point<std::chrono::steady_clock>>( |
| 305 | std::chrono::steady_clock::now()); |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 306 | #endif |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 307 | for (const std::string& processName : processNames) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 308 | { |
Brad Bishop | 1e94e60 | 2022-06-02 19:47:53 -0400 | [diff] [blame] | 309 | if (needToIntrospect(processName)) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 310 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 311 | startNewIntrospect(systemBus, io, interfaceMap, processName, |
| 312 | assocMaps, |
Brad Bishop | d6aa552 | 2022-05-31 19:23:48 -0400 | [diff] [blame] | 313 | #ifdef MAPPER_ENABLE_DEBUG |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 314 | globalStartTime, |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 315 | #endif |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 316 | objectServer); |
| 317 | updateOwners(systemBus, nameOwners, processName); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | }, |
| 321 | "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", |
| 322 | "ListNames"); |
| 323 | } |
| 324 | |
Matt Spinler | a82779f | 2019-01-09 12:39:42 -0600 | [diff] [blame] | 325 | // Remove parents of the passed in path that: |
| 326 | // 1) Only have the 3 default interfaces on them |
| 327 | // - Means D-Bus created these, not application code, |
| 328 | // with the Properties, Introspectable, and Peer ifaces |
| 329 | // 2) Have no other child for this owner |
| 330 | void removeUnneededParents(const std::string& objectPath, |
| 331 | const std::string& owner, |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 332 | InterfaceMapType& interfaceMap) |
Matt Spinler | a82779f | 2019-01-09 12:39:42 -0600 | [diff] [blame] | 333 | { |
| 334 | auto parent = objectPath; |
| 335 | |
| 336 | while (true) |
| 337 | { |
| 338 | auto pos = parent.find_last_of('/'); |
| 339 | if ((pos == std::string::npos) || (pos == 0)) |
| 340 | { |
| 341 | break; |
| 342 | } |
| 343 | parent = parent.substr(0, pos); |
| 344 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 345 | auto parentIt = interfaceMap.find(parent); |
| 346 | if (parentIt == interfaceMap.end()) |
Matt Spinler | a82779f | 2019-01-09 12:39:42 -0600 | [diff] [blame] | 347 | { |
| 348 | break; |
| 349 | } |
| 350 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 351 | auto ifacesIt = parentIt->second.find(owner); |
| 352 | if (ifacesIt == parentIt->second.end()) |
Matt Spinler | a82779f | 2019-01-09 12:39:42 -0600 | [diff] [blame] | 353 | { |
| 354 | break; |
| 355 | } |
| 356 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 357 | if (ifacesIt->second.size() != 3) |
Matt Spinler | a82779f | 2019-01-09 12:39:42 -0600 | [diff] [blame] | 358 | { |
| 359 | break; |
| 360 | } |
| 361 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 362 | auto childPath = parent + '/'; |
Matt Spinler | a82779f | 2019-01-09 12:39:42 -0600 | [diff] [blame] | 363 | |
| 364 | // Remove this parent if there isn't a remaining child on this owner |
| 365 | auto child = std::find_if( |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 366 | interfaceMap.begin(), interfaceMap.end(), |
| 367 | [&owner, &childPath](const auto& entry) { |
Brad Bishop | 86d2880 | 2022-07-11 15:49:31 -0400 | [diff] [blame] | 368 | return entry.first.starts_with(childPath) && |
Matt Spinler | a82779f | 2019-01-09 12:39:42 -0600 | [diff] [blame] | 369 | (entry.second.find(owner) != entry.second.end()); |
| 370 | }); |
| 371 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 372 | if (child == interfaceMap.end()) |
Matt Spinler | a82779f | 2019-01-09 12:39:42 -0600 | [diff] [blame] | 373 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 374 | parentIt->second.erase(ifacesIt); |
| 375 | if (parentIt->second.empty()) |
Matt Spinler | a82779f | 2019-01-09 12:39:42 -0600 | [diff] [blame] | 376 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 377 | interfaceMap.erase(parentIt); |
Matt Spinler | a82779f | 2019-01-09 12:39:42 -0600 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | else |
| 381 | { |
| 382 | break; |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | |
Brad Bishop | 1e94e60 | 2022-06-02 19:47:53 -0400 | [diff] [blame] | 387 | int main() |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 388 | { |
Ed Tanous | 21c6059 | 2020-08-17 23:43:46 -0700 | [diff] [blame] | 389 | boost::asio::io_context io; |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 390 | std::shared_ptr<sdbusplus::asio::connection> systemBus = |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 391 | std::make_shared<sdbusplus::asio::connection>(io); |
| 392 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 393 | sdbusplus::asio::object_server server(systemBus); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 394 | |
| 395 | // Construct a signal set registered for process termination. |
| 396 | boost::asio::signal_set signals(io, SIGINT, SIGTERM); |
Brad Bishop | 2d41d6a | 2021-08-03 08:14:45 -0400 | [diff] [blame] | 397 | signals.async_wait( |
| 398 | [&io](const boost::system::error_code&, int) { io.stop(); }); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 399 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 400 | InterfaceMapType interfaceMap; |
| 401 | boost::container::flat_map<std::string, std::string> nameOwners; |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 402 | |
William A. Kennington III | 60ffc24 | 2022-12-02 19:36:01 -0800 | [diff] [blame] | 403 | auto nameChangeHandler = [&interfaceMap, &io, &nameOwners, &server, |
| 404 | systemBus](sdbusplus::message_t& message) { |
| 405 | std::string name; // well-known |
| 406 | std::string oldOwner; // unique-name |
| 407 | std::string newOwner; // unique-name |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 408 | |
William A. Kennington III | 60ffc24 | 2022-12-02 19:36:01 -0800 | [diff] [blame] | 409 | message.read(name, oldOwner, newOwner); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 410 | |
William A. Kennington III | 60ffc24 | 2022-12-02 19:36:01 -0800 | [diff] [blame] | 411 | if (name.starts_with(':')) |
| 412 | { |
| 413 | // We should do nothing with unique-name connections. |
| 414 | return; |
| 415 | } |
Patrick Williams | 4824820 | 2022-10-10 10:26:47 -0500 | [diff] [blame] | 416 | |
William A. Kennington III | 60ffc24 | 2022-12-02 19:36:01 -0800 | [diff] [blame] | 417 | if (!oldOwner.empty()) |
| 418 | { |
| 419 | processNameChangeDelete(io, nameOwners, name, oldOwner, |
| 420 | interfaceMap, associationMaps, server); |
| 421 | } |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 422 | |
William A. Kennington III | 60ffc24 | 2022-12-02 19:36:01 -0800 | [diff] [blame] | 423 | if (!newOwner.empty()) |
| 424 | { |
Brad Bishop | d6aa552 | 2022-05-31 19:23:48 -0400 | [diff] [blame] | 425 | #ifdef MAPPER_ENABLE_DEBUG |
William A. Kennington III | 60ffc24 | 2022-12-02 19:36:01 -0800 | [diff] [blame] | 426 | auto transaction = std::make_shared< |
| 427 | std::chrono::time_point<std::chrono::steady_clock>>( |
| 428 | std::chrono::steady_clock::now()); |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 429 | #endif |
William A. Kennington III | 60ffc24 | 2022-12-02 19:36:01 -0800 | [diff] [blame] | 430 | // New daemon added |
| 431 | if (needToIntrospect(name)) |
| 432 | { |
| 433 | nameOwners[newOwner] = name; |
| 434 | startNewIntrospect(systemBus.get(), io, interfaceMap, name, |
| 435 | associationMaps, |
Brad Bishop | d6aa552 | 2022-05-31 19:23:48 -0400 | [diff] [blame] | 436 | #ifdef MAPPER_ENABLE_DEBUG |
William A. Kennington III | 60ffc24 | 2022-12-02 19:36:01 -0800 | [diff] [blame] | 437 | transaction, |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 438 | #endif |
William A. Kennington III | 60ffc24 | 2022-12-02 19:36:01 -0800 | [diff] [blame] | 439 | server); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 440 | } |
William A. Kennington III | 60ffc24 | 2022-12-02 19:36:01 -0800 | [diff] [blame] | 441 | } |
| 442 | }; |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 443 | |
Patrick Williams | cc8070b | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 444 | sdbusplus::bus::match_t nameOwnerChanged( |
| 445 | static_cast<sdbusplus::bus_t&>(*systemBus), |
William A. Kennington III | 60ffc24 | 2022-12-02 19:36:01 -0800 | [diff] [blame] | 446 | sdbusplus::bus::match::rules::nameOwnerChanged(), |
| 447 | std::move(nameChangeHandler)); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 448 | |
William A. Kennington III | 60ffc24 | 2022-12-02 19:36:01 -0800 | [diff] [blame] | 449 | auto interfacesAddedHandler = [&io, &interfaceMap, &nameOwners, |
| 450 | &server](sdbusplus::message_t& message) { |
| 451 | sdbusplus::message::object_path objPath; |
| 452 | InterfacesAdded interfacesAdded; |
| 453 | message.read(objPath, interfacesAdded); |
| 454 | std::string wellKnown; |
| 455 | if (!getWellKnown(nameOwners, message.get_sender(), wellKnown)) |
| 456 | { |
| 457 | return; // only introspect well-known |
| 458 | } |
| 459 | if (needToIntrospect(wellKnown)) |
| 460 | { |
| 461 | processInterfaceAdded(io, interfaceMap, objPath, interfacesAdded, |
| 462 | wellKnown, associationMaps, server); |
| 463 | } |
| 464 | }; |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 465 | |
Patrick Williams | cc8070b | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 466 | sdbusplus::bus::match_t interfacesAdded( |
| 467 | static_cast<sdbusplus::bus_t&>(*systemBus), |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 468 | sdbusplus::bus::match::rules::interfacesAdded(), |
William A. Kennington III | 60ffc24 | 2022-12-02 19:36:01 -0800 | [diff] [blame] | 469 | std::move(interfacesAddedHandler)); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 470 | |
William A. Kennington III | 60ffc24 | 2022-12-02 19:36:01 -0800 | [diff] [blame] | 471 | auto interfacesRemovedHandler = [&io, &interfaceMap, &nameOwners, |
| 472 | &server](sdbusplus::message_t& message) { |
| 473 | sdbusplus::message::object_path objPath; |
| 474 | std::vector<std::string> interfacesRemoved; |
| 475 | message.read(objPath, interfacesRemoved); |
| 476 | auto connectionMap = interfaceMap.find(objPath.str); |
| 477 | if (connectionMap == interfaceMap.end()) |
| 478 | { |
| 479 | return; |
| 480 | } |
| 481 | |
| 482 | std::string sender; |
| 483 | if (!getWellKnown(nameOwners, message.get_sender(), sender)) |
| 484 | { |
| 485 | return; |
| 486 | } |
| 487 | for (const std::string& interface : interfacesRemoved) |
| 488 | { |
| 489 | auto interfaceSet = connectionMap->second.find(sender); |
| 490 | if (interfaceSet == connectionMap->second.end()) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 491 | { |
William A. Kennington III | 60ffc24 | 2022-12-02 19:36:01 -0800 | [diff] [blame] | 492 | continue; |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 493 | } |
| 494 | |
William A. Kennington III | 60ffc24 | 2022-12-02 19:36:01 -0800 | [diff] [blame] | 495 | if (interface == assocDefsInterface) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 496 | { |
William A. Kennington III | 60ffc24 | 2022-12-02 19:36:01 -0800 | [diff] [blame] | 497 | removeAssociation(io, objPath.str, sender, server, |
| 498 | associationMaps); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 499 | } |
William A. Kennington III | 60ffc24 | 2022-12-02 19:36:01 -0800 | [diff] [blame] | 500 | |
| 501 | interfaceSet->second.erase(interface); |
| 502 | |
| 503 | if (interfaceSet->second.empty()) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 504 | { |
William A. Kennington III | 60ffc24 | 2022-12-02 19:36:01 -0800 | [diff] [blame] | 505 | // If this was the last interface on this connection, |
| 506 | // erase the connection |
| 507 | connectionMap->second.erase(interfaceSet); |
| 508 | |
| 509 | // Instead of checking if every single path is the endpoint |
| 510 | // of an association that needs to be moved to pending, |
| 511 | // only check when the only remaining owner of this path is |
| 512 | // ourself, which would be because we still own the |
| 513 | // association path. |
| 514 | if ((connectionMap->second.size() == 1) && |
| 515 | (connectionMap->second.begin()->first == |
| 516 | "xyz.openbmc_project.ObjectMapper")) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 517 | { |
William A. Kennington III | 60ffc24 | 2022-12-02 19:36:01 -0800 | [diff] [blame] | 518 | // Remove the 2 association D-Bus paths and move the |
| 519 | // association to pending. |
| 520 | moveAssociationToPending(io, objPath.str, associationMaps, |
| 521 | server); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 522 | } |
| 523 | } |
William A. Kennington III | 60ffc24 | 2022-12-02 19:36:01 -0800 | [diff] [blame] | 524 | } |
| 525 | // If this was the last connection on this object path, |
| 526 | // erase the object path |
| 527 | if (connectionMap->second.empty()) |
| 528 | { |
| 529 | interfaceMap.erase(connectionMap); |
| 530 | } |
Matt Spinler | a82779f | 2019-01-09 12:39:42 -0600 | [diff] [blame] | 531 | |
William A. Kennington III | 60ffc24 | 2022-12-02 19:36:01 -0800 | [diff] [blame] | 532 | removeUnneededParents(objPath.str, sender, interfaceMap); |
| 533 | }; |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 534 | |
Patrick Williams | cc8070b | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 535 | sdbusplus::bus::match_t interfacesRemoved( |
| 536 | static_cast<sdbusplus::bus_t&>(*systemBus), |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 537 | sdbusplus::bus::match::rules::interfacesRemoved(), |
William A. Kennington III | 60ffc24 | 2022-12-02 19:36:01 -0800 | [diff] [blame] | 538 | std::move(interfacesRemovedHandler)); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 539 | |
William A. Kennington III | 60ffc24 | 2022-12-02 19:36:01 -0800 | [diff] [blame] | 540 | auto associationChangedHandler = [&io, &server, &nameOwners, &interfaceMap]( |
| 541 | sdbusplus::message_t& message) { |
| 542 | std::string objectName; |
| 543 | boost::container::flat_map<std::string, |
| 544 | std::variant<std::vector<Association>>> |
| 545 | values; |
| 546 | message.read(objectName, values); |
| 547 | auto prop = values.find(assocDefsProperty); |
| 548 | if (prop != values.end()) |
| 549 | { |
| 550 | std::vector<Association> associations = |
| 551 | std::get<std::vector<Association>>(prop->second); |
| 552 | |
| 553 | std::string wellKnown; |
| 554 | if (!getWellKnown(nameOwners, message.get_sender(), wellKnown)) |
Matt Spinler | 8f876a5 | 2019-04-15 13:22:50 -0500 | [diff] [blame] | 555 | { |
William A. Kennington III | 60ffc24 | 2022-12-02 19:36:01 -0800 | [diff] [blame] | 556 | return; |
Matt Spinler | 8f876a5 | 2019-04-15 13:22:50 -0500 | [diff] [blame] | 557 | } |
William A. Kennington III | 60ffc24 | 2022-12-02 19:36:01 -0800 | [diff] [blame] | 558 | associationChanged(io, server, associations, message.get_path(), |
| 559 | wellKnown, interfaceMap, associationMaps); |
| 560 | } |
| 561 | }; |
Patrick Williams | cc8070b | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 562 | sdbusplus::bus::match_t assocChangedMatch( |
| 563 | static_cast<sdbusplus::bus_t&>(*systemBus), |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 564 | sdbusplus::bus::match::rules::interface( |
| 565 | "org.freedesktop.DBus.Properties") + |
| 566 | sdbusplus::bus::match::rules::member("PropertiesChanged") + |
Matt Spinler | 8f876a5 | 2019-04-15 13:22:50 -0500 | [diff] [blame] | 567 | sdbusplus::bus::match::rules::argN(0, assocDefsInterface), |
William A. Kennington III | 60ffc24 | 2022-12-02 19:36:01 -0800 | [diff] [blame] | 568 | std::move(associationChangedHandler)); |
Matt Spinler | 8f876a5 | 2019-04-15 13:22:50 -0500 | [diff] [blame] | 569 | |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 570 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface = |
Brad Bishop | a02cd54 | 2021-10-12 19:12:42 -0400 | [diff] [blame] | 571 | server.add_interface("/xyz/openbmc_project/object_mapper", |
| 572 | "xyz.openbmc_project.ObjectMapper"); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 573 | |
| 574 | iface->register_method( |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 575 | "GetAncestors", [&interfaceMap](std::string& reqPath, |
| 576 | std::vector<std::string>& interfaces) { |
| 577 | return getAncestors(interfaceMap, reqPath, interfaces); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 578 | }); |
| 579 | |
| 580 | iface->register_method( |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 581 | "GetObject", [&interfaceMap](const std::string& path, |
| 582 | std::vector<std::string>& interfaces) { |
| 583 | return getObject(interfaceMap, path, interfaces); |
| 584 | }); |
| 585 | |
| 586 | iface->register_method( |
| 587 | "GetSubTree", [&interfaceMap](std::string& reqPath, int32_t depth, |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 588 | std::vector<std::string>& interfaces) { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 589 | return getSubTree(interfaceMap, reqPath, depth, interfaces); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 590 | }); |
| 591 | |
| 592 | iface->register_method( |
| 593 | "GetSubTreePaths", |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 594 | [&interfaceMap](std::string& reqPath, int32_t depth, |
| 595 | std::vector<std::string>& interfaces) { |
| 596 | return getSubTreePaths(interfaceMap, reqPath, depth, interfaces); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 597 | }); |
| 598 | |
| 599 | iface->initialize(); |
| 600 | |
| 601 | io.post([&]() { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 602 | doListNames(io, interfaceMap, systemBus.get(), nameOwners, |
Matt Spinler | 11401e2 | 2019-04-08 13:13:25 -0500 | [diff] [blame] | 603 | associationMaps, server); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 604 | }); |
| 605 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 606 | systemBus->request_name("xyz.openbmc_project.ObjectMapper"); |
Vishwanatha Subbanna | 64354ef | 2020-08-21 03:35:26 -0500 | [diff] [blame] | 607 | |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 608 | io.run(); |
| 609 | } |