Andrew Geissler | a80a3af | 2019-02-04 14:01:49 -0600 | [diff] [blame] | 1 | #include "associations.hpp" |
Andrew Geissler | 3b025e6 | 2019-02-01 10:33:54 -0600 | [diff] [blame] | 2 | #include "processing.hpp" |
Matt Spinler | 35396c1 | 2019-04-05 11:46:57 -0500 | [diff] [blame] | 3 | #include "types.hpp" |
Matt Spinler | dd94586 | 2018-09-07 12:41:05 -0500 | [diff] [blame] | 4 | |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 5 | #include <tinyxml2.h> |
| 6 | |
Ed Tanous | 21c6059 | 2020-08-17 23:43:46 -0700 | [diff] [blame] | 7 | #include <boost/asio/io_context.hpp> |
| 8 | #include <boost/asio/signal_set.hpp> |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 9 | #include <boost/container/flat_map.hpp> |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 10 | #include <sdbusplus/asio/connection.hpp> |
| 11 | #include <sdbusplus/asio/object_server.hpp> |
Konstantin Aladyshev | b15df6b | 2022-01-11 14:50:55 +0300 | [diff] [blame] | 12 | #include <xyz/openbmc_project/Common/error.hpp> |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 13 | |
Brad Bishop | 2352088 | 2022-05-26 21:39:53 -0400 | [diff] [blame] | 14 | #include <atomic> |
| 15 | #include <chrono> |
Brad Bishop | ea11946 | 2022-05-31 20:32:04 -0400 | [diff] [blame] | 16 | #include <exception> |
Brad Bishop | 2352088 | 2022-05-26 21:39:53 -0400 | [diff] [blame] | 17 | #include <iomanip> |
| 18 | #include <iostream> |
Brandon Kim | f6ebfc7 | 2022-07-07 12:53:44 -0700 | [diff] [blame] | 19 | #include <string> |
| 20 | #include <string_view> |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame] | 21 | #include <utility> |
Brad Bishop | 2352088 | 2022-05-26 21:39:53 -0400 | [diff] [blame] | 22 | |
Matt Spinler | e2359fb | 2019-04-05 14:11:33 -0500 | [diff] [blame] | 23 | AssociationMaps associationMaps; |
Matt Spinler | 937a232 | 2019-01-23 13:54:22 -0600 | [diff] [blame] | 24 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 25 | void updateOwners(sdbusplus::asio::connection* conn, |
| 26 | boost::container::flat_map<std::string, std::string>& owners, |
| 27 | const std::string& newObject) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 28 | { |
Brad Bishop | 86d2880 | 2022-07-11 15:49:31 -0400 | [diff] [blame] | 29 | if (newObject.starts_with(":")) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 30 | { |
| 31 | return; |
| 32 | } |
| 33 | conn->async_method_call( |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 34 | [&, newObject](const boost::system::error_code ec, |
| 35 | const std::string& nameOwner) { |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 36 | if (ec) |
| 37 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 38 | std::cerr << "Error getting owner of " << newObject << " : " |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 39 | << ec << "\n"; |
| 40 | return; |
| 41 | } |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 42 | owners[nameOwner] = newObject; |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 43 | }, |
| 44 | "org.freedesktop.DBus", "/", "org.freedesktop.DBus", "GetNameOwner", |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 45 | newObject); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 48 | void sendIntrospectionCompleteSignal(sdbusplus::asio::connection* systemBus, |
| 49 | const std::string& processName) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 50 | { |
| 51 | // TODO(ed) This signal doesn't get exposed properly in the |
| 52 | // introspect right now. Find out how to register signals in |
| 53 | // sdbusplus |
Patrick Williams | cc8070b | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 54 | sdbusplus::message_t m = systemBus->new_signal( |
Brad Bishop | a02cd54 | 2021-10-12 19:12:42 -0400 | [diff] [blame] | 55 | "/xyz/openbmc_project/object_mapper", |
| 56 | "xyz.openbmc_project.ObjectMapper.Private", "IntrospectionComplete"); |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 57 | m.append(processName); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 58 | m.signal_send(); |
| 59 | } |
| 60 | |
| 61 | struct InProgressIntrospect |
| 62 | { |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame] | 63 | InProgressIntrospect() = delete; |
| 64 | InProgressIntrospect(const InProgressIntrospect&) = delete; |
| 65 | InProgressIntrospect(InProgressIntrospect&&) = delete; |
| 66 | InProgressIntrospect& operator=(const InProgressIntrospect&) = delete; |
| 67 | InProgressIntrospect& operator=(InProgressIntrospect&&) = delete; |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 68 | InProgressIntrospect( |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 69 | sdbusplus::asio::connection* systemBus, boost::asio::io_context& io, |
| 70 | const std::string& processName, AssociationMaps& am |
Brad Bishop | d6aa552 | 2022-05-31 19:23:48 -0400 | [diff] [blame] | 71 | #ifdef MAPPER_ENABLE_DEBUG |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 72 | , |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 73 | std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>> |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 74 | globalStartTime |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 75 | #endif |
| 76 | ) : |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 77 | systemBus(systemBus), |
| 78 | io(io), processName(processName), assocMaps(am) |
Brad Bishop | d6aa552 | 2022-05-31 19:23:48 -0400 | [diff] [blame] | 79 | #ifdef MAPPER_ENABLE_DEBUG |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 80 | , |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame] | 81 | globalStartTime(std::move(globalStartTime)), |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 82 | processStartTime(std::chrono::steady_clock::now()) |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 83 | #endif |
Brad Bishop | 2352088 | 2022-05-26 21:39:53 -0400 | [diff] [blame] | 84 | {} |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 85 | ~InProgressIntrospect() |
| 86 | { |
Brad Bishop | ea11946 | 2022-05-31 20:32:04 -0400 | [diff] [blame] | 87 | try |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 88 | { |
Brad Bishop | ea11946 | 2022-05-31 20:32:04 -0400 | [diff] [blame] | 89 | sendIntrospectionCompleteSignal(systemBus, processName); |
Brad Bishop | d6aa552 | 2022-05-31 19:23:48 -0400 | [diff] [blame] | 90 | #ifdef MAPPER_ENABLE_DEBUG |
Brad Bishop | ea11946 | 2022-05-31 20:32:04 -0400 | [diff] [blame] | 91 | std::chrono::duration<float> diff = |
| 92 | std::chrono::steady_clock::now() - processStartTime; |
| 93 | std::cout << std::setw(50) << processName << " scan took " |
| 94 | << diff.count() << " seconds\n"; |
| 95 | |
| 96 | // If we're the last outstanding caller globally, calculate the |
| 97 | // time it took |
| 98 | if (globalStartTime != nullptr && globalStartTime.use_count() == 1) |
| 99 | { |
| 100 | diff = std::chrono::steady_clock::now() - *globalStartTime; |
| 101 | std::cout << "Total scan took " << diff.count() |
| 102 | << " seconds to complete\n"; |
| 103 | } |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 104 | #endif |
Brad Bishop | ea11946 | 2022-05-31 20:32:04 -0400 | [diff] [blame] | 105 | } |
| 106 | catch (const std::exception& e) |
| 107 | { |
| 108 | std::cerr |
| 109 | << "Terminating, unhandled exception while introspecting: " |
| 110 | << e.what() << "\n"; |
| 111 | std::terminate(); |
| 112 | } |
| 113 | catch (...) |
| 114 | { |
| 115 | std::cerr |
| 116 | << "Terminating, unhandled exception while introspecting\n"; |
| 117 | std::terminate(); |
| 118 | } |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 119 | } |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 120 | sdbusplus::asio::connection* systemBus; |
Ed Tanous | 21c6059 | 2020-08-17 23:43:46 -0700 | [diff] [blame] | 121 | boost::asio::io_context& io; |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 122 | std::string processName; |
Matt Spinler | 11401e2 | 2019-04-08 13:13:25 -0500 | [diff] [blame] | 123 | AssociationMaps& assocMaps; |
Brad Bishop | d6aa552 | 2022-05-31 19:23:48 -0400 | [diff] [blame] | 124 | #ifdef MAPPER_ENABLE_DEBUG |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 125 | std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>> |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 126 | globalStartTime; |
| 127 | std::chrono::time_point<std::chrono::steady_clock> processStartTime; |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 128 | #endif |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 129 | }; |
| 130 | |
Kallas, Pawel | 5b4357d | 2022-10-12 15:36:37 +0200 | [diff] [blame^] | 131 | void doAssociations(boost::asio::io_context& io, |
| 132 | sdbusplus::asio::connection* systemBus, |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 133 | InterfaceMapType& interfaceMap, |
| 134 | sdbusplus::asio::object_server& objectServer, |
| 135 | const std::string& processName, const std::string& path, |
| 136 | int timeoutRetries = 0) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 137 | { |
Adrian Ambrożewicz | bb40bd3 | 2021-02-12 13:36:26 +0100 | [diff] [blame] | 138 | constexpr int maxTimeoutRetries = 3; |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 139 | systemBus->async_method_call( |
Kallas, Pawel | 5b4357d | 2022-10-12 15:36:37 +0200 | [diff] [blame^] | 140 | [&io, &objectServer, path, processName, &interfaceMap, systemBus, |
Adrian Ambrożewicz | bb40bd3 | 2021-02-12 13:36:26 +0100 | [diff] [blame] | 141 | timeoutRetries]( |
Matt Spinler | 937a232 | 2019-01-23 13:54:22 -0600 | [diff] [blame] | 142 | const boost::system::error_code ec, |
Patrick Williams | 2bb2d6b | 2020-05-13 17:59:02 -0500 | [diff] [blame] | 143 | const std::variant<std::vector<Association>>& variantAssociations) { |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 144 | if (ec) |
| 145 | { |
Adrian Ambrożewicz | bb40bd3 | 2021-02-12 13:36:26 +0100 | [diff] [blame] | 146 | if (ec.value() == boost::system::errc::timed_out && |
| 147 | timeoutRetries < maxTimeoutRetries) |
| 148 | { |
Kallas, Pawel | 5b4357d | 2022-10-12 15:36:37 +0200 | [diff] [blame^] | 149 | doAssociations(io, systemBus, interfaceMap, objectServer, |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 150 | processName, path, timeoutRetries + 1); |
Adrian Ambrożewicz | bb40bd3 | 2021-02-12 13:36:26 +0100 | [diff] [blame] | 151 | return; |
| 152 | } |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 153 | std::cerr << "Error getting associations from " << path << "\n"; |
| 154 | } |
| 155 | std::vector<Association> associations = |
Patrick Williams | b05bc12 | 2020-05-13 12:21:00 -0500 | [diff] [blame] | 156 | std::get<std::vector<Association>>(variantAssociations); |
Kallas, Pawel | 5b4357d | 2022-10-12 15:36:37 +0200 | [diff] [blame^] | 157 | associationChanged(io, objectServer, associations, path, |
| 158 | processName, interfaceMap, associationMaps); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 159 | }, |
| 160 | processName, path, "org.freedesktop.DBus.Properties", "Get", |
John Wang | d0cf942 | 2019-09-17 16:01:34 +0800 | [diff] [blame] | 161 | assocDefsInterface, assocDefsProperty); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Kallas, Pawel | 5b4357d | 2022-10-12 15:36:37 +0200 | [diff] [blame^] | 164 | void doIntrospect(boost::asio::io_context& io, |
| 165 | sdbusplus::asio::connection* systemBus, |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame] | 166 | const std::shared_ptr<InProgressIntrospect>& transaction, |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 167 | InterfaceMapType& interfaceMap, |
| 168 | sdbusplus::asio::object_server& objectServer, |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame] | 169 | const std::string& path, int timeoutRetries = 0) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 170 | { |
Vernon Mauery | c52be0d | 2020-01-14 11:14:25 -0800 | [diff] [blame] | 171 | constexpr int maxTimeoutRetries = 3; |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 172 | systemBus->async_method_call( |
Kallas, Pawel | 5b4357d | 2022-10-12 15:36:37 +0200 | [diff] [blame^] | 173 | [&io, &interfaceMap, &objectServer, transaction, path, systemBus, |
Vernon Mauery | c52be0d | 2020-01-14 11:14:25 -0800 | [diff] [blame] | 174 | timeoutRetries](const boost::system::error_code ec, |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 175 | const std::string& introspectXml) { |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 176 | if (ec) |
| 177 | { |
Vernon Mauery | c52be0d | 2020-01-14 11:14:25 -0800 | [diff] [blame] | 178 | if (ec.value() == boost::system::errc::timed_out && |
| 179 | timeoutRetries < maxTimeoutRetries) |
| 180 | { |
Kallas, Pawel | 5b4357d | 2022-10-12 15:36:37 +0200 | [diff] [blame^] | 181 | doIntrospect(io, systemBus, transaction, interfaceMap, |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 182 | objectServer, path, timeoutRetries + 1); |
Vernon Mauery | c52be0d | 2020-01-14 11:14:25 -0800 | [diff] [blame] | 183 | return; |
| 184 | } |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 185 | std::cerr << "Introspect call failed with error: " << ec << ", " |
| 186 | << ec.message() |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 187 | << " on process: " << transaction->processName |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 188 | << " path: " << path << "\n"; |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | tinyxml2::XMLDocument doc; |
| 193 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 194 | tinyxml2::XMLError e = doc.Parse(introspectXml.c_str()); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 195 | if (e != tinyxml2::XMLError::XML_SUCCESS) |
| 196 | { |
| 197 | std::cerr << "XML parsing failed\n"; |
| 198 | return; |
| 199 | } |
| 200 | |
| 201 | tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node"); |
| 202 | if (pRoot == nullptr) |
| 203 | { |
| 204 | std::cerr << "XML document did not contain any data\n"; |
| 205 | return; |
| 206 | } |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 207 | auto& thisPathMap = interfaceMap[path]; |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 208 | tinyxml2::XMLElement* pElement = |
| 209 | pRoot->FirstChildElement("interface"); |
| 210 | while (pElement != nullptr) |
| 211 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 212 | const char* ifaceName = pElement->Attribute("name"); |
| 213 | if (ifaceName == nullptr) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 214 | { |
| 215 | continue; |
| 216 | } |
| 217 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 218 | thisPathMap[transaction->processName].emplace(ifaceName); |
Ed Tanous | d4dd96a | 2018-11-12 11:37:44 -0800 | [diff] [blame] | 219 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 220 | if (std::strcmp(ifaceName, assocDefsInterface) == 0) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 221 | { |
Kallas, Pawel | 5b4357d | 2022-10-12 15:36:37 +0200 | [diff] [blame^] | 222 | doAssociations(io, systemBus, interfaceMap, objectServer, |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 223 | transaction->processName, path); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 224 | } |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 225 | |
| 226 | pElement = pElement->NextSiblingElement("interface"); |
| 227 | } |
| 228 | |
Matt Spinler | 11401e2 | 2019-04-08 13:13:25 -0500 | [diff] [blame] | 229 | // Check if this new path has a pending association that can |
| 230 | // now be completed. |
Kallas, Pawel | 5b4357d | 2022-10-12 15:36:37 +0200 | [diff] [blame^] | 231 | checkIfPendingAssociation(io, path, interfaceMap, |
Matt Spinler | 11401e2 | 2019-04-08 13:13:25 -0500 | [diff] [blame] | 232 | transaction->assocMaps, objectServer); |
| 233 | |
Ed Tanous | 50232cd | 2018-11-12 11:34:43 -0800 | [diff] [blame] | 234 | pElement = pRoot->FirstChildElement("node"); |
| 235 | while (pElement != nullptr) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 236 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 237 | const char* childPath = pElement->Attribute("name"); |
| 238 | if (childPath != nullptr) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 239 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 240 | std::string parentPath(path); |
| 241 | if (parentPath == "/") |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 242 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 243 | parentPath.clear(); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 244 | } |
Ed Tanous | 50232cd | 2018-11-12 11:34:43 -0800 | [diff] [blame] | 245 | |
Kallas, Pawel | 5b4357d | 2022-10-12 15:36:37 +0200 | [diff] [blame^] | 246 | doIntrospect(io, systemBus, transaction, interfaceMap, |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 247 | objectServer, parentPath + "/" + childPath); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 248 | } |
Ed Tanous | 50232cd | 2018-11-12 11:34:43 -0800 | [diff] [blame] | 249 | pElement = pElement->NextSiblingElement("node"); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 250 | } |
| 251 | }, |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 252 | transaction->processName, path, "org.freedesktop.DBus.Introspectable", |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 253 | "Introspect"); |
| 254 | } |
| 255 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 256 | void startNewIntrospect( |
| 257 | sdbusplus::asio::connection* systemBus, boost::asio::io_context& io, |
| 258 | InterfaceMapType& interfaceMap, const std::string& processName, |
Matt Spinler | 11401e2 | 2019-04-08 13:13:25 -0500 | [diff] [blame] | 259 | AssociationMaps& assocMaps, |
Brad Bishop | d6aa552 | 2022-05-31 19:23:48 -0400 | [diff] [blame] | 260 | #ifdef MAPPER_ENABLE_DEBUG |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 261 | std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>> |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 262 | globalStartTime, |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 263 | #endif |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 264 | sdbusplus::asio::object_server& objectServer) |
| 265 | { |
Brad Bishop | 1e94e60 | 2022-06-02 19:47:53 -0400 | [diff] [blame] | 266 | if (needToIntrospect(processName)) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 267 | { |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 268 | std::shared_ptr<InProgressIntrospect> transaction = |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 269 | std::make_shared<InProgressIntrospect>(systemBus, io, processName, |
Matt Spinler | 11401e2 | 2019-04-08 13:13:25 -0500 | [diff] [blame] | 270 | assocMaps |
Brad Bishop | d6aa552 | 2022-05-31 19:23:48 -0400 | [diff] [blame] | 271 | #ifdef MAPPER_ENABLE_DEBUG |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 272 | , |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 273 | globalStartTime |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 274 | #endif |
| 275 | ); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 276 | |
Kallas, Pawel | 5b4357d | 2022-10-12 15:36:37 +0200 | [diff] [blame^] | 277 | doIntrospect(io, systemBus, transaction, interfaceMap, objectServer, |
| 278 | "/"); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
| 282 | // TODO(ed) replace with std::set_intersection once c++17 is available |
| 283 | template <class InputIt1, class InputIt2> |
| 284 | bool intersect(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2) |
| 285 | { |
| 286 | while (first1 != last1 && first2 != last2) |
| 287 | { |
| 288 | if (*first1 < *first2) |
| 289 | { |
| 290 | ++first1; |
| 291 | continue; |
| 292 | } |
| 293 | if (*first2 < *first1) |
| 294 | { |
| 295 | ++first2; |
| 296 | continue; |
| 297 | } |
| 298 | return true; |
| 299 | } |
| 300 | return false; |
| 301 | } |
| 302 | |
| 303 | void doListNames( |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 304 | boost::asio::io_context& io, InterfaceMapType& interfaceMap, |
| 305 | sdbusplus::asio::connection* systemBus, |
| 306 | boost::container::flat_map<std::string, std::string>& nameOwners, |
Matt Spinler | 11401e2 | 2019-04-08 13:13:25 -0500 | [diff] [blame] | 307 | AssociationMaps& assocMaps, sdbusplus::asio::object_server& objectServer) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 308 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 309 | systemBus->async_method_call( |
| 310 | [&io, &interfaceMap, &nameOwners, &objectServer, systemBus, |
Matt Spinler | 11401e2 | 2019-04-08 13:13:25 -0500 | [diff] [blame] | 311 | &assocMaps](const boost::system::error_code ec, |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 312 | std::vector<std::string> processNames) { |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 313 | if (ec) |
| 314 | { |
| 315 | std::cerr << "Error getting names: " << ec << "\n"; |
| 316 | std::exit(EXIT_FAILURE); |
| 317 | return; |
| 318 | } |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 319 | // Try to make startup consistent |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 320 | std::sort(processNames.begin(), processNames.end()); |
Brad Bishop | d6aa552 | 2022-05-31 19:23:48 -0400 | [diff] [blame] | 321 | #ifdef MAPPER_ENABLE_DEBUG |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 322 | std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>> |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 323 | globalStartTime = std::make_shared< |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 324 | std::chrono::time_point<std::chrono::steady_clock>>( |
| 325 | std::chrono::steady_clock::now()); |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 326 | #endif |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 327 | for (const std::string& processName : processNames) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 328 | { |
Brad Bishop | 1e94e60 | 2022-06-02 19:47:53 -0400 | [diff] [blame] | 329 | if (needToIntrospect(processName)) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 330 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 331 | startNewIntrospect(systemBus, io, interfaceMap, processName, |
| 332 | assocMaps, |
Brad Bishop | d6aa552 | 2022-05-31 19:23:48 -0400 | [diff] [blame] | 333 | #ifdef MAPPER_ENABLE_DEBUG |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 334 | globalStartTime, |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 335 | #endif |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 336 | objectServer); |
| 337 | updateOwners(systemBus, nameOwners, processName); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | }, |
| 341 | "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", |
| 342 | "ListNames"); |
| 343 | } |
| 344 | |
Ed Tanous | 964681c | 2022-07-08 12:47:24 -0700 | [diff] [blame] | 345 | void addObjectMapResult(std::vector<InterfaceMapType::value_type>& objectMap, |
| 346 | const std::string& objectPath, |
| 347 | const ConnectionNames::value_type& interfaceMap) |
Matt Spinler | 9f0958e | 2018-09-11 08:26:10 -0500 | [diff] [blame] | 348 | { |
| 349 | // Adds an object path/service name/interface list entry to |
Matt Spinler | 47c0975 | 2018-11-29 14:54:13 -0600 | [diff] [blame] | 350 | // the results of GetSubTree and GetAncestors. |
Matt Spinler | 9f0958e | 2018-09-11 08:26:10 -0500 | [diff] [blame] | 351 | // If an entry for the object path already exists, just add the |
| 352 | // service name and interfaces to that entry, otherwise create |
| 353 | // a new entry. |
| 354 | auto entry = std::find_if( |
Matt Spinler | 47c0975 | 2018-11-29 14:54:13 -0600 | [diff] [blame] | 355 | objectMap.begin(), objectMap.end(), |
Matt Spinler | 9f0958e | 2018-09-11 08:26:10 -0500 | [diff] [blame] | 356 | [&objectPath](const auto& i) { return objectPath == i.first; }); |
| 357 | |
Matt Spinler | 47c0975 | 2018-11-29 14:54:13 -0600 | [diff] [blame] | 358 | if (entry != objectMap.end()) |
Matt Spinler | 9f0958e | 2018-09-11 08:26:10 -0500 | [diff] [blame] | 359 | { |
| 360 | entry->second.emplace(interfaceMap); |
| 361 | } |
| 362 | else |
| 363 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 364 | InterfaceMapType::value_type object; |
Matt Spinler | 9f0958e | 2018-09-11 08:26:10 -0500 | [diff] [blame] | 365 | object.first = objectPath; |
| 366 | object.second.emplace(interfaceMap); |
Matt Spinler | 47c0975 | 2018-11-29 14:54:13 -0600 | [diff] [blame] | 367 | objectMap.push_back(object); |
Matt Spinler | 9f0958e | 2018-09-11 08:26:10 -0500 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | |
Matt Spinler | a82779f | 2019-01-09 12:39:42 -0600 | [diff] [blame] | 371 | // Remove parents of the passed in path that: |
| 372 | // 1) Only have the 3 default interfaces on them |
| 373 | // - Means D-Bus created these, not application code, |
| 374 | // with the Properties, Introspectable, and Peer ifaces |
| 375 | // 2) Have no other child for this owner |
| 376 | void removeUnneededParents(const std::string& objectPath, |
| 377 | const std::string& owner, |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 378 | InterfaceMapType& interfaceMap) |
Matt Spinler | a82779f | 2019-01-09 12:39:42 -0600 | [diff] [blame] | 379 | { |
| 380 | auto parent = objectPath; |
| 381 | |
| 382 | while (true) |
| 383 | { |
| 384 | auto pos = parent.find_last_of('/'); |
| 385 | if ((pos == std::string::npos) || (pos == 0)) |
| 386 | { |
| 387 | break; |
| 388 | } |
| 389 | parent = parent.substr(0, pos); |
| 390 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 391 | auto parentIt = interfaceMap.find(parent); |
| 392 | if (parentIt == interfaceMap.end()) |
Matt Spinler | a82779f | 2019-01-09 12:39:42 -0600 | [diff] [blame] | 393 | { |
| 394 | break; |
| 395 | } |
| 396 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 397 | auto ifacesIt = parentIt->second.find(owner); |
| 398 | if (ifacesIt == parentIt->second.end()) |
Matt Spinler | a82779f | 2019-01-09 12:39:42 -0600 | [diff] [blame] | 399 | { |
| 400 | break; |
| 401 | } |
| 402 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 403 | if (ifacesIt->second.size() != 3) |
Matt Spinler | a82779f | 2019-01-09 12:39:42 -0600 | [diff] [blame] | 404 | { |
| 405 | break; |
| 406 | } |
| 407 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 408 | auto childPath = parent + '/'; |
Matt Spinler | a82779f | 2019-01-09 12:39:42 -0600 | [diff] [blame] | 409 | |
| 410 | // Remove this parent if there isn't a remaining child on this owner |
| 411 | auto child = std::find_if( |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 412 | interfaceMap.begin(), interfaceMap.end(), |
| 413 | [&owner, &childPath](const auto& entry) { |
Brad Bishop | 86d2880 | 2022-07-11 15:49:31 -0400 | [diff] [blame] | 414 | return entry.first.starts_with(childPath) && |
Matt Spinler | a82779f | 2019-01-09 12:39:42 -0600 | [diff] [blame] | 415 | (entry.second.find(owner) != entry.second.end()); |
| 416 | }); |
| 417 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 418 | if (child == interfaceMap.end()) |
Matt Spinler | a82779f | 2019-01-09 12:39:42 -0600 | [diff] [blame] | 419 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 420 | parentIt->second.erase(ifacesIt); |
| 421 | if (parentIt->second.empty()) |
Matt Spinler | a82779f | 2019-01-09 12:39:42 -0600 | [diff] [blame] | 422 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 423 | interfaceMap.erase(parentIt); |
Matt Spinler | a82779f | 2019-01-09 12:39:42 -0600 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | else |
| 427 | { |
| 428 | break; |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 433 | std::vector<InterfaceMapType::value_type> |
| 434 | getAncestors(const InterfaceMapType& interfaceMap, std::string reqPath, |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 435 | std::vector<std::string>& interfaces) |
| 436 | { |
| 437 | // Interfaces need to be sorted for intersect to function |
| 438 | std::sort(interfaces.begin(), interfaces.end()); |
| 439 | |
Brad Bishop | 86d2880 | 2022-07-11 15:49:31 -0400 | [diff] [blame] | 440 | if (reqPath.ends_with("/")) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 441 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 442 | reqPath.pop_back(); |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 443 | } |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame] | 444 | if (!reqPath.empty() && interfaceMap.find(reqPath) == interfaceMap.end()) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 445 | { |
Konstantin Aladyshev | b15df6b | 2022-01-11 14:50:55 +0300 | [diff] [blame] | 446 | throw sdbusplus::xyz::openbmc_project::Common::Error:: |
| 447 | ResourceNotFound(); |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 450 | std::vector<InterfaceMapType::value_type> ret; |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame] | 451 | for (const auto& objectPath : interfaceMap) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 452 | { |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame] | 453 | const auto& thisPath = objectPath.first; |
Brandon Kim | 82720f0 | 2022-07-08 09:57:22 -0700 | [diff] [blame] | 454 | |
| 455 | if (reqPath == thisPath) |
| 456 | { |
| 457 | continue; |
| 458 | } |
| 459 | |
Brad Bishop | 86d2880 | 2022-07-11 15:49:31 -0400 | [diff] [blame] | 460 | if (reqPath.starts_with(thisPath)) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 461 | { |
| 462 | if (interfaces.empty()) |
| 463 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 464 | ret.emplace_back(objectPath); |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 465 | } |
| 466 | else |
| 467 | { |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame] | 468 | for (const auto& interfaceMap : objectPath.second) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 469 | { |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 470 | if (intersect(interfaces.begin(), interfaces.end(), |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 471 | interfaceMap.second.begin(), |
| 472 | interfaceMap.second.end())) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 473 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 474 | addObjectMapResult(ret, thisPath, interfaceMap); |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | return ret; |
| 482 | } |
| 483 | |
Ed Tanous | 964681c | 2022-07-08 12:47:24 -0700 | [diff] [blame] | 484 | ConnectionNames getObject(const InterfaceMapType& interfaceMap, |
| 485 | const std::string& path, |
| 486 | std::vector<std::string>& interfaces) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 487 | { |
Ed Tanous | 964681c | 2022-07-08 12:47:24 -0700 | [diff] [blame] | 488 | ConnectionNames results; |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 489 | |
| 490 | // Interfaces need to be sorted for intersect to function |
| 491 | std::sort(interfaces.begin(), interfaces.end()); |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 492 | auto pathRef = interfaceMap.find(path); |
| 493 | if (pathRef == interfaceMap.end()) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 494 | { |
Konstantin Aladyshev | b15df6b | 2022-01-11 14:50:55 +0300 | [diff] [blame] | 495 | throw sdbusplus::xyz::openbmc_project::Common::Error:: |
| 496 | ResourceNotFound(); |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 497 | } |
| 498 | if (interfaces.empty()) |
| 499 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 500 | return pathRef->second; |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 501 | } |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame] | 502 | for (const auto& interfaceMap : pathRef->second) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 503 | { |
| 504 | if (intersect(interfaces.begin(), interfaces.end(), |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 505 | interfaceMap.second.begin(), interfaceMap.second.end())) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 506 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 507 | results.emplace(interfaceMap.first, interfaceMap.second); |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | |
| 511 | if (results.empty()) |
| 512 | { |
Konstantin Aladyshev | b15df6b | 2022-01-11 14:50:55 +0300 | [diff] [blame] | 513 | throw sdbusplus::xyz::openbmc_project::Common::Error:: |
| 514 | ResourceNotFound(); |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | return results; |
| 518 | } |
| 519 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 520 | std::vector<InterfaceMapType::value_type> |
| 521 | getSubTree(const InterfaceMapType& interfaceMap, std::string reqPath, |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 522 | int32_t depth, std::vector<std::string>& interfaces) |
| 523 | { |
| 524 | if (depth <= 0) |
| 525 | { |
| 526 | depth = std::numeric_limits<int32_t>::max(); |
| 527 | } |
| 528 | // Interfaces need to be sorted for intersect to function |
| 529 | std::sort(interfaces.begin(), interfaces.end()); |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 530 | |
Brandon Kim | f6ebfc7 | 2022-07-07 12:53:44 -0700 | [diff] [blame] | 531 | // reqPath is now guaranteed to have a trailing "/" while reqPathStripped |
| 532 | // will be guaranteed not to have a trailing "/" |
Brad Bishop | 86d2880 | 2022-07-11 15:49:31 -0400 | [diff] [blame] | 533 | if (!reqPath.ends_with("/")) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 534 | { |
Brandon Kim | f6ebfc7 | 2022-07-07 12:53:44 -0700 | [diff] [blame] | 535 | reqPath += "/"; |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 536 | } |
Brandon Kim | f6ebfc7 | 2022-07-07 12:53:44 -0700 | [diff] [blame] | 537 | std::string_view reqPathStripped = |
| 538 | std::string_view(reqPath).substr(0, reqPath.size() - 1); |
| 539 | |
| 540 | if (!reqPathStripped.empty() && |
| 541 | interfaceMap.find(reqPathStripped) == interfaceMap.end()) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 542 | { |
Konstantin Aladyshev | b15df6b | 2022-01-11 14:50:55 +0300 | [diff] [blame] | 543 | throw sdbusplus::xyz::openbmc_project::Common::Error:: |
| 544 | ResourceNotFound(); |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 545 | } |
| 546 | |
Brandon Kim | 82720f0 | 2022-07-08 09:57:22 -0700 | [diff] [blame] | 547 | std::vector<InterfaceMapType::value_type> ret; |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame] | 548 | for (const auto& objectPath : interfaceMap) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 549 | { |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame] | 550 | const auto& thisPath = objectPath.first; |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 551 | |
Brandon Kim | f6ebfc7 | 2022-07-07 12:53:44 -0700 | [diff] [blame] | 552 | // Skip exact match on stripped search term |
| 553 | if (thisPath == reqPathStripped) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 554 | { |
| 555 | continue; |
| 556 | } |
| 557 | |
Brad Bishop | 86d2880 | 2022-07-11 15:49:31 -0400 | [diff] [blame] | 558 | if (thisPath.starts_with(reqPath)) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 559 | { |
Brandon Kim | f6ebfc7 | 2022-07-07 12:53:44 -0700 | [diff] [blame] | 560 | // count the number of slashes past the stripped search term |
| 561 | int32_t thisDepth = std::count( |
| 562 | thisPath.begin() + reqPathStripped.size(), thisPath.end(), '/'); |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 563 | if (thisDepth <= depth) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 564 | { |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame] | 565 | for (const auto& interfaceMap : objectPath.second) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 566 | { |
| 567 | if (intersect(interfaces.begin(), interfaces.end(), |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 568 | interfaceMap.second.begin(), |
| 569 | interfaceMap.second.end()) || |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 570 | interfaces.empty()) |
| 571 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 572 | addObjectMapResult(ret, thisPath, interfaceMap); |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 573 | } |
| 574 | } |
| 575 | } |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | return ret; |
| 580 | } |
| 581 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 582 | std::vector<std::string> getSubTreePaths(const InterfaceMapType& interfaceMap, |
| 583 | std::string reqPath, int32_t depth, |
| 584 | std::vector<std::string>& interfaces) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 585 | { |
| 586 | if (depth <= 0) |
| 587 | { |
| 588 | depth = std::numeric_limits<int32_t>::max(); |
| 589 | } |
| 590 | // Interfaces need to be sorted for intersect to function |
| 591 | std::sort(interfaces.begin(), interfaces.end()); |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 592 | |
Brandon Kim | f6ebfc7 | 2022-07-07 12:53:44 -0700 | [diff] [blame] | 593 | // reqPath is now guaranteed to have a trailing "/" while reqPathStripped |
| 594 | // will be guaranteed not to have a trailing "/" |
Brad Bishop | 86d2880 | 2022-07-11 15:49:31 -0400 | [diff] [blame] | 595 | if (!reqPath.ends_with("/")) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 596 | { |
Brandon Kim | f6ebfc7 | 2022-07-07 12:53:44 -0700 | [diff] [blame] | 597 | reqPath += "/"; |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 598 | } |
Brandon Kim | f6ebfc7 | 2022-07-07 12:53:44 -0700 | [diff] [blame] | 599 | std::string_view reqPathStripped = |
| 600 | std::string_view(reqPath).substr(0, reqPath.size() - 1); |
| 601 | |
| 602 | if (!reqPathStripped.empty() && |
| 603 | interfaceMap.find(reqPathStripped) == interfaceMap.end()) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 604 | { |
Konstantin Aladyshev | b15df6b | 2022-01-11 14:50:55 +0300 | [diff] [blame] | 605 | throw sdbusplus::xyz::openbmc_project::Common::Error:: |
| 606 | ResourceNotFound(); |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 607 | } |
| 608 | |
Brandon Kim | 82720f0 | 2022-07-08 09:57:22 -0700 | [diff] [blame] | 609 | std::vector<std::string> ret; |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame] | 610 | for (const auto& objectPath : interfaceMap) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 611 | { |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame] | 612 | const auto& thisPath = objectPath.first; |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 613 | |
Brandon Kim | f6ebfc7 | 2022-07-07 12:53:44 -0700 | [diff] [blame] | 614 | // Skip exact match on stripped search term |
| 615 | if (thisPath == reqPathStripped) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 616 | { |
| 617 | continue; |
| 618 | } |
| 619 | |
Brad Bishop | 86d2880 | 2022-07-11 15:49:31 -0400 | [diff] [blame] | 620 | if (thisPath.starts_with(reqPath)) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 621 | { |
Brandon Kim | f6ebfc7 | 2022-07-07 12:53:44 -0700 | [diff] [blame] | 622 | // count the number of slashes past the stripped search term |
| 623 | int thisDepth = std::count( |
| 624 | thisPath.begin() + reqPathStripped.size(), thisPath.end(), '/'); |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 625 | if (thisDepth <= depth) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 626 | { |
| 627 | bool add = interfaces.empty(); |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame] | 628 | for (const auto& interfaceMap : objectPath.second) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 629 | { |
| 630 | if (intersect(interfaces.begin(), interfaces.end(), |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 631 | interfaceMap.second.begin(), |
| 632 | interfaceMap.second.end())) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 633 | { |
| 634 | add = true; |
| 635 | break; |
| 636 | } |
| 637 | } |
| 638 | if (add) |
| 639 | { |
| 640 | // TODO(ed) this is a copy |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 641 | ret.emplace_back(thisPath); |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 642 | } |
| 643 | } |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | return ret; |
| 648 | } |
| 649 | |
Brad Bishop | 1e94e60 | 2022-06-02 19:47:53 -0400 | [diff] [blame] | 650 | int main() |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 651 | { |
Ed Tanous | 21c6059 | 2020-08-17 23:43:46 -0700 | [diff] [blame] | 652 | boost::asio::io_context io; |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 653 | std::shared_ptr<sdbusplus::asio::connection> systemBus = |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 654 | std::make_shared<sdbusplus::asio::connection>(io); |
| 655 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 656 | sdbusplus::asio::object_server server(systemBus); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 657 | |
| 658 | // Construct a signal set registered for process termination. |
| 659 | boost::asio::signal_set signals(io, SIGINT, SIGTERM); |
Brad Bishop | 2d41d6a | 2021-08-03 08:14:45 -0400 | [diff] [blame] | 660 | signals.async_wait( |
| 661 | [&io](const boost::system::error_code&, int) { io.stop(); }); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 662 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 663 | InterfaceMapType interfaceMap; |
| 664 | boost::container::flat_map<std::string, std::string> nameOwners; |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 665 | |
Patrick Williams | cc8070b | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 666 | std::function<void(sdbusplus::message_t & message)> nameChangeHandler = |
| 667 | [&interfaceMap, &io, &nameOwners, &server, |
| 668 | systemBus](sdbusplus::message_t& message) { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 669 | std::string name; // well-known |
| 670 | std::string oldOwner; // unique-name |
| 671 | std::string newOwner; // unique-name |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 672 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 673 | message.read(name, oldOwner, newOwner); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 674 | |
Patrick Williams | 4824820 | 2022-10-10 10:26:47 -0500 | [diff] [blame] | 675 | if (name.starts_with(':')) |
| 676 | { |
| 677 | // We should do nothing with unique-name connections. |
| 678 | return; |
| 679 | } |
| 680 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 681 | if (!oldOwner.empty()) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 682 | { |
Kallas, Pawel | 5b4357d | 2022-10-12 15:36:37 +0200 | [diff] [blame^] | 683 | processNameChangeDelete(io, nameOwners, name, oldOwner, |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 684 | interfaceMap, associationMaps, server); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 685 | } |
| 686 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 687 | if (!newOwner.empty()) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 688 | { |
Brad Bishop | d6aa552 | 2022-05-31 19:23:48 -0400 | [diff] [blame] | 689 | #ifdef MAPPER_ENABLE_DEBUG |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 690 | auto transaction = std::make_shared< |
| 691 | std::chrono::time_point<std::chrono::steady_clock>>( |
| 692 | std::chrono::steady_clock::now()); |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 693 | #endif |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 694 | // New daemon added |
Brad Bishop | 1e94e60 | 2022-06-02 19:47:53 -0400 | [diff] [blame] | 695 | if (needToIntrospect(name)) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 696 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 697 | nameOwners[newOwner] = name; |
| 698 | startNewIntrospect(systemBus.get(), io, interfaceMap, name, |
| 699 | associationMaps, |
Brad Bishop | d6aa552 | 2022-05-31 19:23:48 -0400 | [diff] [blame] | 700 | #ifdef MAPPER_ENABLE_DEBUG |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 701 | transaction, |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 702 | #endif |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 703 | server); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 704 | } |
| 705 | } |
| 706 | }; |
| 707 | |
Patrick Williams | cc8070b | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 708 | sdbusplus::bus::match_t nameOwnerChanged( |
| 709 | static_cast<sdbusplus::bus_t&>(*systemBus), |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 710 | sdbusplus::bus::match::rules::nameOwnerChanged(), nameChangeHandler); |
| 711 | |
Patrick Williams | cc8070b | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 712 | std::function<void(sdbusplus::message_t & message)> interfacesAddedHandler = |
Kallas, Pawel | 5b4357d | 2022-10-12 15:36:37 +0200 | [diff] [blame^] | 713 | [&io, &interfaceMap, &nameOwners, |
| 714 | &server](sdbusplus::message_t& message) { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 715 | sdbusplus::message::object_path objPath; |
| 716 | InterfacesAdded interfacesAdded; |
| 717 | message.read(objPath, interfacesAdded); |
| 718 | std::string wellKnown; |
| 719 | if (!getWellKnown(nameOwners, message.get_sender(), wellKnown)) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 720 | { |
| 721 | return; // only introspect well-known |
| 722 | } |
Brad Bishop | 1e94e60 | 2022-06-02 19:47:53 -0400 | [diff] [blame] | 723 | if (needToIntrospect(wellKnown)) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 724 | { |
Kallas, Pawel | 5b4357d | 2022-10-12 15:36:37 +0200 | [diff] [blame^] | 725 | processInterfaceAdded(io, interfaceMap, objPath, |
| 726 | interfacesAdded, wellKnown, |
| 727 | associationMaps, server); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 728 | } |
| 729 | }; |
| 730 | |
Patrick Williams | cc8070b | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 731 | sdbusplus::bus::match_t interfacesAdded( |
| 732 | static_cast<sdbusplus::bus_t&>(*systemBus), |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 733 | sdbusplus::bus::match::rules::interfacesAdded(), |
| 734 | interfacesAddedHandler); |
| 735 | |
Patrick Williams | cc8070b | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 736 | std::function<void(sdbusplus::message_t & message)> |
Kallas, Pawel | 5b4357d | 2022-10-12 15:36:37 +0200 | [diff] [blame^] | 737 | interfacesRemovedHandler = [&io, &interfaceMap, &nameOwners, |
Patrick Williams | cc8070b | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 738 | &server](sdbusplus::message_t& message) { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 739 | sdbusplus::message::object_path objPath; |
| 740 | std::vector<std::string> interfacesRemoved; |
| 741 | message.read(objPath, interfacesRemoved); |
| 742 | auto connectionMap = interfaceMap.find(objPath.str); |
| 743 | if (connectionMap == interfaceMap.end()) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 744 | { |
| 745 | return; |
| 746 | } |
| 747 | |
| 748 | std::string sender; |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 749 | if (!getWellKnown(nameOwners, message.get_sender(), sender)) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 750 | { |
| 751 | return; |
| 752 | } |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 753 | for (const std::string& interface : interfacesRemoved) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 754 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 755 | auto interfaceSet = connectionMap->second.find(sender); |
| 756 | if (interfaceSet == connectionMap->second.end()) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 757 | { |
| 758 | continue; |
| 759 | } |
| 760 | |
John Wang | d0cf942 | 2019-09-17 16:01:34 +0800 | [diff] [blame] | 761 | if (interface == assocDefsInterface) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 762 | { |
Kallas, Pawel | 5b4357d | 2022-10-12 15:36:37 +0200 | [diff] [blame^] | 763 | removeAssociation(io, objPath.str, sender, server, |
Matt Spinler | e2359fb | 2019-04-05 14:11:33 -0500 | [diff] [blame] | 764 | associationMaps); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 765 | } |
| 766 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 767 | interfaceSet->second.erase(interface); |
Matt Spinler | 9c3d285 | 2019-04-08 15:57:19 -0500 | [diff] [blame] | 768 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 769 | if (interfaceSet->second.empty()) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 770 | { |
Matt Spinler | 9c3d285 | 2019-04-08 15:57:19 -0500 | [diff] [blame] | 771 | // If this was the last interface on this connection, |
| 772 | // erase the connection |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 773 | connectionMap->second.erase(interfaceSet); |
Matt Spinler | 9c3d285 | 2019-04-08 15:57:19 -0500 | [diff] [blame] | 774 | |
| 775 | // Instead of checking if every single path is the endpoint |
| 776 | // of an association that needs to be moved to pending, |
| 777 | // only check when the only remaining owner of this path is |
| 778 | // ourself, which would be because we still own the |
| 779 | // association path. |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 780 | if ((connectionMap->second.size() == 1) && |
| 781 | (connectionMap->second.begin()->first == |
Brad Bishop | a02cd54 | 2021-10-12 19:12:42 -0400 | [diff] [blame] | 782 | "xyz.openbmc_project.ObjectMapper")) |
Matt Spinler | 9c3d285 | 2019-04-08 15:57:19 -0500 | [diff] [blame] | 783 | { |
| 784 | // Remove the 2 association D-Bus paths and move the |
| 785 | // association to pending. |
Kallas, Pawel | 5b4357d | 2022-10-12 15:36:37 +0200 | [diff] [blame^] | 786 | moveAssociationToPending(io, objPath.str, |
| 787 | associationMaps, server); |
Matt Spinler | 9c3d285 | 2019-04-08 15:57:19 -0500 | [diff] [blame] | 788 | } |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 789 | } |
| 790 | } |
| 791 | // If this was the last connection on this object path, |
| 792 | // erase the object path |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 793 | if (connectionMap->second.empty()) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 794 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 795 | interfaceMap.erase(connectionMap); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 796 | } |
Matt Spinler | a82779f | 2019-01-09 12:39:42 -0600 | [diff] [blame] | 797 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 798 | removeUnneededParents(objPath.str, sender, interfaceMap); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 799 | }; |
| 800 | |
Patrick Williams | cc8070b | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 801 | sdbusplus::bus::match_t interfacesRemoved( |
| 802 | static_cast<sdbusplus::bus_t&>(*systemBus), |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 803 | sdbusplus::bus::match::rules::interfacesRemoved(), |
| 804 | interfacesRemovedHandler); |
| 805 | |
Patrick Williams | cc8070b | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 806 | std::function<void(sdbusplus::message_t & message)> |
Kallas, Pawel | 5b4357d | 2022-10-12 15:36:37 +0200 | [diff] [blame^] | 807 | associationChangedHandler = [&io, &server, &nameOwners, &interfaceMap]( |
Patrick Williams | cc8070b | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 808 | sdbusplus::message_t& message) { |
Matt Spinler | 8f876a5 | 2019-04-15 13:22:50 -0500 | [diff] [blame] | 809 | std::string objectName; |
Patrick Williams | 2bb2d6b | 2020-05-13 17:59:02 -0500 | [diff] [blame] | 810 | boost::container::flat_map<std::string, |
| 811 | std::variant<std::vector<Association>>> |
Matt Spinler | 8f876a5 | 2019-04-15 13:22:50 -0500 | [diff] [blame] | 812 | values; |
| 813 | message.read(objectName, values); |
John Wang | d0cf942 | 2019-09-17 16:01:34 +0800 | [diff] [blame] | 814 | auto prop = values.find(assocDefsProperty); |
Matt Spinler | 8f876a5 | 2019-04-15 13:22:50 -0500 | [diff] [blame] | 815 | if (prop != values.end()) |
| 816 | { |
| 817 | std::vector<Association> associations = |
Patrick Williams | b05bc12 | 2020-05-13 12:21:00 -0500 | [diff] [blame] | 818 | std::get<std::vector<Association>>(prop->second); |
Matt Spinler | 8f876a5 | 2019-04-15 13:22:50 -0500 | [diff] [blame] | 819 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 820 | std::string wellKnown; |
| 821 | if (!getWellKnown(nameOwners, message.get_sender(), wellKnown)) |
Matt Spinler | 8f876a5 | 2019-04-15 13:22:50 -0500 | [diff] [blame] | 822 | { |
| 823 | return; |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 824 | } |
Kallas, Pawel | 5b4357d | 2022-10-12 15:36:37 +0200 | [diff] [blame^] | 825 | associationChanged(io, server, associations, message.get_path(), |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 826 | wellKnown, interfaceMap, associationMaps); |
Matt Spinler | 8f876a5 | 2019-04-15 13:22:50 -0500 | [diff] [blame] | 827 | } |
| 828 | }; |
Patrick Williams | cc8070b | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 829 | sdbusplus::bus::match_t assocChangedMatch( |
| 830 | static_cast<sdbusplus::bus_t&>(*systemBus), |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 831 | sdbusplus::bus::match::rules::interface( |
| 832 | "org.freedesktop.DBus.Properties") + |
| 833 | sdbusplus::bus::match::rules::member("PropertiesChanged") + |
Matt Spinler | 8f876a5 | 2019-04-15 13:22:50 -0500 | [diff] [blame] | 834 | sdbusplus::bus::match::rules::argN(0, assocDefsInterface), |
| 835 | associationChangedHandler); |
| 836 | |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 837 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface = |
Brad Bishop | a02cd54 | 2021-10-12 19:12:42 -0400 | [diff] [blame] | 838 | server.add_interface("/xyz/openbmc_project/object_mapper", |
| 839 | "xyz.openbmc_project.ObjectMapper"); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 840 | |
| 841 | iface->register_method( |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 842 | "GetAncestors", [&interfaceMap](std::string& reqPath, |
| 843 | std::vector<std::string>& interfaces) { |
| 844 | return getAncestors(interfaceMap, reqPath, interfaces); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 845 | }); |
| 846 | |
| 847 | iface->register_method( |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 848 | "GetObject", [&interfaceMap](const std::string& path, |
| 849 | std::vector<std::string>& interfaces) { |
| 850 | return getObject(interfaceMap, path, interfaces); |
| 851 | }); |
| 852 | |
| 853 | iface->register_method( |
| 854 | "GetSubTree", [&interfaceMap](std::string& reqPath, int32_t depth, |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 855 | std::vector<std::string>& interfaces) { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 856 | return getSubTree(interfaceMap, reqPath, depth, interfaces); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 857 | }); |
| 858 | |
| 859 | iface->register_method( |
| 860 | "GetSubTreePaths", |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 861 | [&interfaceMap](std::string& reqPath, int32_t depth, |
| 862 | std::vector<std::string>& interfaces) { |
| 863 | return getSubTreePaths(interfaceMap, reqPath, depth, interfaces); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 864 | }); |
| 865 | |
| 866 | iface->initialize(); |
| 867 | |
| 868 | io.post([&]() { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 869 | doListNames(io, interfaceMap, systemBus.get(), nameOwners, |
Matt Spinler | 11401e2 | 2019-04-08 13:13:25 -0500 | [diff] [blame] | 870 | associationMaps, server); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 871 | }); |
| 872 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 873 | systemBus->request_name("xyz.openbmc_project.ObjectMapper"); |
Vishwanatha Subbanna | 64354ef | 2020-08-21 03:35:26 -0500 | [diff] [blame] | 874 | |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 875 | io.run(); |
| 876 | } |