blob: 3ee6362989dee71c6d4c394a069cc77f30162a31 [file] [log] [blame]
Matt Spinlerdd945862018-09-07 12:41:05 -05001#include "src/argument.hpp"
2
Ed Tanous60520632018-06-11 17:46:52 -07003#include <tinyxml2.h>
4
5#include <atomic>
6#include <boost/algorithm/string/predicate.hpp>
7#include <boost/container/flat_map.hpp>
8#include <boost/container/flat_set.hpp>
9#include <chrono>
10#include <iomanip>
11#include <iostream>
12#include <sdbusplus/asio/connection.hpp>
13#include <sdbusplus/asio/object_server.hpp>
14
15constexpr const char* OBJECT_MAPPER_DBUS_NAME =
16 "xyz.openbmc_project.ObjectMapper";
17constexpr const char* ASSOCIATIONS_INTERFACE = "org.openbmc.Associations";
Matt Spinler1036b4d2018-09-18 16:45:29 -050018constexpr const char* XYZ_ASSOCIATION_INTERFACE =
19 "xyz.openbmc_project.Association";
Ed Tanous60520632018-06-11 17:46:52 -070020
21// interface_map_type is the underlying datastructure the mapper uses.
22// The 3 levels of map are
23// object paths
24// connection names
25// interface names
26using interface_map_type = boost::container::flat_map<
27 std::string, boost::container::flat_map<
28 std::string, boost::container::flat_set<std::string>>>;
29
30using Association = std::tuple<std::string, std::string, std::string>;
31
Matt Spinler1036b4d2018-09-18 16:45:29 -050032// Associations and some metadata are stored in associationInterfaces.
33// The fields are:
34// * ifacePos - holds the D-Bus interface object
35// * endpointPos - holds the endpoints array that shadows the property
36// * sourcePos - holds the owning source path of the association
37static constexpr auto ifacePos = 0;
38static constexpr auto endpointsPos = 1;
39static constexpr auto sourcePos = 2;
40using Endpoints = std::vector<std::string>;
41boost::container::flat_map<
42 std::string, std::tuple<std::shared_ptr<sdbusplus::asio::dbus_interface>,
43 Endpoints, std::string>>
Ed Tanous60520632018-06-11 17:46:52 -070044 associationInterfaces;
45
Matt Spinlerdd945862018-09-07 12:41:05 -050046static boost::container::flat_set<std::string> service_whitelist;
47static boost::container::flat_set<std::string> service_blacklist;
Matt Spinlerdd945862018-09-07 12:41:05 -050048
Ed Tanous60520632018-06-11 17:46:52 -070049/** Exception thrown when a path is not found in the object list. */
50struct NotFoundException final : public sdbusplus::exception_t
51{
52 const char* name() const noexcept override
53 {
54 return "org.freedesktop.DBus.Error.FileNotFound";
55 };
56 const char* description() const noexcept override
57 {
58 return "path or object not found";
59 };
60 const char* what() const noexcept override
61 {
62 return "org.freedesktop.DBus.Error.FileNotFound: "
63 "The requested object was not found";
64 };
65};
66
67bool get_well_known(
68 boost::container::flat_map<std::string, std::string>& owners,
69 const std::string& request, std::string& well_known)
70{
71 // If it's already a well known name, just return
72 if (!boost::starts_with(request, ":"))
73 {
74 well_known = request;
75 return true;
76 }
77
78 auto it = owners.find(request);
79 if (it == owners.end())
80 {
81 return false;
82 }
83 well_known = it->second;
84 return true;
85}
86
87void update_owners(sdbusplus::asio::connection* conn,
88 boost::container::flat_map<std::string, std::string>& owners,
89 const std::string& new_object)
90{
91 if (boost::starts_with(new_object, ":"))
92 {
93 return;
94 }
95 conn->async_method_call(
96 [&, new_object](const boost::system::error_code ec,
97 const std::string& nameOwner) {
98 if (ec)
99 {
100 std::cerr << "Error getting owner of " << new_object << " : "
101 << ec << "\n";
102 return;
103 }
104 owners[nameOwner] = new_object;
105 },
106 "org.freedesktop.DBus", "/", "org.freedesktop.DBus", "GetNameOwner",
107 new_object);
108}
109
110void send_introspection_complete_signal(sdbusplus::asio::connection* system_bus,
111 const std::string& process_name)
112{
113 // TODO(ed) This signal doesn't get exposed properly in the
114 // introspect right now. Find out how to register signals in
115 // sdbusplus
116 sdbusplus::message::message m = system_bus->new_signal(
117 "/xyz/openbmc_project/object_mapper",
118 "xyz.openbmc_project.ObjectMapper.Private", "IntrospectionComplete");
119 m.append(process_name);
120 m.signal_send();
121}
122
123struct InProgressIntrospect
124{
125 InProgressIntrospect(
126 sdbusplus::asio::connection* system_bus, boost::asio::io_service& io,
Matt Spinleraecabe82018-09-19 13:25:42 -0500127 const std::string& process_name
128#ifdef DEBUG
129 ,
Ed Tanous60520632018-06-11 17:46:52 -0700130 std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>>
Matt Spinleraecabe82018-09-19 13:25:42 -0500131 global_start_time
132#endif
133 ) :
Ed Tanous60520632018-06-11 17:46:52 -0700134 system_bus(system_bus),
Matt Spinleraecabe82018-09-19 13:25:42 -0500135 io(io), process_name(process_name)
136#ifdef DEBUG
137 ,
Ed Tanous60520632018-06-11 17:46:52 -0700138 global_start_time(global_start_time),
139 process_start_time(std::chrono::steady_clock::now())
Matt Spinleraecabe82018-09-19 13:25:42 -0500140#endif
Ed Tanous60520632018-06-11 17:46:52 -0700141 {
142 }
143 ~InProgressIntrospect()
144 {
145 send_introspection_complete_signal(system_bus, process_name);
Matt Spinleraecabe82018-09-19 13:25:42 -0500146
147#ifdef DEBUG
Ed Tanous60520632018-06-11 17:46:52 -0700148 std::chrono::duration<float> diff =
149 std::chrono::steady_clock::now() - process_start_time;
150 std::cout << std::setw(50) << process_name << " scan took "
151 << diff.count() << " seconds\n";
152
153 // If we're the last outstanding caller globally, calculate the
154 // time it took
155 if (global_start_time != nullptr && global_start_time.use_count() == 1)
156 {
157 diff = std::chrono::steady_clock::now() - *global_start_time;
158 std::cout << "Total scan took " << diff.count()
159 << " seconds to complete\n";
160 }
Matt Spinleraecabe82018-09-19 13:25:42 -0500161#endif
Ed Tanous60520632018-06-11 17:46:52 -0700162 }
163 sdbusplus::asio::connection* system_bus;
164 boost::asio::io_service& io;
165 std::string process_name;
Matt Spinleraecabe82018-09-19 13:25:42 -0500166#ifdef DEBUG
Ed Tanous60520632018-06-11 17:46:52 -0700167 std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>>
168 global_start_time;
169 std::chrono::time_point<std::chrono::steady_clock> process_start_time;
Matt Spinleraecabe82018-09-19 13:25:42 -0500170#endif
Ed Tanous60520632018-06-11 17:46:52 -0700171};
172
Ed Tanous60520632018-06-11 17:46:52 -0700173void addAssociation(sdbusplus::asio::object_server& objectServer,
174 const std::vector<Association>& associations,
175 const std::string& path)
176{
177 boost::container::flat_map<std::string,
178 boost::container::flat_set<std::string>>
179 objects;
180 for (const Association& association : associations)
181 {
182 std::string forward;
183 std::string reverse;
184 std::string endpoint;
185 std::tie(forward, reverse, endpoint) = association;
186
187 if (forward.size())
188 {
189 objects[path + "/" + forward].emplace(endpoint);
190 }
191 if (reverse.size())
192 {
193 if (endpoint.empty())
194 {
195 std::cerr << "Found invalid association on path " << path
196 << "\n";
197 continue;
198 }
199 objects[endpoint + "/" + reverse].emplace(path);
200 }
201 }
202 for (const auto& object : objects)
203 {
204 // the mapper exposes the new association interface but intakes
205 // the old
206
207 auto& iface = associationInterfaces[object.first];
Matt Spinler1036b4d2018-09-18 16:45:29 -0500208 auto& i = std::get<ifacePos>(iface);
209 auto& endpoints = std::get<endpointsPos>(iface);
210 std::get<sourcePos>(iface) = path;
211
212 // Only add new endpoints
213 for (auto& e : object.second)
214 {
215 if (std::find(endpoints.begin(), endpoints.end(), e) ==
216 endpoints.end())
217 {
218 endpoints.push_back(e);
219 }
220 }
221
222 // If the interface already exists, only need to update
223 // the property value, otherwise create it
224 if (i)
225 {
226 i->set_property("endpoints", endpoints);
227 }
228 else
229 {
230 i = objectServer.add_interface(object.first,
231 XYZ_ASSOCIATION_INTERFACE);
232 i->register_property("endpoints", endpoints);
233 i->initialize();
234 }
235 }
236}
237
238void removeAssociation(const std::string& sourcePath,
239 sdbusplus::asio::object_server& server)
240{
241 // The sourcePath passed in can be:
242 // a) the source of the association object, in which case
243 // that whole object needs to be deleted, and/or
244 // b) just an entry in the endpoints property under some
245 // other path, in which case it just needs to be removed
246 // from the property.
247 for (auto& assoc : associationInterfaces)
248 {
249 if (sourcePath == std::get<sourcePos>(assoc.second))
250 {
251 server.remove_interface(std::get<ifacePos>(assoc.second));
252 std::get<ifacePos>(assoc.second) = nullptr;
253 std::get<endpointsPos>(assoc.second).clear();
254 std::get<sourcePos>(assoc.second).clear();
255 }
256 else
257 {
258 auto& endpoints = std::get<endpointsPos>(assoc.second);
259 auto toRemove =
260 std::find(endpoints.begin(), endpoints.end(), sourcePath);
261 if (toRemove != endpoints.end())
262 {
263 endpoints.erase(toRemove);
264
265 if (endpoints.empty())
266 {
267 // Remove the interface object too if no longer needed
268 server.remove_interface(std::get<ifacePos>(assoc.second));
269 std::get<ifacePos>(assoc.second) = nullptr;
270 std::get<sourcePos>(assoc.second).clear();
271 }
272 else
273 {
274 std::get<ifacePos>(assoc.second)
275 ->set_property("endpoints", endpoints);
276 }
277 }
278 }
Ed Tanous60520632018-06-11 17:46:52 -0700279 }
280}
281
282void do_associations(sdbusplus::asio::connection* system_bus,
283 sdbusplus::asio::object_server& objectServer,
284 const std::string& processName, const std::string& path)
285{
286 system_bus->async_method_call(
287 [&objectServer,
288 path](const boost::system::error_code ec,
289 const sdbusplus::message::variant<std::vector<Association>>&
290 variantAssociations) {
291 if (ec)
292 {
293 std::cerr << "Error getting associations from " << path << "\n";
294 }
295 std::vector<Association> associations =
296 sdbusplus::message::variant_ns::get<std::vector<Association>>(
297 variantAssociations);
298 addAssociation(objectServer, associations, path);
299 },
300 processName, path, "org.freedesktop.DBus.Properties", "Get",
301 ASSOCIATIONS_INTERFACE, "associations");
302}
303
304void do_introspect(sdbusplus::asio::connection* system_bus,
305 std::shared_ptr<InProgressIntrospect> transaction,
306 interface_map_type& interface_map,
307 sdbusplus::asio::object_server& objectServer,
308 std::string path)
309{
310 system_bus->async_method_call(
311 [&interface_map, &objectServer, transaction, path,
312 system_bus](const boost::system::error_code ec,
313 const std::string& introspect_xml) {
314 if (ec)
315 {
316 std::cerr << "Introspect call failed with error: " << ec << ", "
317 << ec.message()
318 << " on process: " << transaction->process_name
319 << " path: " << path << "\n";
320 return;
321 }
322
323 tinyxml2::XMLDocument doc;
324
325 tinyxml2::XMLError e = doc.Parse(introspect_xml.c_str());
326 if (e != tinyxml2::XMLError::XML_SUCCESS)
327 {
328 std::cerr << "XML parsing failed\n";
329 return;
330 }
331
332 tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node");
333 if (pRoot == nullptr)
334 {
335 std::cerr << "XML document did not contain any data\n";
336 return;
337 }
338 auto& thisPathMap = interface_map[path];
Ed Tanous60520632018-06-11 17:46:52 -0700339 tinyxml2::XMLElement* pElement =
340 pRoot->FirstChildElement("interface");
341 while (pElement != nullptr)
342 {
343 const char* iface_name = pElement->Attribute("name");
344 if (iface_name == nullptr)
345 {
346 continue;
347 }
348
Matt Spinlerdd945862018-09-07 12:41:05 -0500349 std::string iface{iface_name};
350
Ed Tanousd4dd96a2018-11-12 11:37:44 -0800351 thisPathMap[transaction->process_name].emplace(iface_name);
352
Ed Tanous60520632018-06-11 17:46:52 -0700353 if (std::strcmp(iface_name, ASSOCIATIONS_INTERFACE) == 0)
354 {
355 do_associations(system_bus, objectServer,
356 transaction->process_name, path);
357 }
Ed Tanous60520632018-06-11 17:46:52 -0700358
359 pElement = pElement->NextSiblingElement("interface");
360 }
361
Ed Tanous50232cd2018-11-12 11:34:43 -0800362 pElement = pRoot->FirstChildElement("node");
363 while (pElement != nullptr)
Ed Tanous60520632018-06-11 17:46:52 -0700364 {
Ed Tanous50232cd2018-11-12 11:34:43 -0800365 const char* child_path = pElement->Attribute("name");
366 if (child_path != nullptr)
Ed Tanous60520632018-06-11 17:46:52 -0700367 {
Ed Tanous50232cd2018-11-12 11:34:43 -0800368 std::string parent_path(path);
369 if (parent_path == "/")
Ed Tanous60520632018-06-11 17:46:52 -0700370 {
Ed Tanous50232cd2018-11-12 11:34:43 -0800371 parent_path.clear();
Ed Tanous60520632018-06-11 17:46:52 -0700372 }
Ed Tanous50232cd2018-11-12 11:34:43 -0800373
374 do_introspect(system_bus, transaction, interface_map,
375 objectServer, parent_path + "/" + child_path);
Ed Tanous60520632018-06-11 17:46:52 -0700376 }
Ed Tanous50232cd2018-11-12 11:34:43 -0800377 pElement = pElement->NextSiblingElement("node");
Ed Tanous60520632018-06-11 17:46:52 -0700378 }
379 },
380 transaction->process_name, path, "org.freedesktop.DBus.Introspectable",
381 "Introspect");
382}
383
384bool need_to_introspect(const std::string& process_name)
385{
Matt Spinlerdd945862018-09-07 12:41:05 -0500386 auto inWhitelist =
387 std::find_if(service_whitelist.begin(), service_whitelist.end(),
388 [&process_name](const auto& prefix) {
389 return boost::starts_with(process_name, prefix);
390 }) != service_whitelist.end();
391
392 // This holds full service names, not prefixes
393 auto inBlacklist =
394 service_blacklist.find(process_name) != service_blacklist.end();
395
396 return inWhitelist && !inBlacklist;
Ed Tanous60520632018-06-11 17:46:52 -0700397}
398
399void start_new_introspect(
400 sdbusplus::asio::connection* system_bus, boost::asio::io_service& io,
401 interface_map_type& interface_map, const std::string& process_name,
Matt Spinleraecabe82018-09-19 13:25:42 -0500402#ifdef DEBUG
Ed Tanous60520632018-06-11 17:46:52 -0700403 std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>>
404 global_start_time,
Matt Spinleraecabe82018-09-19 13:25:42 -0500405#endif
Ed Tanous60520632018-06-11 17:46:52 -0700406 sdbusplus::asio::object_server& objectServer)
407{
408 if (need_to_introspect(process_name))
409 {
Ed Tanous60520632018-06-11 17:46:52 -0700410 std::shared_ptr<InProgressIntrospect> transaction =
Matt Spinleraecabe82018-09-19 13:25:42 -0500411 std::make_shared<InProgressIntrospect>(system_bus, io, process_name
412#ifdef DEBUG
413 ,
414 global_start_time
415#endif
416 );
Ed Tanous60520632018-06-11 17:46:52 -0700417
418 do_introspect(system_bus, transaction, interface_map, objectServer,
419 "/");
420 }
421}
422
423// TODO(ed) replace with std::set_intersection once c++17 is available
424template <class InputIt1, class InputIt2>
425bool intersect(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2)
426{
427 while (first1 != last1 && first2 != last2)
428 {
429 if (*first1 < *first2)
430 {
431 ++first1;
432 continue;
433 }
434 if (*first2 < *first1)
435 {
436 ++first2;
437 continue;
438 }
439 return true;
440 }
441 return false;
442}
443
444void doListNames(
445 boost::asio::io_service& io, interface_map_type& interface_map,
446 sdbusplus::asio::connection* system_bus,
447 boost::container::flat_map<std::string, std::string>& name_owners,
448 sdbusplus::asio::object_server& objectServer)
449{
450 system_bus->async_method_call(
451 [&io, &interface_map, &name_owners, &objectServer,
452 system_bus](const boost::system::error_code ec,
453 std::vector<std::string> process_names) {
454 if (ec)
455 {
456 std::cerr << "Error getting names: " << ec << "\n";
457 std::exit(EXIT_FAILURE);
458 return;
459 }
Ed Tanous60520632018-06-11 17:46:52 -0700460 // Try to make startup consistent
461 std::sort(process_names.begin(), process_names.end());
Matt Spinleraecabe82018-09-19 13:25:42 -0500462#ifdef DEBUG
Ed Tanous60520632018-06-11 17:46:52 -0700463 std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>>
464 global_start_time = std::make_shared<
465 std::chrono::time_point<std::chrono::steady_clock>>(
466 std::chrono::steady_clock::now());
Matt Spinleraecabe82018-09-19 13:25:42 -0500467#endif
Ed Tanous60520632018-06-11 17:46:52 -0700468 for (const std::string& process_name : process_names)
469 {
470 if (need_to_introspect(process_name))
471 {
472 start_new_introspect(system_bus, io, interface_map,
Matt Spinleraecabe82018-09-19 13:25:42 -0500473 process_name,
474#ifdef DEBUG
475 global_start_time,
476#endif
Ed Tanous60520632018-06-11 17:46:52 -0700477 objectServer);
478 update_owners(system_bus, name_owners, process_name);
479 }
480 }
481 },
482 "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus",
483 "ListNames");
484}
485
Matt Spinlerdd945862018-09-07 12:41:05 -0500486void splitArgs(const std::string& stringArgs,
487 boost::container::flat_set<std::string>& listArgs)
488{
489 std::istringstream args;
490 std::string arg;
491
492 args.str(stringArgs);
493
494 while (!args.eof())
495 {
496 args >> arg;
497 if (!arg.empty())
498 {
499 listArgs.insert(arg);
500 }
501 }
502}
503
Matt Spinler9f0958e2018-09-11 08:26:10 -0500504void addSubtreeResult(
505 std::vector<interface_map_type::value_type>& subtree,
506 const std::string& objectPath,
507 const std::pair<std::string, boost::container::flat_set<std::string>>&
508 interfaceMap)
509{
510 // Adds an object path/service name/interface list entry to
511 // the results of GetSubTree.
512 // If an entry for the object path already exists, just add the
513 // service name and interfaces to that entry, otherwise create
514 // a new entry.
515 auto entry = std::find_if(
516 subtree.begin(), subtree.end(),
517 [&objectPath](const auto& i) { return objectPath == i.first; });
518
519 if (entry != subtree.end())
520 {
521 entry->second.emplace(interfaceMap);
522 }
523 else
524 {
525 interface_map_type::value_type object;
526 object.first = objectPath;
527 object.second.emplace(interfaceMap);
528 subtree.push_back(object);
529 }
530}
531
Ed Tanous60520632018-06-11 17:46:52 -0700532int main(int argc, char** argv)
533{
Matt Spinlerdd945862018-09-07 12:41:05 -0500534 auto options = ArgumentParser(argc, argv);
Ed Tanous60520632018-06-11 17:46:52 -0700535 boost::asio::io_service io;
536 std::shared_ptr<sdbusplus::asio::connection> system_bus =
537 std::make_shared<sdbusplus::asio::connection>(io);
538
Matt Spinlerdd945862018-09-07 12:41:05 -0500539 splitArgs(options["service-namespaces"], service_whitelist);
Matt Spinlerdd945862018-09-07 12:41:05 -0500540 splitArgs(options["service-blacklists"], service_blacklist);
541
Ed Tanousd4dd96a2018-11-12 11:37:44 -0800542 // TODO(Ed) Remove this once all service files are updated to not use this.
543 // For now, simply squash the input, and ignore it.
544 boost::container::flat_set<std::string> iface_whitelist;
545 splitArgs(options["interface-namespaces"], iface_whitelist);
546
Ed Tanous60520632018-06-11 17:46:52 -0700547 system_bus->request_name(OBJECT_MAPPER_DBUS_NAME);
548 sdbusplus::asio::object_server server(system_bus);
549
550 // Construct a signal set registered for process termination.
551 boost::asio::signal_set signals(io, SIGINT, SIGTERM);
552 signals.async_wait([&io](const boost::system::error_code& error,
553 int signal_number) { io.stop(); });
554
555 interface_map_type interface_map;
556 boost::container::flat_map<std::string, std::string> name_owners;
557
558 std::function<void(sdbusplus::message::message & message)>
559 nameChangeHandler = [&interface_map, &io, &name_owners, &server,
560 system_bus](sdbusplus::message::message& message) {
561 std::string name;
562 std::string old_owner;
563 std::string new_owner;
564
565 message.read(name, old_owner, new_owner);
566
567 if (!old_owner.empty())
568 {
569 if (boost::starts_with(old_owner, ":"))
570 {
571 auto it = name_owners.find(old_owner);
572 if (it != name_owners.end())
573 {
574 name_owners.erase(it);
575 }
576 }
577 // Connection removed
578 interface_map_type::iterator path_it = interface_map.begin();
579 while (path_it != interface_map.end())
580 {
Matt Spinler1036b4d2018-09-18 16:45:29 -0500581 // If an associations interface is being removed,
582 // also need to remove the corresponding associations
583 // objects and properties.
584 auto ifaces = path_it->second.find(name);
585 if (ifaces != path_it->second.end())
586 {
587 auto assoc = std::find(ifaces->second.begin(),
588 ifaces->second.end(),
589 ASSOCIATIONS_INTERFACE);
590
591 if (assoc != ifaces->second.end())
592 {
593 removeAssociation(path_it->first, server);
594 }
595 }
596
Ed Tanous60520632018-06-11 17:46:52 -0700597 path_it->second.erase(name);
598 if (path_it->second.empty())
599 {
600 // If the last connection to the object is gone,
601 // delete the top level object
602 path_it = interface_map.erase(path_it);
603 continue;
604 }
605 path_it++;
606 }
607 }
608
609 if (!new_owner.empty())
610 {
Matt Spinleraecabe82018-09-19 13:25:42 -0500611#ifdef DEBUG
Ed Tanous60520632018-06-11 17:46:52 -0700612 auto transaction = std::make_shared<
613 std::chrono::time_point<std::chrono::steady_clock>>(
614 std::chrono::steady_clock::now());
Matt Spinleraecabe82018-09-19 13:25:42 -0500615#endif
Ed Tanous60520632018-06-11 17:46:52 -0700616 // New daemon added
617 if (need_to_introspect(name))
618 {
619 name_owners[new_owner] = name;
620 start_new_introspect(system_bus.get(), io, interface_map,
Matt Spinleraecabe82018-09-19 13:25:42 -0500621 name,
622#ifdef DEBUG
623 transaction,
624#endif
625 server);
Ed Tanous60520632018-06-11 17:46:52 -0700626 }
627 }
628 };
629
630 sdbusplus::bus::match::match nameOwnerChanged(
631 static_cast<sdbusplus::bus::bus&>(*system_bus),
632 sdbusplus::bus::match::rules::nameOwnerChanged(), nameChangeHandler);
633
634 std::function<void(sdbusplus::message::message & message)>
635 interfacesAddedHandler = [&interface_map, &name_owners, &server](
636 sdbusplus::message::message& message) {
637 sdbusplus::message::object_path obj_path;
638 std::vector<std::pair<
639 std::string, std::vector<std::pair<
640 std::string, sdbusplus::message::variant<
641 std::vector<Association>>>>>>
642 interfaces_added;
643 message.read(obj_path, interfaces_added);
644 std::string well_known;
645 if (!get_well_known(name_owners, message.get_sender(), well_known))
646 {
647 return; // only introspect well-known
648 }
649 if (need_to_introspect(well_known))
650 {
651 auto& iface_list = interface_map[obj_path.str];
652
Matt Spinlercc74e4b2018-09-18 16:21:54 -0500653 for (const auto& interface_pair : interfaces_added)
Ed Tanous60520632018-06-11 17:46:52 -0700654 {
655 iface_list[well_known].emplace(interface_pair.first);
656
657 if (interface_pair.first == ASSOCIATIONS_INTERFACE)
658 {
659 const sdbusplus::message::variant<
660 std::vector<Association>>* variantAssociations =
661 nullptr;
662 for (const auto& interface : interface_pair.second)
663 {
664 if (interface.first == "associations")
665 {
666 variantAssociations = &(interface.second);
667 }
668 }
669 if (variantAssociations == nullptr)
670 {
671 std::cerr << "Illegal association found on "
672 << well_known << "\n";
673 continue;
674 }
675 std::vector<Association> associations =
676 sdbusplus::message::variant_ns::get<
677 std::vector<Association>>(*variantAssociations);
678 addAssociation(server, associations, obj_path.str);
679 }
680 }
681 }
682 };
683
684 sdbusplus::bus::match::match interfacesAdded(
685 static_cast<sdbusplus::bus::bus&>(*system_bus),
686 sdbusplus::bus::match::rules::interfacesAdded(),
687 interfacesAddedHandler);
688
689 std::function<void(sdbusplus::message::message & message)>
690 interfacesRemovedHandler = [&interface_map, &name_owners, &server](
691 sdbusplus::message::message& message) {
692 sdbusplus::message::object_path obj_path;
693 std::vector<std::string> interfaces_removed;
694 message.read(obj_path, interfaces_removed);
695 auto connection_map = interface_map.find(obj_path.str);
696 if (connection_map == interface_map.end())
697 {
698 return;
699 }
700
701 std::string sender;
702 if (!get_well_known(name_owners, message.get_sender(), sender))
703 {
704 return;
705 }
706 for (const std::string& interface : interfaces_removed)
707 {
708 auto interface_set = connection_map->second.find(sender);
709 if (interface_set == connection_map->second.end())
710 {
711 continue;
712 }
713
714 if (interface == ASSOCIATIONS_INTERFACE)
715 {
Matt Spinler1036b4d2018-09-18 16:45:29 -0500716 removeAssociation(obj_path.str, server);
Ed Tanous60520632018-06-11 17:46:52 -0700717 }
718
719 interface_set->second.erase(interface);
720 // If this was the last interface on this connection,
721 // erase the connection
722 if (interface_set->second.empty())
723 {
724 connection_map->second.erase(interface_set);
725 }
726 }
727 // If this was the last connection on this object path,
728 // erase the object path
729 if (connection_map->second.empty())
730 {
731 interface_map.erase(connection_map);
732 }
733 };
734
735 sdbusplus::bus::match::match interfacesRemoved(
736 static_cast<sdbusplus::bus::bus&>(*system_bus),
737 sdbusplus::bus::match::rules::interfacesRemoved(),
738 interfacesRemovedHandler);
739
740 std::function<void(sdbusplus::message::message & message)>
741 associationChangedHandler =
742 [&server](sdbusplus::message::message& message) {
743 std::string objectName;
744 boost::container::flat_map<
745 std::string,
746 sdbusplus::message::variant<std::vector<Association>>>
747 values;
748 message.read(objectName, values);
749 auto findAssociations = values.find("associations");
750 if (findAssociations != values.end())
751 {
752 std::vector<Association> associations =
753 sdbusplus::message::variant_ns::get<
754 std::vector<Association>>(findAssociations->second);
755 addAssociation(server, associations, message.get_path());
756 }
757 };
758 sdbusplus::bus::match::match associationChanged(
759 static_cast<sdbusplus::bus::bus&>(*system_bus),
760 sdbusplus::bus::match::rules::interface(
761 "org.freedesktop.DBus.Properties") +
762 sdbusplus::bus::match::rules::member("PropertiesChanged") +
763 sdbusplus::bus::match::rules::argN(0, ASSOCIATIONS_INTERFACE),
764 associationChangedHandler);
765
766 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
767 server.add_interface("/xyz/openbmc_project/object_mapper",
768 "xyz.openbmc_project.ObjectMapper");
769
770 iface->register_method(
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600771 "GetAncestors", [&interface_map](std::string& req_path,
Ed Tanous60520632018-06-11 17:46:52 -0700772 std::vector<std::string>& interfaces) {
773 // Interfaces need to be sorted for intersect to function
774 std::sort(interfaces.begin(), interfaces.end());
775
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600776 if (req_path.size() > 1)
777 {
778 if (req_path.back() == '/')
779 {
780 req_path.pop_back();
781 }
782
783 if (interface_map.find(req_path) == interface_map.end())
784 {
785 throw NotFoundException();
786 }
787 }
788
Ed Tanous60520632018-06-11 17:46:52 -0700789 std::vector<interface_map_type::value_type> ret;
790 for (auto& object_path : interface_map)
791 {
792 auto& this_path = object_path.first;
Matt Spinlerea6516c2018-09-25 16:23:13 -0500793 if (boost::starts_with(req_path, this_path) &&
794 (req_path != this_path))
Ed Tanous60520632018-06-11 17:46:52 -0700795 {
796 if (interfaces.empty())
797 {
798 ret.emplace_back(object_path);
799 }
800 else
801 {
802 for (auto& interface_map : object_path.second)
803 {
804
805 if (intersect(interfaces.begin(), interfaces.end(),
806 interface_map.second.begin(),
807 interface_map.second.end()))
808 {
809 ret.emplace_back(object_path);
810 break;
811 }
812 }
813 }
814 }
815 }
Ed Tanous60520632018-06-11 17:46:52 -0700816
817 return ret;
818 });
819
820 iface->register_method(
821 "GetObject", [&interface_map](const std::string& path,
822 std::vector<std::string>& interfaces) {
Matt Spinler7a38d412018-09-18 16:13:25 -0500823 boost::container::flat_map<std::string,
824 boost::container::flat_set<std::string>>
825 results;
826
Ed Tanous60520632018-06-11 17:46:52 -0700827 // Interfaces need to be sorted for intersect to function
828 std::sort(interfaces.begin(), interfaces.end());
829 auto path_ref = interface_map.find(path);
830 if (path_ref == interface_map.end())
831 {
832 throw NotFoundException();
833 }
834 if (interfaces.empty())
835 {
836 return path_ref->second;
837 }
838 for (auto& interface_map : path_ref->second)
839 {
840 if (intersect(interfaces.begin(), interfaces.end(),
841 interface_map.second.begin(),
842 interface_map.second.end()))
843 {
Matt Spinler7a38d412018-09-18 16:13:25 -0500844 results.emplace(interface_map.first, interface_map.second);
Ed Tanous60520632018-06-11 17:46:52 -0700845 }
846 }
Matt Spinler7a38d412018-09-18 16:13:25 -0500847
848 if (results.empty())
849 {
850 throw NotFoundException();
851 }
852
853 return results;
Ed Tanous60520632018-06-11 17:46:52 -0700854 });
855
856 iface->register_method(
Matt Spinler153494e2018-11-07 16:35:40 -0600857 "GetSubTree", [&interface_map](std::string& req_path, int32_t depth,
858 std::vector<std::string>& interfaces) {
Ed Tanous60520632018-06-11 17:46:52 -0700859 if (depth <= 0)
860 {
861 depth = std::numeric_limits<int32_t>::max();
862 }
863 // Interfaces need to be sorted for intersect to function
864 std::sort(interfaces.begin(), interfaces.end());
865 std::vector<interface_map_type::value_type> ret;
866
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600867 if (req_path.size() > 1)
Matt Spinler153494e2018-11-07 16:35:40 -0600868 {
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600869 if (req_path.back() == '/')
870 {
871 req_path.pop_back();
872 }
873
874 if (interface_map.find(req_path) == interface_map.end())
875 {
876 throw NotFoundException();
877 }
Matt Spinler153494e2018-11-07 16:35:40 -0600878 }
879
Ed Tanous60520632018-06-11 17:46:52 -0700880 for (auto& object_path : interface_map)
881 {
882 auto& this_path = object_path.first;
Matt Spinler153494e2018-11-07 16:35:40 -0600883
884 if (this_path == req_path)
885 {
886 continue;
887 }
888
Ed Tanous60520632018-06-11 17:46:52 -0700889 if (boost::starts_with(this_path, req_path))
890 {
891 // count the number of slashes past the search term
892 int32_t this_depth =
893 std::count(this_path.begin() + req_path.size(),
894 this_path.end(), '/');
895 if (this_depth <= depth)
896 {
Ed Tanous60520632018-06-11 17:46:52 -0700897 for (auto& interface_map : object_path.second)
898 {
899 if (intersect(interfaces.begin(), interfaces.end(),
900 interface_map.second.begin(),
Matt Spinler9f0958e2018-09-11 08:26:10 -0500901 interface_map.second.end()) ||
902 interfaces.empty())
Ed Tanous60520632018-06-11 17:46:52 -0700903 {
Matt Spinler9f0958e2018-09-11 08:26:10 -0500904 addSubtreeResult(ret, this_path, interface_map);
Ed Tanous60520632018-06-11 17:46:52 -0700905 }
906 }
Ed Tanous60520632018-06-11 17:46:52 -0700907 }
908 }
909 }
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600910
Ed Tanous60520632018-06-11 17:46:52 -0700911 return ret;
912 });
913
914 iface->register_method(
915 "GetSubTreePaths",
Matt Spinler153494e2018-11-07 16:35:40 -0600916 [&interface_map](std::string& req_path, int32_t depth,
Ed Tanous60520632018-06-11 17:46:52 -0700917 std::vector<std::string>& interfaces) {
918 if (depth <= 0)
919 {
920 depth = std::numeric_limits<int32_t>::max();
921 }
922 // Interfaces need to be sorted for intersect to function
923 std::sort(interfaces.begin(), interfaces.end());
924 std::vector<std::string> ret;
Matt Spinler153494e2018-11-07 16:35:40 -0600925
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600926 if (req_path.size() > 1)
Matt Spinler153494e2018-11-07 16:35:40 -0600927 {
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600928 if (req_path.back() == '/')
929 {
930 req_path.pop_back();
931 }
932
933 if (interface_map.find(req_path) == interface_map.end())
934 {
935 throw NotFoundException();
936 }
Matt Spinler153494e2018-11-07 16:35:40 -0600937 }
938
Ed Tanous60520632018-06-11 17:46:52 -0700939 for (auto& object_path : interface_map)
940 {
941 auto& this_path = object_path.first;
Matt Spinler153494e2018-11-07 16:35:40 -0600942
943 if (this_path == req_path)
944 {
945 continue;
946 }
947
Ed Tanous60520632018-06-11 17:46:52 -0700948 if (boost::starts_with(this_path, req_path))
949 {
950 // count the number of slashes past the search term
951 int this_depth =
952 std::count(this_path.begin() + req_path.size(),
953 this_path.end(), '/');
954 if (this_depth <= depth)
955 {
956 bool add = interfaces.empty();
957 for (auto& interface_map : object_path.second)
958 {
959 if (intersect(interfaces.begin(), interfaces.end(),
960 interface_map.second.begin(),
961 interface_map.second.end()))
962 {
963 add = true;
964 break;
965 }
966 }
967 if (add)
968 {
969 // TODO(ed) this is a copy
970 ret.emplace_back(this_path);
971 }
972 }
973 }
974 }
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600975
Ed Tanous60520632018-06-11 17:46:52 -0700976 return ret;
977 });
978
979 iface->initialize();
980
981 io.post([&]() {
982 doListNames(io, interface_map, system_bus.get(), name_owners, server);
983 });
984
Ed Tanous60520632018-06-11 17:46:52 -0700985 io.run();
986}