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