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