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 | |
| 8 | #include <atomic> |
| 9 | #include <boost/algorithm/string/predicate.hpp> |
Ed Tanous | 21c6059 | 2020-08-17 23:43:46 -0700 | [diff] [blame] | 10 | #include <boost/asio/io_context.hpp> |
| 11 | #include <boost/asio/signal_set.hpp> |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 12 | #include <boost/container/flat_map.hpp> |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 13 | #include <chrono> |
| 14 | #include <iomanip> |
| 15 | #include <iostream> |
| 16 | #include <sdbusplus/asio/connection.hpp> |
| 17 | #include <sdbusplus/asio/object_server.hpp> |
| 18 | |
Matt Spinler | e2359fb | 2019-04-05 14:11:33 -0500 | [diff] [blame] | 19 | AssociationMaps associationMaps; |
Matt Spinler | 937a232 | 2019-01-23 13:54:22 -0600 | [diff] [blame] | 20 | |
Andrew Geissler | 82815da | 2019-02-04 12:19:41 -0600 | [diff] [blame] | 21 | static WhiteBlackList service_whitelist; |
| 22 | static WhiteBlackList service_blacklist; |
Matt Spinler | dd94586 | 2018-09-07 12:41:05 -0500 | [diff] [blame] | 23 | |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 24 | /** Exception thrown when a path is not found in the object list. */ |
| 25 | struct NotFoundException final : public sdbusplus::exception_t |
| 26 | { |
| 27 | const char* name() const noexcept override |
| 28 | { |
Patrick Williams | 3735ea2 | 2020-11-11 13:12:48 -0600 | [diff] [blame] | 29 | return "xyz.openbmc_project.Common.Error.ResourceNotFound"; |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 30 | }; |
| 31 | const char* description() const noexcept override |
| 32 | { |
| 33 | return "path or object not found"; |
| 34 | }; |
| 35 | const char* what() const noexcept override |
| 36 | { |
Patrick Williams | 3735ea2 | 2020-11-11 13:12:48 -0600 | [diff] [blame] | 37 | return "xyz.openbmc_project.Common.Error.ResourceNotFound: " |
| 38 | "The resource is not found."; |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 39 | }; |
Patrick Williams | bedd416 | 2021-09-08 13:54:13 -0500 | [diff] [blame] | 40 | |
| 41 | int get_errno() const noexcept override |
| 42 | { |
| 43 | return ENOENT; |
| 44 | } |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 47 | void update_owners(sdbusplus::asio::connection* conn, |
| 48 | boost::container::flat_map<std::string, std::string>& owners, |
| 49 | const std::string& new_object) |
| 50 | { |
| 51 | if (boost::starts_with(new_object, ":")) |
| 52 | { |
| 53 | return; |
| 54 | } |
| 55 | conn->async_method_call( |
| 56 | [&, new_object](const boost::system::error_code ec, |
| 57 | const std::string& nameOwner) { |
| 58 | if (ec) |
| 59 | { |
| 60 | std::cerr << "Error getting owner of " << new_object << " : " |
| 61 | << ec << "\n"; |
| 62 | return; |
| 63 | } |
| 64 | owners[nameOwner] = new_object; |
| 65 | }, |
| 66 | "org.freedesktop.DBus", "/", "org.freedesktop.DBus", "GetNameOwner", |
| 67 | new_object); |
| 68 | } |
| 69 | |
| 70 | void send_introspection_complete_signal(sdbusplus::asio::connection* system_bus, |
| 71 | const std::string& process_name) |
| 72 | { |
| 73 | // TODO(ed) This signal doesn't get exposed properly in the |
| 74 | // introspect right now. Find out how to register signals in |
| 75 | // sdbusplus |
| 76 | sdbusplus::message::message m = system_bus->new_signal( |
Brad Bishop | a02cd54 | 2021-10-12 19:12:42 -0400 | [diff] [blame] | 77 | "/xyz/openbmc_project/object_mapper", |
| 78 | "xyz.openbmc_project.ObjectMapper.Private", "IntrospectionComplete"); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 79 | m.append(process_name); |
| 80 | m.signal_send(); |
| 81 | } |
| 82 | |
| 83 | struct InProgressIntrospect |
| 84 | { |
| 85 | InProgressIntrospect( |
Ed Tanous | 21c6059 | 2020-08-17 23:43:46 -0700 | [diff] [blame] | 86 | sdbusplus::asio::connection* system_bus, boost::asio::io_context& io, |
Matt Spinler | 11401e2 | 2019-04-08 13:13:25 -0500 | [diff] [blame] | 87 | const std::string& process_name, AssociationMaps& am |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 88 | #ifdef DEBUG |
| 89 | , |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 90 | std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>> |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 91 | global_start_time |
| 92 | #endif |
| 93 | ) : |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 94 | system_bus(system_bus), |
Matt Spinler | 11401e2 | 2019-04-08 13:13:25 -0500 | [diff] [blame] | 95 | io(io), process_name(process_name), assocMaps(am) |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 96 | #ifdef DEBUG |
| 97 | , |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 98 | global_start_time(global_start_time), |
| 99 | process_start_time(std::chrono::steady_clock::now()) |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 100 | #endif |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 101 | { |
| 102 | } |
| 103 | ~InProgressIntrospect() |
| 104 | { |
| 105 | send_introspection_complete_signal(system_bus, process_name); |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 106 | |
| 107 | #ifdef DEBUG |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 108 | std::chrono::duration<float> diff = |
| 109 | std::chrono::steady_clock::now() - process_start_time; |
| 110 | std::cout << std::setw(50) << process_name << " scan took " |
| 111 | << diff.count() << " seconds\n"; |
| 112 | |
| 113 | // If we're the last outstanding caller globally, calculate the |
| 114 | // time it took |
| 115 | if (global_start_time != nullptr && global_start_time.use_count() == 1) |
| 116 | { |
| 117 | diff = std::chrono::steady_clock::now() - *global_start_time; |
| 118 | std::cout << "Total scan took " << diff.count() |
| 119 | << " seconds to complete\n"; |
| 120 | } |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 121 | #endif |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 122 | } |
| 123 | sdbusplus::asio::connection* system_bus; |
Ed Tanous | 21c6059 | 2020-08-17 23:43:46 -0700 | [diff] [blame] | 124 | boost::asio::io_context& io; |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 125 | std::string process_name; |
Matt Spinler | 11401e2 | 2019-04-08 13:13:25 -0500 | [diff] [blame] | 126 | AssociationMaps& assocMaps; |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 127 | #ifdef 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>> |
| 129 | global_start_time; |
| 130 | std::chrono::time_point<std::chrono::steady_clock> process_start_time; |
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 | |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 134 | void do_associations(sdbusplus::asio::connection* system_bus, |
Matt Spinler | e0b0e3a | 2019-04-08 10:39:23 -0500 | [diff] [blame] | 135 | interface_map_type& interfaceMap, |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 136 | sdbusplus::asio::object_server& objectServer, |
Adrian Ambrożewicz | bb40bd3 | 2021-02-12 13:36:26 +0100 | [diff] [blame] | 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; |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 141 | system_bus->async_method_call( |
Adrian Ambrożewicz | bb40bd3 | 2021-02-12 13:36:26 +0100 | [diff] [blame] | 142 | [&objectServer, path, processName, &interfaceMap, system_bus, |
| 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 | { |
| 151 | do_associations(system_bus, interfaceMap, objectServer, |
| 152 | processName, path, timeoutRetries + 1); |
| 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 | |
| 166 | void do_introspect(sdbusplus::asio::connection* system_bus, |
| 167 | std::shared_ptr<InProgressIntrospect> transaction, |
| 168 | interface_map_type& interface_map, |
| 169 | sdbusplus::asio::object_server& objectServer, |
Vernon Mauery | c52be0d | 2020-01-14 11:14:25 -0800 | [diff] [blame] | 170 | 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; |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 173 | system_bus->async_method_call( |
Vernon Mauery | c52be0d | 2020-01-14 11:14:25 -0800 | [diff] [blame] | 174 | [&interface_map, &objectServer, transaction, path, system_bus, |
| 175 | timeoutRetries](const boost::system::error_code ec, |
| 176 | const std::string& introspect_xml) { |
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 | { |
| 182 | do_introspect(system_bus, transaction, interface_map, |
| 183 | objectServer, path, timeoutRetries + 1); |
| 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() |
| 188 | << " on process: " << transaction->process_name |
| 189 | << " path: " << path << "\n"; |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | tinyxml2::XMLDocument doc; |
| 194 | |
| 195 | tinyxml2::XMLError e = doc.Parse(introspect_xml.c_str()); |
| 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 | } |
| 208 | auto& thisPathMap = interface_map[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 | { |
| 213 | const char* iface_name = pElement->Attribute("name"); |
| 214 | if (iface_name == nullptr) |
| 215 | { |
| 216 | continue; |
| 217 | } |
| 218 | |
Ed Tanous | d4dd96a | 2018-11-12 11:37:44 -0800 | [diff] [blame] | 219 | thisPathMap[transaction->process_name].emplace(iface_name); |
| 220 | |
John Wang | d0cf942 | 2019-09-17 16:01:34 +0800 | [diff] [blame] | 221 | if (std::strcmp(iface_name, assocDefsInterface) == 0) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 222 | { |
Matt Spinler | e0b0e3a | 2019-04-08 10:39:23 -0500 | [diff] [blame] | 223 | do_associations(system_bus, interface_map, objectServer, |
John Wang | d0cf942 | 2019-09-17 16:01:34 +0800 | [diff] [blame] | 224 | transaction->process_name, 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. |
| 232 | checkIfPendingAssociation(path, interface_map, |
| 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 | { |
Ed Tanous | 50232cd | 2018-11-12 11:34:43 -0800 | [diff] [blame] | 238 | const char* child_path = pElement->Attribute("name"); |
| 239 | if (child_path != nullptr) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 240 | { |
Ed Tanous | 50232cd | 2018-11-12 11:34:43 -0800 | [diff] [blame] | 241 | std::string parent_path(path); |
| 242 | if (parent_path == "/") |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 243 | { |
Ed Tanous | 50232cd | 2018-11-12 11:34:43 -0800 | [diff] [blame] | 244 | parent_path.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 | |
| 247 | do_introspect(system_bus, transaction, interface_map, |
| 248 | objectServer, parent_path + "/" + child_path); |
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 | }, |
| 253 | transaction->process_name, path, "org.freedesktop.DBus.Introspectable", |
| 254 | "Introspect"); |
| 255 | } |
| 256 | |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 257 | void start_new_introspect( |
Ed Tanous | 21c6059 | 2020-08-17 23:43:46 -0700 | [diff] [blame] | 258 | sdbusplus::asio::connection* system_bus, boost::asio::io_context& io, |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 259 | interface_map_type& interface_map, const std::string& process_name, |
Matt Spinler | 11401e2 | 2019-04-08 13:13:25 -0500 | [diff] [blame] | 260 | AssociationMaps& assocMaps, |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 261 | #ifdef 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>> |
| 263 | global_start_time, |
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 | { |
Andrew Geissler | 82815da | 2019-02-04 12:19:41 -0600 | [diff] [blame] | 267 | if (needToIntrospect(process_name, service_whitelist, service_blacklist)) |
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 = |
Matt Spinler | 11401e2 | 2019-04-08 13:13:25 -0500 | [diff] [blame] | 270 | std::make_shared<InProgressIntrospect>(system_bus, io, process_name, |
| 271 | assocMaps |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 272 | #ifdef DEBUG |
| 273 | , |
| 274 | global_start_time |
| 275 | #endif |
| 276 | ); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 277 | |
| 278 | do_introspect(system_bus, transaction, interface_map, objectServer, |
| 279 | "/"); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | // TODO(ed) replace with std::set_intersection once c++17 is available |
| 284 | template <class InputIt1, class InputIt2> |
| 285 | bool intersect(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2) |
| 286 | { |
| 287 | while (first1 != last1 && first2 != last2) |
| 288 | { |
| 289 | if (*first1 < *first2) |
| 290 | { |
| 291 | ++first1; |
| 292 | continue; |
| 293 | } |
| 294 | if (*first2 < *first1) |
| 295 | { |
| 296 | ++first2; |
| 297 | continue; |
| 298 | } |
| 299 | return true; |
| 300 | } |
| 301 | return false; |
| 302 | } |
| 303 | |
| 304 | void doListNames( |
Ed Tanous | 21c6059 | 2020-08-17 23:43:46 -0700 | [diff] [blame] | 305 | boost::asio::io_context& io, interface_map_type& interface_map, |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 306 | sdbusplus::asio::connection* system_bus, |
| 307 | boost::container::flat_map<std::string, std::string>& name_owners, |
Matt Spinler | 11401e2 | 2019-04-08 13:13:25 -0500 | [diff] [blame] | 308 | AssociationMaps& assocMaps, sdbusplus::asio::object_server& objectServer) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 309 | { |
| 310 | system_bus->async_method_call( |
Matt Spinler | 11401e2 | 2019-04-08 13:13:25 -0500 | [diff] [blame] | 311 | [&io, &interface_map, &name_owners, &objectServer, system_bus, |
| 312 | &assocMaps](const boost::system::error_code ec, |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 313 | std::vector<std::string> process_names) { |
| 314 | if (ec) |
| 315 | { |
| 316 | std::cerr << "Error getting names: " << ec << "\n"; |
| 317 | std::exit(EXIT_FAILURE); |
| 318 | return; |
| 319 | } |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 320 | // Try to make startup consistent |
| 321 | std::sort(process_names.begin(), process_names.end()); |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 322 | #ifdef DEBUG |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 323 | std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>> |
| 324 | global_start_time = std::make_shared< |
| 325 | std::chrono::time_point<std::chrono::steady_clock>>( |
| 326 | std::chrono::steady_clock::now()); |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 327 | #endif |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 328 | for (const std::string& process_name : process_names) |
| 329 | { |
Andrew Geissler | 82815da | 2019-02-04 12:19:41 -0600 | [diff] [blame] | 330 | if (needToIntrospect(process_name, service_whitelist, |
| 331 | service_blacklist)) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 332 | { |
| 333 | start_new_introspect(system_bus, io, interface_map, |
Matt Spinler | 11401e2 | 2019-04-08 13:13:25 -0500 | [diff] [blame] | 334 | process_name, assocMaps, |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 335 | #ifdef DEBUG |
| 336 | global_start_time, |
| 337 | #endif |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 338 | objectServer); |
| 339 | update_owners(system_bus, name_owners, process_name); |
| 340 | } |
| 341 | } |
| 342 | }, |
| 343 | "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", |
| 344 | "ListNames"); |
| 345 | } |
| 346 | |
Matt Spinler | dd94586 | 2018-09-07 12:41:05 -0500 | [diff] [blame] | 347 | void splitArgs(const std::string& stringArgs, |
| 348 | boost::container::flat_set<std::string>& listArgs) |
| 349 | { |
| 350 | std::istringstream args; |
| 351 | std::string arg; |
| 352 | |
| 353 | args.str(stringArgs); |
| 354 | |
| 355 | while (!args.eof()) |
| 356 | { |
| 357 | args >> arg; |
| 358 | if (!arg.empty()) |
| 359 | { |
| 360 | listArgs.insert(arg); |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
Matt Spinler | 47c0975 | 2018-11-29 14:54:13 -0600 | [diff] [blame] | 365 | void addObjectMapResult( |
| 366 | std::vector<interface_map_type::value_type>& objectMap, |
Matt Spinler | 9f0958e | 2018-09-11 08:26:10 -0500 | [diff] [blame] | 367 | const std::string& objectPath, |
| 368 | const std::pair<std::string, boost::container::flat_set<std::string>>& |
| 369 | interfaceMap) |
| 370 | { |
| 371 | // Adds an object path/service name/interface list entry to |
Matt Spinler | 47c0975 | 2018-11-29 14:54:13 -0600 | [diff] [blame] | 372 | // the results of GetSubTree and GetAncestors. |
Matt Spinler | 9f0958e | 2018-09-11 08:26:10 -0500 | [diff] [blame] | 373 | // If an entry for the object path already exists, just add the |
| 374 | // service name and interfaces to that entry, otherwise create |
| 375 | // a new entry. |
| 376 | auto entry = std::find_if( |
Matt Spinler | 47c0975 | 2018-11-29 14:54:13 -0600 | [diff] [blame] | 377 | objectMap.begin(), objectMap.end(), |
Matt Spinler | 9f0958e | 2018-09-11 08:26:10 -0500 | [diff] [blame] | 378 | [&objectPath](const auto& i) { return objectPath == i.first; }); |
| 379 | |
Matt Spinler | 47c0975 | 2018-11-29 14:54:13 -0600 | [diff] [blame] | 380 | if (entry != objectMap.end()) |
Matt Spinler | 9f0958e | 2018-09-11 08:26:10 -0500 | [diff] [blame] | 381 | { |
| 382 | entry->second.emplace(interfaceMap); |
| 383 | } |
| 384 | else |
| 385 | { |
| 386 | interface_map_type::value_type object; |
| 387 | object.first = objectPath; |
| 388 | object.second.emplace(interfaceMap); |
Matt Spinler | 47c0975 | 2018-11-29 14:54:13 -0600 | [diff] [blame] | 389 | objectMap.push_back(object); |
Matt Spinler | 9f0958e | 2018-09-11 08:26:10 -0500 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
Matt Spinler | a82779f | 2019-01-09 12:39:42 -0600 | [diff] [blame] | 393 | // Remove parents of the passed in path that: |
| 394 | // 1) Only have the 3 default interfaces on them |
| 395 | // - Means D-Bus created these, not application code, |
| 396 | // with the Properties, Introspectable, and Peer ifaces |
| 397 | // 2) Have no other child for this owner |
| 398 | void removeUnneededParents(const std::string& objectPath, |
| 399 | const std::string& owner, |
| 400 | interface_map_type& interface_map) |
| 401 | { |
| 402 | auto parent = objectPath; |
| 403 | |
| 404 | while (true) |
| 405 | { |
| 406 | auto pos = parent.find_last_of('/'); |
| 407 | if ((pos == std::string::npos) || (pos == 0)) |
| 408 | { |
| 409 | break; |
| 410 | } |
| 411 | parent = parent.substr(0, pos); |
| 412 | |
| 413 | auto parent_it = interface_map.find(parent); |
| 414 | if (parent_it == interface_map.end()) |
| 415 | { |
| 416 | break; |
| 417 | } |
| 418 | |
| 419 | auto ifaces_it = parent_it->second.find(owner); |
| 420 | if (ifaces_it == parent_it->second.end()) |
| 421 | { |
| 422 | break; |
| 423 | } |
| 424 | |
| 425 | if (ifaces_it->second.size() != 3) |
| 426 | { |
| 427 | break; |
| 428 | } |
| 429 | |
| 430 | auto child_path = parent + '/'; |
| 431 | |
| 432 | // Remove this parent if there isn't a remaining child on this owner |
| 433 | auto child = std::find_if( |
| 434 | interface_map.begin(), interface_map.end(), |
| 435 | [&owner, &child_path](const auto& entry) { |
| 436 | return boost::starts_with(entry.first, child_path) && |
| 437 | (entry.second.find(owner) != entry.second.end()); |
| 438 | }); |
| 439 | |
| 440 | if (child == interface_map.end()) |
| 441 | { |
| 442 | parent_it->second.erase(ifaces_it); |
| 443 | if (parent_it->second.empty()) |
| 444 | { |
| 445 | interface_map.erase(parent_it); |
| 446 | } |
| 447 | } |
| 448 | else |
| 449 | { |
| 450 | break; |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 455 | std::vector<interface_map_type::value_type> |
| 456 | getAncestors(const interface_map_type& interface_map, std::string req_path, |
| 457 | std::vector<std::string>& interfaces) |
| 458 | { |
| 459 | // Interfaces need to be sorted for intersect to function |
| 460 | std::sort(interfaces.begin(), interfaces.end()); |
| 461 | |
| 462 | if (boost::ends_with(req_path, "/")) |
| 463 | { |
| 464 | req_path.pop_back(); |
| 465 | } |
| 466 | if (req_path.size() && interface_map.find(req_path) == interface_map.end()) |
| 467 | { |
| 468 | throw NotFoundException(); |
| 469 | } |
| 470 | |
| 471 | std::vector<interface_map_type::value_type> ret; |
| 472 | for (auto& object_path : interface_map) |
| 473 | { |
| 474 | auto& this_path = object_path.first; |
| 475 | if (boost::starts_with(req_path, this_path) && (req_path != this_path)) |
| 476 | { |
| 477 | if (interfaces.empty()) |
| 478 | { |
| 479 | ret.emplace_back(object_path); |
| 480 | } |
| 481 | else |
| 482 | { |
| 483 | for (auto& interface_map : object_path.second) |
| 484 | { |
| 485 | |
| 486 | if (intersect(interfaces.begin(), interfaces.end(), |
| 487 | interface_map.second.begin(), |
| 488 | interface_map.second.end())) |
| 489 | { |
| 490 | addObjectMapResult(ret, this_path, interface_map); |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | return ret; |
| 498 | } |
| 499 | |
| 500 | boost::container::flat_map<std::string, boost::container::flat_set<std::string>> |
| 501 | getObject(const interface_map_type& interface_map, const std::string& path, |
| 502 | std::vector<std::string>& interfaces) |
| 503 | { |
| 504 | boost::container::flat_map<std::string, |
| 505 | boost::container::flat_set<std::string>> |
| 506 | results; |
| 507 | |
| 508 | // Interfaces need to be sorted for intersect to function |
| 509 | std::sort(interfaces.begin(), interfaces.end()); |
| 510 | auto path_ref = interface_map.find(path); |
| 511 | if (path_ref == interface_map.end()) |
| 512 | { |
| 513 | throw NotFoundException(); |
| 514 | } |
| 515 | if (interfaces.empty()) |
| 516 | { |
| 517 | return path_ref->second; |
| 518 | } |
| 519 | for (auto& interface_map : path_ref->second) |
| 520 | { |
| 521 | if (intersect(interfaces.begin(), interfaces.end(), |
| 522 | interface_map.second.begin(), interface_map.second.end())) |
| 523 | { |
| 524 | results.emplace(interface_map.first, interface_map.second); |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | if (results.empty()) |
| 529 | { |
| 530 | throw NotFoundException(); |
| 531 | } |
| 532 | |
| 533 | return results; |
| 534 | } |
| 535 | |
| 536 | std::vector<interface_map_type::value_type> |
| 537 | getSubTree(const interface_map_type& interface_map, std::string req_path, |
| 538 | int32_t depth, std::vector<std::string>& interfaces) |
| 539 | { |
| 540 | if (depth <= 0) |
| 541 | { |
| 542 | depth = std::numeric_limits<int32_t>::max(); |
| 543 | } |
| 544 | // Interfaces need to be sorted for intersect to function |
| 545 | std::sort(interfaces.begin(), interfaces.end()); |
| 546 | std::vector<interface_map_type::value_type> ret; |
| 547 | |
| 548 | if (boost::ends_with(req_path, "/")) |
| 549 | { |
| 550 | req_path.pop_back(); |
| 551 | } |
| 552 | if (req_path.size() && interface_map.find(req_path) == interface_map.end()) |
| 553 | { |
| 554 | throw NotFoundException(); |
| 555 | } |
| 556 | |
| 557 | for (auto& object_path : interface_map) |
| 558 | { |
| 559 | auto& this_path = object_path.first; |
| 560 | |
| 561 | if (this_path == req_path) |
| 562 | { |
| 563 | continue; |
| 564 | } |
| 565 | |
| 566 | if (boost::starts_with(this_path, req_path)) |
| 567 | { |
| 568 | // count the number of slashes past the search term |
| 569 | int32_t this_depth = std::count(this_path.begin() + req_path.size(), |
| 570 | this_path.end(), '/'); |
| 571 | if (this_depth <= depth) |
| 572 | { |
| 573 | for (auto& interface_map : object_path.second) |
| 574 | { |
| 575 | if (intersect(interfaces.begin(), interfaces.end(), |
| 576 | interface_map.second.begin(), |
| 577 | interface_map.second.end()) || |
| 578 | interfaces.empty()) |
| 579 | { |
| 580 | addObjectMapResult(ret, this_path, interface_map); |
| 581 | } |
| 582 | } |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | return ret; |
| 588 | } |
| 589 | |
| 590 | std::vector<std::string> |
| 591 | getSubTreePaths(const interface_map_type& interface_map, |
| 592 | std::string req_path, int32_t depth, |
| 593 | std::vector<std::string>& interfaces) |
| 594 | { |
| 595 | if (depth <= 0) |
| 596 | { |
| 597 | depth = std::numeric_limits<int32_t>::max(); |
| 598 | } |
| 599 | // Interfaces need to be sorted for intersect to function |
| 600 | std::sort(interfaces.begin(), interfaces.end()); |
| 601 | std::vector<std::string> ret; |
| 602 | |
| 603 | if (boost::ends_with(req_path, "/")) |
| 604 | { |
| 605 | req_path.pop_back(); |
| 606 | } |
| 607 | if (req_path.size() && interface_map.find(req_path) == interface_map.end()) |
| 608 | { |
| 609 | throw NotFoundException(); |
| 610 | } |
| 611 | |
| 612 | for (auto& object_path : interface_map) |
| 613 | { |
| 614 | auto& this_path = object_path.first; |
| 615 | |
| 616 | if (this_path == req_path) |
| 617 | { |
| 618 | continue; |
| 619 | } |
| 620 | |
| 621 | if (boost::starts_with(this_path, req_path)) |
| 622 | { |
| 623 | // count the number of slashes past the search term |
| 624 | int this_depth = std::count(this_path.begin() + req_path.size(), |
| 625 | this_path.end(), '/'); |
| 626 | if (this_depth <= depth) |
| 627 | { |
| 628 | bool add = interfaces.empty(); |
| 629 | for (auto& interface_map : object_path.second) |
| 630 | { |
| 631 | if (intersect(interfaces.begin(), interfaces.end(), |
| 632 | interface_map.second.begin(), |
| 633 | interface_map.second.end())) |
| 634 | { |
| 635 | add = true; |
| 636 | break; |
| 637 | } |
| 638 | } |
| 639 | if (add) |
| 640 | { |
| 641 | // TODO(ed) this is a copy |
| 642 | ret.emplace_back(this_path); |
| 643 | } |
| 644 | } |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | return ret; |
| 649 | } |
| 650 | |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 651 | int main(int argc, char** argv) |
| 652 | { |
Matt Spinler | dd94586 | 2018-09-07 12:41:05 -0500 | [diff] [blame] | 653 | auto options = ArgumentParser(argc, argv); |
Ed Tanous | 21c6059 | 2020-08-17 23:43:46 -0700 | [diff] [blame] | 654 | boost::asio::io_context io; |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 655 | std::shared_ptr<sdbusplus::asio::connection> system_bus = |
| 656 | std::make_shared<sdbusplus::asio::connection>(io); |
| 657 | |
Matt Spinler | dd94586 | 2018-09-07 12:41:05 -0500 | [diff] [blame] | 658 | splitArgs(options["service-namespaces"], service_whitelist); |
Matt Spinler | dd94586 | 2018-09-07 12:41:05 -0500 | [diff] [blame] | 659 | splitArgs(options["service-blacklists"], service_blacklist); |
| 660 | |
Ed Tanous | d4dd96a | 2018-11-12 11:37:44 -0800 | [diff] [blame] | 661 | // TODO(Ed) Remove this once all service files are updated to not use this. |
| 662 | // For now, simply squash the input, and ignore it. |
| 663 | boost::container::flat_set<std::string> iface_whitelist; |
| 664 | splitArgs(options["interface-namespaces"], iface_whitelist); |
| 665 | |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 666 | sdbusplus::asio::object_server server(system_bus); |
| 667 | |
| 668 | // Construct a signal set registered for process termination. |
| 669 | boost::asio::signal_set signals(io, SIGINT, SIGTERM); |
Brad Bishop | 2d41d6a | 2021-08-03 08:14:45 -0400 | [diff] [blame] | 670 | signals.async_wait( |
| 671 | [&io](const boost::system::error_code&, int) { io.stop(); }); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 672 | |
| 673 | interface_map_type interface_map; |
| 674 | boost::container::flat_map<std::string, std::string> name_owners; |
| 675 | |
| 676 | std::function<void(sdbusplus::message::message & message)> |
| 677 | nameChangeHandler = [&interface_map, &io, &name_owners, &server, |
| 678 | system_bus](sdbusplus::message::message& message) { |
Andrew Geissler | 2067926 | 2019-02-11 20:20:40 -0600 | [diff] [blame] | 679 | std::string name; // well-known |
| 680 | std::string old_owner; // unique-name |
| 681 | std::string new_owner; // unique-name |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 682 | |
| 683 | message.read(name, old_owner, new_owner); |
| 684 | |
| 685 | if (!old_owner.empty()) |
| 686 | { |
Andrew Geissler | 2067926 | 2019-02-11 20:20:40 -0600 | [diff] [blame] | 687 | processNameChangeDelete(name_owners, name, old_owner, |
Matt Spinler | e2359fb | 2019-04-05 14:11:33 -0500 | [diff] [blame] | 688 | interface_map, associationMaps, server); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | if (!new_owner.empty()) |
| 692 | { |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 693 | #ifdef DEBUG |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 694 | auto transaction = std::make_shared< |
| 695 | std::chrono::time_point<std::chrono::steady_clock>>( |
| 696 | std::chrono::steady_clock::now()); |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 697 | #endif |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 698 | // New daemon added |
Andrew Geissler | 82815da | 2019-02-04 12:19:41 -0600 | [diff] [blame] | 699 | if (needToIntrospect(name, service_whitelist, |
| 700 | service_blacklist)) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 701 | { |
| 702 | name_owners[new_owner] = name; |
| 703 | start_new_introspect(system_bus.get(), io, interface_map, |
Matt Spinler | 11401e2 | 2019-04-08 13:13:25 -0500 | [diff] [blame] | 704 | name, associationMaps, |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 705 | #ifdef DEBUG |
| 706 | transaction, |
| 707 | #endif |
| 708 | server); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 709 | } |
| 710 | } |
| 711 | }; |
| 712 | |
| 713 | sdbusplus::bus::match::match nameOwnerChanged( |
| 714 | static_cast<sdbusplus::bus::bus&>(*system_bus), |
| 715 | sdbusplus::bus::match::rules::nameOwnerChanged(), nameChangeHandler); |
| 716 | |
| 717 | std::function<void(sdbusplus::message::message & message)> |
| 718 | interfacesAddedHandler = [&interface_map, &name_owners, &server]( |
| 719 | sdbusplus::message::message& message) { |
| 720 | sdbusplus::message::object_path obj_path; |
Andrew Geissler | 7046189 | 2019-02-27 09:57:37 -0600 | [diff] [blame] | 721 | InterfacesAdded interfaces_added; |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 722 | message.read(obj_path, interfaces_added); |
| 723 | std::string well_known; |
Andrew Geissler | 3b025e6 | 2019-02-01 10:33:54 -0600 | [diff] [blame] | 724 | if (!getWellKnown(name_owners, message.get_sender(), well_known)) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 725 | { |
| 726 | return; // only introspect well-known |
| 727 | } |
Andrew Geissler | 82815da | 2019-02-04 12:19:41 -0600 | [diff] [blame] | 728 | if (needToIntrospect(well_known, service_whitelist, |
| 729 | service_blacklist)) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 730 | { |
Andrew Geissler | 7046189 | 2019-02-27 09:57:37 -0600 | [diff] [blame] | 731 | processInterfaceAdded(interface_map, obj_path, interfaces_added, |
Matt Spinler | e2359fb | 2019-04-05 14:11:33 -0500 | [diff] [blame] | 732 | well_known, associationMaps, server); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 733 | } |
| 734 | }; |
| 735 | |
| 736 | sdbusplus::bus::match::match interfacesAdded( |
| 737 | static_cast<sdbusplus::bus::bus&>(*system_bus), |
| 738 | sdbusplus::bus::match::rules::interfacesAdded(), |
| 739 | interfacesAddedHandler); |
| 740 | |
| 741 | std::function<void(sdbusplus::message::message & message)> |
| 742 | interfacesRemovedHandler = [&interface_map, &name_owners, &server]( |
| 743 | sdbusplus::message::message& message) { |
| 744 | sdbusplus::message::object_path obj_path; |
| 745 | std::vector<std::string> interfaces_removed; |
| 746 | message.read(obj_path, interfaces_removed); |
| 747 | auto connection_map = interface_map.find(obj_path.str); |
| 748 | if (connection_map == interface_map.end()) |
| 749 | { |
| 750 | return; |
| 751 | } |
| 752 | |
| 753 | std::string sender; |
Andrew Geissler | 3b025e6 | 2019-02-01 10:33:54 -0600 | [diff] [blame] | 754 | if (!getWellKnown(name_owners, message.get_sender(), sender)) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 755 | { |
| 756 | return; |
| 757 | } |
| 758 | for (const std::string& interface : interfaces_removed) |
| 759 | { |
| 760 | auto interface_set = connection_map->second.find(sender); |
| 761 | if (interface_set == connection_map->second.end()) |
| 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 | { |
Andrew Geissler | 67e5a42 | 2019-02-04 13:00:59 -0600 | [diff] [blame] | 768 | removeAssociation(obj_path.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 | |
| 772 | interface_set->second.erase(interface); |
Matt Spinler | 9c3d285 | 2019-04-08 15:57:19 -0500 | [diff] [blame] | 773 | |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 774 | if (interface_set->second.empty()) |
| 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 |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 778 | connection_map->second.erase(interface_set); |
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. |
| 785 | if ((connection_map->second.size() == 1) && |
| 786 | (connection_map->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. |
| 791 | moveAssociationToPending(obj_path.str, associationMaps, |
| 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 |
| 798 | if (connection_map->second.empty()) |
| 799 | { |
| 800 | interface_map.erase(connection_map); |
| 801 | } |
Matt Spinler | a82779f | 2019-01-09 12:39:42 -0600 | [diff] [blame] | 802 | |
| 803 | removeUnneededParents(obj_path.str, sender, interface_map); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 804 | }; |
| 805 | |
| 806 | sdbusplus::bus::match::match interfacesRemoved( |
| 807 | static_cast<sdbusplus::bus::bus&>(*system_bus), |
| 808 | sdbusplus::bus::match::rules::interfacesRemoved(), |
| 809 | interfacesRemovedHandler); |
| 810 | |
| 811 | std::function<void(sdbusplus::message::message & message)> |
Matt Spinler | e0b0e3a | 2019-04-08 10:39:23 -0500 | [diff] [blame] | 812 | associationChangedHandler = [&server, &name_owners, &interface_map]( |
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 | |
| 825 | std::string well_known; |
| 826 | if (!getWellKnown(name_owners, message.get_sender(), |
| 827 | well_known)) |
| 828 | { |
| 829 | return; |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 830 | } |
Matt Spinler | 8f876a5 | 2019-04-15 13:22:50 -0500 | [diff] [blame] | 831 | associationChanged(server, associations, message.get_path(), |
Matt Spinler | e0b0e3a | 2019-04-08 10:39:23 -0500 | [diff] [blame] | 832 | well_known, interface_map, associationMaps); |
Matt Spinler | 8f876a5 | 2019-04-15 13:22:50 -0500 | [diff] [blame] | 833 | } |
| 834 | }; |
| 835 | sdbusplus::bus::match::match assocChangedMatch( |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 836 | static_cast<sdbusplus::bus::bus&>(*system_bus), |
| 837 | sdbusplus::bus::match::rules::interface( |
| 838 | "org.freedesktop.DBus.Properties") + |
| 839 | sdbusplus::bus::match::rules::member("PropertiesChanged") + |
Matt Spinler | 8f876a5 | 2019-04-15 13:22:50 -0500 | [diff] [blame] | 840 | sdbusplus::bus::match::rules::argN(0, assocDefsInterface), |
| 841 | associationChangedHandler); |
| 842 | |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 843 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface = |
Brad Bishop | a02cd54 | 2021-10-12 19:12:42 -0400 | [diff] [blame] | 844 | server.add_interface("/xyz/openbmc_project/object_mapper", |
| 845 | "xyz.openbmc_project.ObjectMapper"); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 846 | |
| 847 | iface->register_method( |
Matt Spinler | 6a39e8c | 2018-11-13 11:11:36 -0600 | [diff] [blame] | 848 | "GetAncestors", [&interface_map](std::string& req_path, |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 849 | std::vector<std::string>& interfaces) { |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 850 | return getAncestors(interface_map, req_path, interfaces); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 851 | }); |
| 852 | |
| 853 | iface->register_method( |
| 854 | "GetObject", [&interface_map](const std::string& path, |
| 855 | std::vector<std::string>& interfaces) { |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 856 | return getObject(interface_map, path, interfaces); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 857 | }); |
| 858 | |
| 859 | iface->register_method( |
Matt Spinler | 153494e | 2018-11-07 16:35:40 -0600 | [diff] [blame] | 860 | "GetSubTree", [&interface_map](std::string& req_path, int32_t depth, |
| 861 | std::vector<std::string>& interfaces) { |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 862 | return getSubTree(interface_map, req_path, depth, interfaces); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 863 | }); |
| 864 | |
| 865 | iface->register_method( |
| 866 | "GetSubTreePaths", |
Matt Spinler | 153494e | 2018-11-07 16:35:40 -0600 | [diff] [blame] | 867 | [&interface_map](std::string& req_path, int32_t depth, |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 868 | std::vector<std::string>& interfaces) { |
Ed Tanous | 0a13c76 | 2021-09-28 13:29:25 -0700 | [diff] [blame] | 869 | return getSubTreePaths(interface_map, req_path, depth, interfaces); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 870 | }); |
| 871 | |
| 872 | iface->initialize(); |
| 873 | |
| 874 | io.post([&]() { |
Matt Spinler | 11401e2 | 2019-04-08 13:13:25 -0500 | [diff] [blame] | 875 | doListNames(io, interface_map, system_bus.get(), name_owners, |
| 876 | associationMaps, server); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 877 | }); |
| 878 | |
Brad Bishop | a02cd54 | 2021-10-12 19:12:42 -0400 | [diff] [blame] | 879 | system_bus->request_name("xyz.openbmc_project.ObjectMapper"); |
Vishwanatha Subbanna | 64354ef | 2020-08-21 03:35:26 -0500 | [diff] [blame] | 880 | |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 881 | io.run(); |
| 882 | } |