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