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