xref: /openbmc/phosphor-objmgr/src/main.cpp (revision e0b0e3a2)
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
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)
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 #ifdef DEBUG
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;
127 #endif
128 };
129 
130 void do_associations(sdbusplus::asio::connection* system_bus,
131                      interface_map_type& interfaceMap,
132                      sdbusplus::asio::object_server& objectServer,
133                      const std::string& processName, const std::string& path,
134                      const std::string& assocDefIface)
135 {
136     system_bus->async_method_call(
137         [&objectServer, path, processName, &interfaceMap](
138             const boost::system::error_code ec,
139             const sdbusplus::message::variant<std::vector<Association>>&
140                 variantAssociations) {
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);
148             associationChanged(objectServer, associations, path, processName,
149                                interfaceMap, associationMaps);
150         },
151         processName, path, "org.freedesktop.DBus.Properties", "Get",
152         assocDefIface, getAssocDefPropName(assocDefIface));
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];
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 
200                 thisPathMap[transaction->process_name].emplace(iface_name);
201 
202                 if (isAssocDefIface(iface_name))
203                 {
204                     do_associations(system_bus, interface_map, objectServer,
205                                     transaction->process_name, path,
206                                     iface_name);
207                 }
208 
209                 pElement = pElement->NextSiblingElement("interface");
210             }
211 
212             pElement = pRoot->FirstChildElement("node");
213             while (pElement != nullptr)
214             {
215                 const char* child_path = pElement->Attribute("name");
216                 if (child_path != nullptr)
217                 {
218                     std::string parent_path(path);
219                     if (parent_path == "/")
220                     {
221                         parent_path.clear();
222                     }
223 
224                     do_introspect(system_bus, transaction, interface_map,
225                                   objectServer, parent_path + "/" + child_path);
226                 }
227                 pElement = pElement->NextSiblingElement("node");
228             }
229         },
230         transaction->process_name, path, "org.freedesktop.DBus.Introspectable",
231         "Introspect");
232 }
233 
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,
237 #ifdef DEBUG
238     std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>>
239         global_start_time,
240 #endif
241     sdbusplus::asio::object_server& objectServer)
242 {
243     if (needToIntrospect(process_name, service_whitelist, service_blacklist))
244     {
245         std::shared_ptr<InProgressIntrospect> transaction =
246             std::make_shared<InProgressIntrospect>(system_bus, io, process_name
247 #ifdef DEBUG
248                                                    ,
249                                                    global_start_time
250 #endif
251             );
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             }
295             // Try to make startup consistent
296             std::sort(process_names.begin(), process_names.end());
297 #ifdef DEBUG
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());
302 #endif
303             for (const std::string& process_name : process_names)
304             {
305                 if (needToIntrospect(process_name, service_whitelist,
306                                      service_blacklist))
307                 {
308                     start_new_introspect(system_bus, io, interface_map,
309                                          process_name,
310 #ifdef DEBUG
311                                          global_start_time,
312 #endif
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 
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 
340 void addObjectMapResult(
341     std::vector<interface_map_type::value_type>& objectMap,
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
347     // the results of GetSubTree and GetAncestors.
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(
352         objectMap.begin(), objectMap.end(),
353         [&objectPath](const auto& i) { return objectPath == i.first; });
354 
355     if (entry != objectMap.end())
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);
364         objectMap.push_back(object);
365     }
366 }
367 
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 
430 int main(int argc, char** argv)
431 {
432     auto options = ArgumentParser(argc, argv);
433     boost::asio::io_service io;
434     std::shared_ptr<sdbusplus::asio::connection> system_bus =
435         std::make_shared<sdbusplus::asio::connection>(io);
436 
437     splitArgs(options["service-namespaces"], service_whitelist);
438     splitArgs(options["service-blacklists"], service_blacklist);
439 
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 
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) {
459             std::string name;      // well-known
460             std::string old_owner; // unique-name
461             std::string new_owner; // unique-name
462 
463             message.read(name, old_owner, new_owner);
464 
465             if (!old_owner.empty())
466             {
467                 processNameChangeDelete(name_owners, name, old_owner,
468                                         interface_map, associationMaps, server);
469             }
470 
471             if (!new_owner.empty())
472             {
473 #ifdef DEBUG
474                 auto transaction = std::make_shared<
475                     std::chrono::time_point<std::chrono::steady_clock>>(
476                     std::chrono::steady_clock::now());
477 #endif
478                 // New daemon added
479                 if (needToIntrospect(name, service_whitelist,
480                                      service_blacklist))
481                 {
482                     name_owners[new_owner] = name;
483                     start_new_introspect(system_bus.get(), io, interface_map,
484                                          name,
485 #ifdef DEBUG
486                                          transaction,
487 #endif
488                                          server);
489                 }
490             }
491         };
492 
493     sdbusplus::bus::match::match nameOwnerChanged(
494         static_cast<sdbusplus::bus::bus&>(*system_bus),
495         sdbusplus::bus::match::rules::nameOwnerChanged(), nameChangeHandler);
496 
497     std::function<void(sdbusplus::message::message & message)>
498         interfacesAddedHandler = [&interface_map, &name_owners, &server](
499                                      sdbusplus::message::message& message) {
500             sdbusplus::message::object_path obj_path;
501             InterfacesAdded interfaces_added;
502             message.read(obj_path, interfaces_added);
503             std::string well_known;
504             if (!getWellKnown(name_owners, message.get_sender(), well_known))
505             {
506                 return; // only introspect well-known
507             }
508             if (needToIntrospect(well_known, service_whitelist,
509                                  service_blacklist))
510             {
511                 processInterfaceAdded(interface_map, obj_path, interfaces_added,
512                                       well_known, associationMaps, server);
513             }
514         };
515 
516     sdbusplus::bus::match::match interfacesAdded(
517         static_cast<sdbusplus::bus::bus&>(*system_bus),
518         sdbusplus::bus::match::rules::interfacesAdded(),
519         interfacesAddedHandler);
520 
521     std::function<void(sdbusplus::message::message & message)>
522         interfacesRemovedHandler = [&interface_map, &name_owners, &server](
523                                        sdbusplus::message::message& message) {
524             sdbusplus::message::object_path obj_path;
525             std::vector<std::string> interfaces_removed;
526             message.read(obj_path, interfaces_removed);
527             auto connection_map = interface_map.find(obj_path.str);
528             if (connection_map == interface_map.end())
529             {
530                 return;
531             }
532 
533             std::string sender;
534             if (!getWellKnown(name_owners, message.get_sender(), sender))
535             {
536                 return;
537             }
538             for (const std::string& interface : interfaces_removed)
539             {
540                 auto interface_set = connection_map->second.find(sender);
541                 if (interface_set == connection_map->second.end())
542                 {
543                     continue;
544                 }
545 
546                 if (isAssocDefIface(interface))
547                 {
548                     removeAssociation(obj_path.str, sender, server,
549                                       associationMaps);
550                 }
551 
552                 interface_set->second.erase(interface);
553                 // If this was the last interface on this connection,
554                 // erase the connection
555                 if (interface_set->second.empty())
556                 {
557                     connection_map->second.erase(interface_set);
558                 }
559             }
560             // If this was the last connection on this object path,
561             // erase the object path
562             if (connection_map->second.empty())
563             {
564                 interface_map.erase(connection_map);
565             }
566 
567             removeUnneededParents(obj_path.str, sender, interface_map);
568         };
569 
570     sdbusplus::bus::match::match interfacesRemoved(
571         static_cast<sdbusplus::bus::bus&>(*system_bus),
572         sdbusplus::bus::match::rules::interfacesRemoved(),
573         interfacesRemovedHandler);
574 
575     std::function<void(sdbusplus::message::message & message)>
576         associationChangedHandler = [&server, &name_owners, &interface_map](
577                                         sdbusplus::message::message& message) {
578             std::string objectName;
579             boost::container::flat_map<
580                 std::string,
581                 sdbusplus::message::variant<std::vector<Association>>>
582                 values;
583             message.read(objectName, values);
584 
585             auto prop =
586                 std::find_if(values.begin(), values.end(), [](const auto& v) {
587                     using namespace boost::algorithm;
588                     return to_lower_copy(v.first) == "associations";
589                 });
590 
591             if (prop != values.end())
592             {
593                 std::vector<Association> associations =
594                     sdbusplus::message::variant_ns::get<
595                         std::vector<Association>>(prop->second);
596 
597                 std::string well_known;
598                 if (!getWellKnown(name_owners, message.get_sender(),
599                                   well_known))
600                 {
601                     return;
602                 }
603                 associationChanged(server, associations, message.get_path(),
604                                    well_known, interface_map, associationMaps);
605             }
606         };
607     sdbusplus::bus::match::match assocChangedMatch(
608         static_cast<sdbusplus::bus::bus&>(*system_bus),
609         sdbusplus::bus::match::rules::interface(
610             "org.freedesktop.DBus.Properties") +
611             sdbusplus::bus::match::rules::member("PropertiesChanged") +
612             sdbusplus::bus::match::rules::argN(0, assocDefsInterface),
613         associationChangedHandler);
614 
615     sdbusplus::bus::match::match orgOpenbmcAssocChangedMatch(
616         static_cast<sdbusplus::bus::bus&>(*system_bus),
617         sdbusplus::bus::match::rules::interface(
618             "org.freedesktop.DBus.Properties") +
619             sdbusplus::bus::match::rules::member("PropertiesChanged") +
620             sdbusplus::bus::match::rules::argN(0, orgOpenBMCAssocDefsInterface),
621         associationChangedHandler);
622 
623     std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
624         server.add_interface("/xyz/openbmc_project/object_mapper",
625                              "xyz.openbmc_project.ObjectMapper");
626 
627     iface->register_method(
628         "GetAncestors", [&interface_map](std::string& req_path,
629                                          std::vector<std::string>& interfaces) {
630             // Interfaces need to be sorted for intersect to function
631             std::sort(interfaces.begin(), interfaces.end());
632 
633             if (boost::ends_with(req_path, "/"))
634             {
635                 req_path.pop_back();
636             }
637             if (req_path.size() &&
638                 interface_map.find(req_path) == interface_map.end())
639             {
640                 throw NotFoundException();
641             }
642 
643             std::vector<interface_map_type::value_type> ret;
644             for (auto& object_path : interface_map)
645             {
646                 auto& this_path = object_path.first;
647                 if (boost::starts_with(req_path, this_path) &&
648                     (req_path != this_path))
649                 {
650                     if (interfaces.empty())
651                     {
652                         ret.emplace_back(object_path);
653                     }
654                     else
655                     {
656                         for (auto& interface_map : object_path.second)
657                         {
658 
659                             if (intersect(interfaces.begin(), interfaces.end(),
660                                           interface_map.second.begin(),
661                                           interface_map.second.end()))
662                             {
663                                 addObjectMapResult(ret, this_path,
664                                                    interface_map);
665                             }
666                         }
667                     }
668                 }
669             }
670 
671             return ret;
672         });
673 
674     iface->register_method(
675         "GetObject", [&interface_map](const std::string& path,
676                                       std::vector<std::string>& interfaces) {
677             boost::container::flat_map<std::string,
678                                        boost::container::flat_set<std::string>>
679                 results;
680 
681             // Interfaces need to be sorted for intersect to function
682             std::sort(interfaces.begin(), interfaces.end());
683             auto path_ref = interface_map.find(path);
684             if (path_ref == interface_map.end())
685             {
686                 throw NotFoundException();
687             }
688             if (interfaces.empty())
689             {
690                 return path_ref->second;
691             }
692             for (auto& interface_map : path_ref->second)
693             {
694                 if (intersect(interfaces.begin(), interfaces.end(),
695                               interface_map.second.begin(),
696                               interface_map.second.end()))
697                 {
698                     results.emplace(interface_map.first, interface_map.second);
699                 }
700             }
701 
702             if (results.empty())
703             {
704                 throw NotFoundException();
705             }
706 
707             return results;
708         });
709 
710     iface->register_method(
711         "GetSubTree", [&interface_map](std::string& req_path, int32_t depth,
712                                        std::vector<std::string>& interfaces) {
713             if (depth <= 0)
714             {
715                 depth = std::numeric_limits<int32_t>::max();
716             }
717             // Interfaces need to be sorted for intersect to function
718             std::sort(interfaces.begin(), interfaces.end());
719             std::vector<interface_map_type::value_type> ret;
720 
721             if (boost::ends_with(req_path, "/"))
722             {
723                 req_path.pop_back();
724             }
725             if (req_path.size() &&
726                 interface_map.find(req_path) == interface_map.end())
727             {
728                 throw NotFoundException();
729             }
730 
731             for (auto& object_path : interface_map)
732             {
733                 auto& this_path = object_path.first;
734 
735                 if (this_path == req_path)
736                 {
737                     continue;
738                 }
739 
740                 if (boost::starts_with(this_path, req_path))
741                 {
742                     // count the number of slashes past the search term
743                     int32_t this_depth =
744                         std::count(this_path.begin() + req_path.size(),
745                                    this_path.end(), '/');
746                     if (this_depth <= depth)
747                     {
748                         for (auto& interface_map : object_path.second)
749                         {
750                             if (intersect(interfaces.begin(), interfaces.end(),
751                                           interface_map.second.begin(),
752                                           interface_map.second.end()) ||
753                                 interfaces.empty())
754                             {
755                                 addObjectMapResult(ret, this_path,
756                                                    interface_map);
757                             }
758                         }
759                     }
760                 }
761             }
762 
763             return ret;
764         });
765 
766     iface->register_method(
767         "GetSubTreePaths",
768         [&interface_map](std::string& req_path, int32_t depth,
769                          std::vector<std::string>& interfaces) {
770             if (depth <= 0)
771             {
772                 depth = std::numeric_limits<int32_t>::max();
773             }
774             // Interfaces need to be sorted for intersect to function
775             std::sort(interfaces.begin(), interfaces.end());
776             std::vector<std::string> ret;
777 
778             if (boost::ends_with(req_path, "/"))
779             {
780                 req_path.pop_back();
781             }
782             if (req_path.size() &&
783                 interface_map.find(req_path) == interface_map.end())
784             {
785                 throw NotFoundException();
786             }
787 
788             for (auto& object_path : interface_map)
789             {
790                 auto& this_path = object_path.first;
791 
792                 if (this_path == req_path)
793                 {
794                     continue;
795                 }
796 
797                 if (boost::starts_with(this_path, req_path))
798                 {
799                     // count the number of slashes past the search term
800                     int this_depth =
801                         std::count(this_path.begin() + req_path.size(),
802                                    this_path.end(), '/');
803                     if (this_depth <= depth)
804                     {
805                         bool add = interfaces.empty();
806                         for (auto& interface_map : object_path.second)
807                         {
808                             if (intersect(interfaces.begin(), interfaces.end(),
809                                           interface_map.second.begin(),
810                                           interface_map.second.end()))
811                             {
812                                 add = true;
813                                 break;
814                             }
815                         }
816                         if (add)
817                         {
818                             // TODO(ed) this is a copy
819                             ret.emplace_back(this_path);
820                         }
821                     }
822                 }
823             }
824 
825             return ret;
826         });
827 
828     iface->initialize();
829 
830     io.post([&]() {
831         doListNames(io, interface_map, system_bus.get(), name_owners, server);
832     });
833 
834     io.run();
835 }
836