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 | dd94586 | 2018-09-07 12:41:05 -0500 | [diff] [blame] | 3 | #include "src/argument.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 | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 8 | #include <boost/algorithm/string/predicate.hpp> |
Ed Tanous | 21c6059 | 2020-08-17 23:43:46 -0700 | [diff] [blame] | 9 | #include <boost/asio/io_context.hpp> |
| 10 | #include <boost/asio/signal_set.hpp> |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 11 | #include <boost/container/flat_map.hpp> |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 12 | #include <sdbusplus/asio/connection.hpp> |
| 13 | #include <sdbusplus/asio/object_server.hpp> |
Konstantin Aladyshev | b15df6b | 2022-01-11 14:50:55 +0300 | [diff] [blame] | 14 | #include <xyz/openbmc_project/Common/error.hpp> |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 15 | |
Brad Bishop | 2352088 | 2022-05-26 21:39:53 -0400 | [diff] [blame] | 16 | #include <atomic> |
| 17 | #include <chrono> |
Brad Bishop | ea11946 | 2022-05-31 20:32:04 -0400 | [diff] [blame] | 18 | #include <exception> |
Brad Bishop | 2352088 | 2022-05-26 21:39:53 -0400 | [diff] [blame] | 19 | #include <iomanip> |
| 20 | #include <iostream> |
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 | f944a45 | 2022-05-05 15:06:46 -0400 | [diff] [blame] | 25 | static AllowDenyList serviceAllowList; |
| 26 | static AllowDenyList serviceDenyList; |
Matt Spinler | dd94586 | 2018-09-07 12:41:05 -0500 | [diff] [blame] | 27 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 28 | void updateOwners(sdbusplus::asio::connection* conn, |
| 29 | boost::container::flat_map<std::string, std::string>& owners, |
| 30 | const std::string& newObject) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 31 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 32 | if (boost::starts_with(newObject, ":")) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 33 | { |
| 34 | return; |
| 35 | } |
| 36 | conn->async_method_call( |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 37 | [&, newObject](const boost::system::error_code ec, |
| 38 | const std::string& nameOwner) { |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 39 | if (ec) |
| 40 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 41 | std::cerr << "Error getting owner of " << newObject << " : " |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 42 | << ec << "\n"; |
| 43 | return; |
| 44 | } |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 45 | owners[nameOwner] = newObject; |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 46 | }, |
| 47 | "org.freedesktop.DBus", "/", "org.freedesktop.DBus", "GetNameOwner", |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 48 | newObject); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 51 | void sendIntrospectionCompleteSignal(sdbusplus::asio::connection* systemBus, |
| 52 | const std::string& processName) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 53 | { |
| 54 | // TODO(ed) This signal doesn't get exposed properly in the |
| 55 | // introspect right now. Find out how to register signals in |
| 56 | // sdbusplus |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 57 | sdbusplus::message::message m = systemBus->new_signal( |
Brad Bishop | a02cd54 | 2021-10-12 19:12:42 -0400 | [diff] [blame] | 58 | "/xyz/openbmc_project/object_mapper", |
| 59 | "xyz.openbmc_project.ObjectMapper.Private", "IntrospectionComplete"); |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 60 | m.append(processName); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 61 | m.signal_send(); |
| 62 | } |
| 63 | |
| 64 | struct InProgressIntrospect |
| 65 | { |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame] | 66 | InProgressIntrospect() = delete; |
| 67 | InProgressIntrospect(const InProgressIntrospect&) = delete; |
| 68 | InProgressIntrospect(InProgressIntrospect&&) = delete; |
| 69 | InProgressIntrospect& operator=(const InProgressIntrospect&) = delete; |
| 70 | InProgressIntrospect& operator=(InProgressIntrospect&&) = delete; |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 71 | InProgressIntrospect( |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 72 | sdbusplus::asio::connection* systemBus, boost::asio::io_context& io, |
| 73 | const std::string& processName, AssociationMaps& am |
Brad Bishop | d6aa552 | 2022-05-31 19:23:48 -0400 | [diff] [blame^] | 74 | #ifdef MAPPER_ENABLE_DEBUG |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 75 | , |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 76 | std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>> |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 77 | globalStartTime |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 78 | #endif |
| 79 | ) : |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 80 | systemBus(systemBus), |
| 81 | io(io), processName(processName), assocMaps(am) |
Brad Bishop | d6aa552 | 2022-05-31 19:23:48 -0400 | [diff] [blame^] | 82 | #ifdef MAPPER_ENABLE_DEBUG |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 83 | , |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame] | 84 | globalStartTime(std::move(globalStartTime)), |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 85 | processStartTime(std::chrono::steady_clock::now()) |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 86 | #endif |
Brad Bishop | 2352088 | 2022-05-26 21:39:53 -0400 | [diff] [blame] | 87 | {} |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 88 | ~InProgressIntrospect() |
| 89 | { |
Brad Bishop | ea11946 | 2022-05-31 20:32:04 -0400 | [diff] [blame] | 90 | try |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 91 | { |
Brad Bishop | ea11946 | 2022-05-31 20:32:04 -0400 | [diff] [blame] | 92 | sendIntrospectionCompleteSignal(systemBus, processName); |
Brad Bishop | d6aa552 | 2022-05-31 19:23:48 -0400 | [diff] [blame^] | 93 | #ifdef MAPPER_ENABLE_DEBUG |
Brad Bishop | ea11946 | 2022-05-31 20:32:04 -0400 | [diff] [blame] | 94 | std::chrono::duration<float> diff = |
| 95 | std::chrono::steady_clock::now() - processStartTime; |
| 96 | std::cout << std::setw(50) << processName << " scan took " |
| 97 | << diff.count() << " seconds\n"; |
| 98 | |
| 99 | // If we're the last outstanding caller globally, calculate the |
| 100 | // time it took |
| 101 | if (globalStartTime != nullptr && globalStartTime.use_count() == 1) |
| 102 | { |
| 103 | diff = std::chrono::steady_clock::now() - *globalStartTime; |
| 104 | std::cout << "Total scan took " << diff.count() |
| 105 | << " seconds to complete\n"; |
| 106 | } |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 107 | #endif |
Brad Bishop | ea11946 | 2022-05-31 20:32:04 -0400 | [diff] [blame] | 108 | } |
| 109 | catch (const std::exception& e) |
| 110 | { |
| 111 | std::cerr |
| 112 | << "Terminating, unhandled exception while introspecting: " |
| 113 | << e.what() << "\n"; |
| 114 | std::terminate(); |
| 115 | } |
| 116 | catch (...) |
| 117 | { |
| 118 | std::cerr |
| 119 | << "Terminating, unhandled exception while introspecting\n"; |
| 120 | std::terminate(); |
| 121 | } |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 122 | } |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 123 | sdbusplus::asio::connection* systemBus; |
Ed Tanous | 21c6059 | 2020-08-17 23:43:46 -0700 | [diff] [blame] | 124 | boost::asio::io_context& io; |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 125 | std::string processName; |
Matt Spinler | 11401e2 | 2019-04-08 13:13:25 -0500 | [diff] [blame] | 126 | AssociationMaps& assocMaps; |
Brad Bishop | d6aa552 | 2022-05-31 19:23:48 -0400 | [diff] [blame^] | 127 | #ifdef MAPPER_ENABLE_DEBUG |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 128 | std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>> |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 129 | globalStartTime; |
| 130 | std::chrono::time_point<std::chrono::steady_clock> processStartTime; |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 131 | #endif |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 132 | }; |
| 133 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 134 | void doAssociations(sdbusplus::asio::connection* systemBus, |
| 135 | InterfaceMapType& interfaceMap, |
| 136 | sdbusplus::asio::object_server& objectServer, |
| 137 | const std::string& processName, const std::string& path, |
| 138 | int timeoutRetries = 0) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 139 | { |
Adrian Ambrożewicz | bb40bd3 | 2021-02-12 13:36:26 +0100 | [diff] [blame] | 140 | constexpr int maxTimeoutRetries = 3; |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 141 | systemBus->async_method_call( |
| 142 | [&objectServer, path, processName, &interfaceMap, systemBus, |
Adrian Ambrożewicz | bb40bd3 | 2021-02-12 13:36:26 +0100 | [diff] [blame] | 143 | timeoutRetries]( |
Matt Spinler | 937a232 | 2019-01-23 13:54:22 -0600 | [diff] [blame] | 144 | const boost::system::error_code ec, |
Patrick Williams | 2bb2d6b | 2020-05-13 17:59:02 -0500 | [diff] [blame] | 145 | const std::variant<std::vector<Association>>& variantAssociations) { |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 146 | if (ec) |
| 147 | { |
Adrian Ambrożewicz | bb40bd3 | 2021-02-12 13:36:26 +0100 | [diff] [blame] | 148 | if (ec.value() == boost::system::errc::timed_out && |
| 149 | timeoutRetries < maxTimeoutRetries) |
| 150 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 151 | doAssociations(systemBus, interfaceMap, objectServer, |
| 152 | processName, path, timeoutRetries + 1); |
Adrian Ambrożewicz | bb40bd3 | 2021-02-12 13:36:26 +0100 | [diff] [blame] | 153 | return; |
| 154 | } |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 155 | std::cerr << "Error getting associations from " << path << "\n"; |
| 156 | } |
| 157 | std::vector<Association> associations = |
Patrick Williams | b05bc12 | 2020-05-13 12:21:00 -0500 | [diff] [blame] | 158 | std::get<std::vector<Association>>(variantAssociations); |
Andrew Geissler | 4511b33 | 2019-02-21 15:40:40 -0600 | [diff] [blame] | 159 | associationChanged(objectServer, associations, path, processName, |
Matt Spinler | e0b0e3a | 2019-04-08 10:39:23 -0500 | [diff] [blame] | 160 | interfaceMap, associationMaps); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 161 | }, |
| 162 | processName, path, "org.freedesktop.DBus.Properties", "Get", |
John Wang | d0cf942 | 2019-09-17 16:01:34 +0800 | [diff] [blame] | 163 | assocDefsInterface, assocDefsProperty); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 166 | void doIntrospect(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( |
| 174 | [&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 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 182 | doIntrospect(systemBus, transaction, interfaceMap, |
| 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 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 223 | doAssociations(systemBus, interfaceMap, objectServer, |
| 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. |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 232 | checkIfPendingAssociation(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 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 247 | doIntrospect(systemBus, transaction, interfaceMap, |
| 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 | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 267 | if (needToIntrospect(processName, serviceAllowList, serviceDenyList)) |
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 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 278 | doIntrospect(systemBus, transaction, interfaceMap, objectServer, "/"); |
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 | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 329 | if (needToIntrospect(processName, serviceAllowList, |
Brad Bishop | f944a45 | 2022-05-05 15:06:46 -0400 | [diff] [blame] | 330 | serviceDenyList)) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 331 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 332 | startNewIntrospect(systemBus, io, interfaceMap, processName, |
| 333 | assocMaps, |
Brad Bishop | d6aa552 | 2022-05-31 19:23:48 -0400 | [diff] [blame^] | 334 | #ifdef MAPPER_ENABLE_DEBUG |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 335 | globalStartTime, |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 336 | #endif |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 337 | objectServer); |
| 338 | updateOwners(systemBus, nameOwners, processName); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | }, |
| 342 | "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", |
| 343 | "ListNames"); |
| 344 | } |
| 345 | |
Matt Spinler | dd94586 | 2018-09-07 12:41:05 -0500 | [diff] [blame] | 346 | void splitArgs(const std::string& stringArgs, |
| 347 | boost::container::flat_set<std::string>& listArgs) |
| 348 | { |
| 349 | std::istringstream args; |
| 350 | std::string arg; |
| 351 | |
| 352 | args.str(stringArgs); |
| 353 | |
| 354 | while (!args.eof()) |
| 355 | { |
| 356 | args >> arg; |
| 357 | if (!arg.empty()) |
| 358 | { |
| 359 | listArgs.insert(arg); |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | |
Matt Spinler | 47c0975 | 2018-11-29 14:54:13 -0600 | [diff] [blame] | 364 | void addObjectMapResult( |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 365 | std::vector<InterfaceMapType::value_type>& objectMap, |
Matt Spinler | 9f0958e | 2018-09-11 08:26:10 -0500 | [diff] [blame] | 366 | const std::string& objectPath, |
| 367 | const std::pair<std::string, boost::container::flat_set<std::string>>& |
| 368 | interfaceMap) |
| 369 | { |
| 370 | // Adds an object path/service name/interface list entry to |
Matt Spinler | 47c0975 | 2018-11-29 14:54:13 -0600 | [diff] [blame] | 371 | // the results of GetSubTree and GetAncestors. |
Matt Spinler | 9f0958e | 2018-09-11 08:26:10 -0500 | [diff] [blame] | 372 | // If an entry for the object path already exists, just add the |
| 373 | // service name and interfaces to that entry, otherwise create |
| 374 | // a new entry. |
| 375 | auto entry = std::find_if( |
Matt Spinler | 47c0975 | 2018-11-29 14:54:13 -0600 | [diff] [blame] | 376 | objectMap.begin(), objectMap.end(), |
Matt Spinler | 9f0958e | 2018-09-11 08:26:10 -0500 | [diff] [blame] | 377 | [&objectPath](const auto& i) { return objectPath == i.first; }); |
| 378 | |
Matt Spinler | 47c0975 | 2018-11-29 14:54:13 -0600 | [diff] [blame] | 379 | if (entry != objectMap.end()) |
Matt Spinler | 9f0958e | 2018-09-11 08:26:10 -0500 | [diff] [blame] | 380 | { |
| 381 | entry->second.emplace(interfaceMap); |
| 382 | } |
| 383 | else |
| 384 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 385 | InterfaceMapType::value_type object; |
Matt Spinler | 9f0958e | 2018-09-11 08:26:10 -0500 | [diff] [blame] | 386 | object.first = objectPath; |
| 387 | object.second.emplace(interfaceMap); |
Matt Spinler | 47c0975 | 2018-11-29 14:54:13 -0600 | [diff] [blame] | 388 | objectMap.push_back(object); |
Matt Spinler | 9f0958e | 2018-09-11 08:26:10 -0500 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | |
Matt Spinler | a82779f | 2019-01-09 12:39:42 -0600 | [diff] [blame] | 392 | // Remove parents of the passed in path that: |
| 393 | // 1) Only have the 3 default interfaces on them |
| 394 | // - Means D-Bus created these, not application code, |
| 395 | // with the Properties, Introspectable, and Peer ifaces |
| 396 | // 2) Have no other child for this owner |
| 397 | void removeUnneededParents(const std::string& objectPath, |
| 398 | const std::string& owner, |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 399 | InterfaceMapType& interfaceMap) |
Matt Spinler | a82779f | 2019-01-09 12:39:42 -0600 | [diff] [blame] | 400 | { |
| 401 | auto parent = objectPath; |
| 402 | |
| 403 | while (true) |
| 404 | { |
| 405 | auto pos = parent.find_last_of('/'); |
| 406 | if ((pos == std::string::npos) || (pos == 0)) |
| 407 | { |
| 408 | break; |
| 409 | } |
| 410 | parent = parent.substr(0, pos); |
| 411 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 412 | auto parentIt = interfaceMap.find(parent); |
| 413 | if (parentIt == interfaceMap.end()) |
Matt Spinler | a82779f | 2019-01-09 12:39:42 -0600 | [diff] [blame] | 414 | { |
| 415 | break; |
| 416 | } |
| 417 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 418 | auto ifacesIt = parentIt->second.find(owner); |
| 419 | if (ifacesIt == parentIt->second.end()) |
Matt Spinler | a82779f | 2019-01-09 12:39:42 -0600 | [diff] [blame] | 420 | { |
| 421 | break; |
| 422 | } |
| 423 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 424 | if (ifacesIt->second.size() != 3) |
Matt Spinler | a82779f | 2019-01-09 12:39:42 -0600 | [diff] [blame] | 425 | { |
| 426 | break; |
| 427 | } |
| 428 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 429 | auto childPath = parent + '/'; |
Matt Spinler | a82779f | 2019-01-09 12:39:42 -0600 | [diff] [blame] | 430 | |
| 431 | // Remove this parent if there isn't a remaining child on this owner |
| 432 | auto child = std::find_if( |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 433 | interfaceMap.begin(), interfaceMap.end(), |
| 434 | [&owner, &childPath](const auto& entry) { |
| 435 | return boost::starts_with(entry.first, childPath) && |
Matt Spinler | a82779f | 2019-01-09 12:39:42 -0600 | [diff] [blame] | 436 | (entry.second.find(owner) != entry.second.end()); |
| 437 | }); |
| 438 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 439 | if (child == interfaceMap.end()) |
Matt Spinler | a82779f | 2019-01-09 12:39:42 -0600 | [diff] [blame] | 440 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 441 | parentIt->second.erase(ifacesIt); |
| 442 | if (parentIt->second.empty()) |
Matt Spinler | a82779f | 2019-01-09 12:39:42 -0600 | [diff] [blame] | 443 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 444 | interfaceMap.erase(parentIt); |
Matt Spinler | a82779f | 2019-01-09 12:39:42 -0600 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | else |
| 448 | { |
| 449 | break; |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 454 | std::vector<InterfaceMapType::value_type> |
| 455 | getAncestors(const InterfaceMapType& interfaceMap, std::string reqPath, |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 456 | std::vector<std::string>& interfaces) |
| 457 | { |
| 458 | // Interfaces need to be sorted for intersect to function |
| 459 | std::sort(interfaces.begin(), interfaces.end()); |
| 460 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 461 | if (boost::ends_with(reqPath, "/")) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 462 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 463 | reqPath.pop_back(); |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 464 | } |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame] | 465 | if (!reqPath.empty() && interfaceMap.find(reqPath) == interfaceMap.end()) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 466 | { |
Konstantin Aladyshev | b15df6b | 2022-01-11 14:50:55 +0300 | [diff] [blame] | 467 | throw sdbusplus::xyz::openbmc_project::Common::Error:: |
| 468 | ResourceNotFound(); |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 469 | } |
| 470 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 471 | std::vector<InterfaceMapType::value_type> ret; |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame] | 472 | for (const auto& objectPath : interfaceMap) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 473 | { |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame] | 474 | const auto& thisPath = objectPath.first; |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 475 | if (boost::starts_with(reqPath, thisPath) && (reqPath != thisPath)) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 476 | { |
| 477 | if (interfaces.empty()) |
| 478 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 479 | ret.emplace_back(objectPath); |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 480 | } |
| 481 | else |
| 482 | { |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame] | 483 | for (const auto& interfaceMap : objectPath.second) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 484 | { |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 485 | if (intersect(interfaces.begin(), interfaces.end(), |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 486 | interfaceMap.second.begin(), |
| 487 | interfaceMap.second.end())) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 488 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 489 | addObjectMapResult(ret, thisPath, interfaceMap); |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 490 | } |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | return ret; |
| 497 | } |
| 498 | |
| 499 | boost::container::flat_map<std::string, boost::container::flat_set<std::string>> |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 500 | getObject(const InterfaceMapType& interfaceMap, const std::string& path, |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 501 | std::vector<std::string>& interfaces) |
| 502 | { |
| 503 | boost::container::flat_map<std::string, |
| 504 | boost::container::flat_set<std::string>> |
| 505 | results; |
| 506 | |
| 507 | // Interfaces need to be sorted for intersect to function |
| 508 | std::sort(interfaces.begin(), interfaces.end()); |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 509 | auto pathRef = interfaceMap.find(path); |
| 510 | if (pathRef == interfaceMap.end()) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 511 | { |
Konstantin Aladyshev | b15df6b | 2022-01-11 14:50:55 +0300 | [diff] [blame] | 512 | throw sdbusplus::xyz::openbmc_project::Common::Error:: |
| 513 | ResourceNotFound(); |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 514 | } |
| 515 | if (interfaces.empty()) |
| 516 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 517 | return pathRef->second; |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 518 | } |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame] | 519 | for (const auto& interfaceMap : pathRef->second) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 520 | { |
| 521 | if (intersect(interfaces.begin(), interfaces.end(), |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 522 | interfaceMap.second.begin(), interfaceMap.second.end())) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 523 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 524 | results.emplace(interfaceMap.first, interfaceMap.second); |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 525 | } |
| 526 | } |
| 527 | |
| 528 | if (results.empty()) |
| 529 | { |
Konstantin Aladyshev | b15df6b | 2022-01-11 14:50:55 +0300 | [diff] [blame] | 530 | throw sdbusplus::xyz::openbmc_project::Common::Error:: |
| 531 | ResourceNotFound(); |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | return results; |
| 535 | } |
| 536 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 537 | std::vector<InterfaceMapType::value_type> |
| 538 | getSubTree(const InterfaceMapType& interfaceMap, std::string reqPath, |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 539 | int32_t depth, std::vector<std::string>& interfaces) |
| 540 | { |
| 541 | if (depth <= 0) |
| 542 | { |
| 543 | depth = std::numeric_limits<int32_t>::max(); |
| 544 | } |
| 545 | // Interfaces need to be sorted for intersect to function |
| 546 | std::sort(interfaces.begin(), interfaces.end()); |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 547 | std::vector<InterfaceMapType::value_type> ret; |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 548 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 549 | if (boost::ends_with(reqPath, "/")) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 550 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 551 | reqPath.pop_back(); |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 552 | } |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame] | 553 | if (!reqPath.empty() && interfaceMap.find(reqPath) == interfaceMap.end()) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 554 | { |
Konstantin Aladyshev | b15df6b | 2022-01-11 14:50:55 +0300 | [diff] [blame] | 555 | throw sdbusplus::xyz::openbmc_project::Common::Error:: |
| 556 | ResourceNotFound(); |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 557 | } |
| 558 | |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame] | 559 | for (const auto& objectPath : interfaceMap) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 560 | { |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame] | 561 | const auto& thisPath = objectPath.first; |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 562 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 563 | if (thisPath == reqPath) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 564 | { |
| 565 | continue; |
| 566 | } |
| 567 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 568 | if (boost::starts_with(thisPath, reqPath)) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 569 | { |
| 570 | // count the number of slashes past the search term |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 571 | int32_t thisDepth = std::count(thisPath.begin() + reqPath.size(), |
| 572 | thisPath.end(), '/'); |
| 573 | if (thisDepth <= depth) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 574 | { |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame] | 575 | for (const auto& interfaceMap : objectPath.second) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 576 | { |
| 577 | if (intersect(interfaces.begin(), interfaces.end(), |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 578 | interfaceMap.second.begin(), |
| 579 | interfaceMap.second.end()) || |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 580 | interfaces.empty()) |
| 581 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 582 | addObjectMapResult(ret, thisPath, interfaceMap); |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 583 | } |
| 584 | } |
| 585 | } |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | return ret; |
| 590 | } |
| 591 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 592 | std::vector<std::string> getSubTreePaths(const InterfaceMapType& interfaceMap, |
| 593 | std::string reqPath, int32_t depth, |
| 594 | std::vector<std::string>& interfaces) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 595 | { |
| 596 | if (depth <= 0) |
| 597 | { |
| 598 | depth = std::numeric_limits<int32_t>::max(); |
| 599 | } |
| 600 | // Interfaces need to be sorted for intersect to function |
| 601 | std::sort(interfaces.begin(), interfaces.end()); |
| 602 | std::vector<std::string> ret; |
| 603 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 604 | if (boost::ends_with(reqPath, "/")) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 605 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 606 | reqPath.pop_back(); |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 607 | } |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame] | 608 | if (!reqPath.empty() && interfaceMap.find(reqPath) == interfaceMap.end()) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 609 | { |
Konstantin Aladyshev | b15df6b | 2022-01-11 14:50:55 +0300 | [diff] [blame] | 610 | throw sdbusplus::xyz::openbmc_project::Common::Error:: |
| 611 | ResourceNotFound(); |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 612 | } |
| 613 | |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame] | 614 | for (const auto& objectPath : interfaceMap) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 615 | { |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame] | 616 | const auto& thisPath = objectPath.first; |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 617 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 618 | if (thisPath == reqPath) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 619 | { |
| 620 | continue; |
| 621 | } |
| 622 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 623 | if (boost::starts_with(thisPath, reqPath)) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 624 | { |
| 625 | // count the number of slashes past the search term |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 626 | int thisDepth = std::count(thisPath.begin() + reqPath.size(), |
| 627 | thisPath.end(), '/'); |
| 628 | if (thisDepth <= depth) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 629 | { |
| 630 | bool add = interfaces.empty(); |
Brad Bishop | 1f62380 | 2022-05-31 18:22:10 -0400 | [diff] [blame] | 631 | for (const auto& interfaceMap : objectPath.second) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 632 | { |
| 633 | if (intersect(interfaces.begin(), interfaces.end(), |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 634 | interfaceMap.second.begin(), |
| 635 | interfaceMap.second.end())) |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 636 | { |
| 637 | add = true; |
| 638 | break; |
| 639 | } |
| 640 | } |
| 641 | if (add) |
| 642 | { |
| 643 | // TODO(ed) this is a copy |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 644 | ret.emplace_back(thisPath); |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 645 | } |
| 646 | } |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | return ret; |
| 651 | } |
| 652 | |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 653 | int main(int argc, char** argv) |
| 654 | { |
Matt Spinler | dd94586 | 2018-09-07 12:41:05 -0500 | [diff] [blame] | 655 | auto options = ArgumentParser(argc, argv); |
Ed Tanous | 21c6059 | 2020-08-17 23:43:46 -0700 | [diff] [blame] | 656 | boost::asio::io_context io; |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 657 | std::shared_ptr<sdbusplus::asio::connection> systemBus = |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 658 | std::make_shared<sdbusplus::asio::connection>(io); |
| 659 | |
Brad Bishop | f944a45 | 2022-05-05 15:06:46 -0400 | [diff] [blame] | 660 | splitArgs(options["service-namespaces"], serviceAllowList); |
| 661 | splitArgs(options["service-blacklists"], serviceDenyList); |
Matt Spinler | dd94586 | 2018-09-07 12:41:05 -0500 | [diff] [blame] | 662 | |
Ed Tanous | d4dd96a | 2018-11-12 11:37:44 -0800 | [diff] [blame] | 663 | // TODO(Ed) Remove this once all service files are updated to not use this. |
| 664 | // For now, simply squash the input, and ignore it. |
Brad Bishop | f944a45 | 2022-05-05 15:06:46 -0400 | [diff] [blame] | 665 | boost::container::flat_set<std::string> ifaceAllowlist; |
| 666 | splitArgs(options["interface-namespaces"], ifaceAllowlist); |
Ed Tanous | d4dd96a | 2018-11-12 11:37:44 -0800 | [diff] [blame] | 667 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 668 | sdbusplus::asio::object_server server(systemBus); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 669 | |
| 670 | // Construct a signal set registered for process termination. |
| 671 | boost::asio::signal_set signals(io, SIGINT, SIGTERM); |
Brad Bishop | 2d41d6a | 2021-08-03 08:14:45 -0400 | [diff] [blame] | 672 | signals.async_wait( |
| 673 | [&io](const boost::system::error_code&, int) { io.stop(); }); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 674 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 675 | InterfaceMapType interfaceMap; |
| 676 | boost::container::flat_map<std::string, std::string> nameOwners; |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 677 | |
| 678 | std::function<void(sdbusplus::message::message & message)> |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 679 | nameChangeHandler = [&interfaceMap, &io, &nameOwners, &server, |
| 680 | systemBus](sdbusplus::message::message& message) { |
| 681 | std::string name; // well-known |
| 682 | std::string oldOwner; // unique-name |
| 683 | std::string newOwner; // unique-name |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 684 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 685 | message.read(name, oldOwner, newOwner); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 686 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 687 | if (!oldOwner.empty()) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 688 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 689 | processNameChangeDelete(nameOwners, name, oldOwner, |
| 690 | interfaceMap, associationMaps, server); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 691 | } |
| 692 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 693 | if (!newOwner.empty()) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 694 | { |
Brad Bishop | d6aa552 | 2022-05-31 19:23:48 -0400 | [diff] [blame^] | 695 | #ifdef MAPPER_ENABLE_DEBUG |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 696 | auto transaction = std::make_shared< |
| 697 | std::chrono::time_point<std::chrono::steady_clock>>( |
| 698 | std::chrono::steady_clock::now()); |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 699 | #endif |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 700 | // New daemon added |
Brad Bishop | f944a45 | 2022-05-05 15:06:46 -0400 | [diff] [blame] | 701 | if (needToIntrospect(name, serviceAllowList, serviceDenyList)) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 702 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 703 | nameOwners[newOwner] = name; |
| 704 | startNewIntrospect(systemBus.get(), io, interfaceMap, name, |
| 705 | associationMaps, |
Brad Bishop | d6aa552 | 2022-05-31 19:23:48 -0400 | [diff] [blame^] | 706 | #ifdef MAPPER_ENABLE_DEBUG |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 707 | transaction, |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 708 | #endif |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 709 | server); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 710 | } |
| 711 | } |
| 712 | }; |
| 713 | |
| 714 | sdbusplus::bus::match::match nameOwnerChanged( |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 715 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 716 | sdbusplus::bus::match::rules::nameOwnerChanged(), nameChangeHandler); |
| 717 | |
| 718 | std::function<void(sdbusplus::message::message & message)> |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 719 | interfacesAddedHandler = [&interfaceMap, &nameOwners, &server]( |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 720 | sdbusplus::message::message& message) { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 721 | sdbusplus::message::object_path objPath; |
| 722 | InterfacesAdded interfacesAdded; |
| 723 | message.read(objPath, interfacesAdded); |
| 724 | std::string wellKnown; |
| 725 | if (!getWellKnown(nameOwners, message.get_sender(), wellKnown)) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 726 | { |
| 727 | return; // only introspect well-known |
| 728 | } |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 729 | if (needToIntrospect(wellKnown, serviceAllowList, serviceDenyList)) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 730 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 731 | processInterfaceAdded(interfaceMap, objPath, interfacesAdded, |
| 732 | wellKnown, associationMaps, server); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 733 | } |
| 734 | }; |
| 735 | |
| 736 | sdbusplus::bus::match::match interfacesAdded( |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 737 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 738 | sdbusplus::bus::match::rules::interfacesAdded(), |
| 739 | interfacesAddedHandler); |
| 740 | |
| 741 | std::function<void(sdbusplus::message::message & message)> |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 742 | interfacesRemovedHandler = [&interfaceMap, &nameOwners, &server]( |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 743 | sdbusplus::message::message& message) { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 744 | sdbusplus::message::object_path objPath; |
| 745 | std::vector<std::string> interfacesRemoved; |
| 746 | message.read(objPath, interfacesRemoved); |
| 747 | auto connectionMap = interfaceMap.find(objPath.str); |
| 748 | if (connectionMap == interfaceMap.end()) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 749 | { |
| 750 | return; |
| 751 | } |
| 752 | |
| 753 | std::string sender; |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 754 | if (!getWellKnown(nameOwners, message.get_sender(), sender)) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 755 | { |
| 756 | return; |
| 757 | } |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 758 | for (const std::string& interface : interfacesRemoved) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 759 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 760 | auto interfaceSet = connectionMap->second.find(sender); |
| 761 | if (interfaceSet == connectionMap->second.end()) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 762 | { |
| 763 | continue; |
| 764 | } |
| 765 | |
John Wang | d0cf942 | 2019-09-17 16:01:34 +0800 | [diff] [blame] | 766 | if (interface == assocDefsInterface) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 767 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 768 | removeAssociation(objPath.str, sender, server, |
Matt Spinler | e2359fb | 2019-04-05 14:11:33 -0500 | [diff] [blame] | 769 | associationMaps); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 770 | } |
| 771 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 772 | interfaceSet->second.erase(interface); |
Matt Spinler | 9c3d285 | 2019-04-08 15:57:19 -0500 | [diff] [blame] | 773 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 774 | if (interfaceSet->second.empty()) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 775 | { |
Matt Spinler | 9c3d285 | 2019-04-08 15:57:19 -0500 | [diff] [blame] | 776 | // If this was the last interface on this connection, |
| 777 | // erase the connection |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 778 | connectionMap->second.erase(interfaceSet); |
Matt Spinler | 9c3d285 | 2019-04-08 15:57:19 -0500 | [diff] [blame] | 779 | |
| 780 | // Instead of checking if every single path is the endpoint |
| 781 | // of an association that needs to be moved to pending, |
| 782 | // only check when the only remaining owner of this path is |
| 783 | // ourself, which would be because we still own the |
| 784 | // association path. |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 785 | if ((connectionMap->second.size() == 1) && |
| 786 | (connectionMap->second.begin()->first == |
Brad Bishop | a02cd54 | 2021-10-12 19:12:42 -0400 | [diff] [blame] | 787 | "xyz.openbmc_project.ObjectMapper")) |
Matt Spinler | 9c3d285 | 2019-04-08 15:57:19 -0500 | [diff] [blame] | 788 | { |
| 789 | // Remove the 2 association D-Bus paths and move the |
| 790 | // association to pending. |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 791 | moveAssociationToPending(objPath.str, associationMaps, |
Matt Spinler | 9c3d285 | 2019-04-08 15:57:19 -0500 | [diff] [blame] | 792 | server); |
| 793 | } |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 794 | } |
| 795 | } |
| 796 | // If this was the last connection on this object path, |
| 797 | // erase the object path |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 798 | if (connectionMap->second.empty()) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 799 | { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 800 | interfaceMap.erase(connectionMap); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 801 | } |
Matt Spinler | a82779f | 2019-01-09 12:39:42 -0600 | [diff] [blame] | 802 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 803 | removeUnneededParents(objPath.str, sender, interfaceMap); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 804 | }; |
| 805 | |
| 806 | sdbusplus::bus::match::match interfacesRemoved( |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 807 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 808 | sdbusplus::bus::match::rules::interfacesRemoved(), |
| 809 | interfacesRemovedHandler); |
| 810 | |
| 811 | std::function<void(sdbusplus::message::message & message)> |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 812 | associationChangedHandler = [&server, &nameOwners, &interfaceMap]( |
Matt Spinler | 8f876a5 | 2019-04-15 13:22:50 -0500 | [diff] [blame] | 813 | sdbusplus::message::message& message) { |
| 814 | std::string objectName; |
Patrick Williams | 2bb2d6b | 2020-05-13 17:59:02 -0500 | [diff] [blame] | 815 | boost::container::flat_map<std::string, |
| 816 | std::variant<std::vector<Association>>> |
Matt Spinler | 8f876a5 | 2019-04-15 13:22:50 -0500 | [diff] [blame] | 817 | values; |
| 818 | message.read(objectName, values); |
John Wang | d0cf942 | 2019-09-17 16:01:34 +0800 | [diff] [blame] | 819 | auto prop = values.find(assocDefsProperty); |
Matt Spinler | 8f876a5 | 2019-04-15 13:22:50 -0500 | [diff] [blame] | 820 | if (prop != values.end()) |
| 821 | { |
| 822 | std::vector<Association> associations = |
Patrick Williams | b05bc12 | 2020-05-13 12:21:00 -0500 | [diff] [blame] | 823 | std::get<std::vector<Association>>(prop->second); |
Matt Spinler | 8f876a5 | 2019-04-15 13:22:50 -0500 | [diff] [blame] | 824 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 825 | std::string wellKnown; |
| 826 | if (!getWellKnown(nameOwners, message.get_sender(), wellKnown)) |
Matt Spinler | 8f876a5 | 2019-04-15 13:22:50 -0500 | [diff] [blame] | 827 | { |
| 828 | return; |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 829 | } |
Matt Spinler | 8f876a5 | 2019-04-15 13:22:50 -0500 | [diff] [blame] | 830 | associationChanged(server, associations, message.get_path(), |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 831 | wellKnown, interfaceMap, associationMaps); |
Matt Spinler | 8f876a5 | 2019-04-15 13:22:50 -0500 | [diff] [blame] | 832 | } |
| 833 | }; |
| 834 | sdbusplus::bus::match::match assocChangedMatch( |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 835 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 836 | sdbusplus::bus::match::rules::interface( |
| 837 | "org.freedesktop.DBus.Properties") + |
| 838 | sdbusplus::bus::match::rules::member("PropertiesChanged") + |
Matt Spinler | 8f876a5 | 2019-04-15 13:22:50 -0500 | [diff] [blame] | 839 | sdbusplus::bus::match::rules::argN(0, assocDefsInterface), |
| 840 | associationChangedHandler); |
| 841 | |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 842 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface = |
Brad Bishop | a02cd54 | 2021-10-12 19:12:42 -0400 | [diff] [blame] | 843 | server.add_interface("/xyz/openbmc_project/object_mapper", |
| 844 | "xyz.openbmc_project.ObjectMapper"); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 845 | |
| 846 | iface->register_method( |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 847 | "GetAncestors", [&interfaceMap](std::string& reqPath, |
| 848 | std::vector<std::string>& interfaces) { |
| 849 | return getAncestors(interfaceMap, reqPath, interfaces); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 850 | }); |
| 851 | |
| 852 | iface->register_method( |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 853 | "GetObject", [&interfaceMap](const std::string& path, |
| 854 | std::vector<std::string>& interfaces) { |
| 855 | return getObject(interfaceMap, path, interfaces); |
| 856 | }); |
| 857 | |
| 858 | iface->register_method( |
| 859 | "GetSubTree", [&interfaceMap](std::string& reqPath, int32_t depth, |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 860 | std::vector<std::string>& interfaces) { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 861 | return getSubTree(interfaceMap, reqPath, depth, interfaces); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 862 | }); |
| 863 | |
| 864 | iface->register_method( |
| 865 | "GetSubTreePaths", |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 866 | [&interfaceMap](std::string& reqPath, int32_t depth, |
| 867 | std::vector<std::string>& interfaces) { |
| 868 | return getSubTreePaths(interfaceMap, reqPath, depth, interfaces); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 869 | }); |
| 870 | |
| 871 | iface->initialize(); |
| 872 | |
| 873 | io.post([&]() { |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 874 | doListNames(io, interfaceMap, systemBus.get(), nameOwners, |
Matt Spinler | 11401e2 | 2019-04-08 13:13:25 -0500 | [diff] [blame] | 875 | associationMaps, server); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 876 | }); |
| 877 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 878 | systemBus->request_name("xyz.openbmc_project.ObjectMapper"); |
Vishwanatha Subbanna | 64354ef | 2020-08-21 03:35:26 -0500 | [diff] [blame] | 879 | |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 880 | io.run(); |
| 881 | } |