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