blob: 3208a17b0ceb8ec1db48e14d30f48b7b76bdd97c [file] [log] [blame]
Andrew Geisslera80a3af2019-02-04 14:01:49 -06001#include "associations.hpp"
Andrew Geissler3b025e62019-02-01 10:33:54 -06002#include "processing.hpp"
Matt Spinlerdd945862018-09-07 12:41:05 -05003#include "src/argument.hpp"
4
Ed Tanous60520632018-06-11 17:46:52 -07005#include <tinyxml2.h>
6
7#include <atomic>
8#include <boost/algorithm/string/predicate.hpp>
9#include <boost/container/flat_map.hpp>
Ed Tanous60520632018-06-11 17:46:52 -070010#include <chrono>
11#include <iomanip>
12#include <iostream>
13#include <sdbusplus/asio/connection.hpp>
14#include <sdbusplus/asio/object_server.hpp>
15
16constexpr const char* OBJECT_MAPPER_DBUS_NAME =
17 "xyz.openbmc_project.ObjectMapper";
Ed Tanous60520632018-06-11 17:46:52 -070018
Andrew Geissler67e5a422019-02-04 13:00:59 -060019AssociationInterfaces associationInterfaces;
Matt Spinler937a2322019-01-23 13:54:22 -060020AssociationOwnersType associationOwners;
21
Andrew Geissler82815da2019-02-04 12:19:41 -060022static WhiteBlackList service_whitelist;
23static WhiteBlackList service_blacklist;
Matt Spinlerdd945862018-09-07 12:41:05 -050024
Ed Tanous60520632018-06-11 17:46:52 -070025/** Exception thrown when a path is not found in the object list. */
26struct NotFoundException final : public sdbusplus::exception_t
27{
28 const char* name() const noexcept override
29 {
30 return "org.freedesktop.DBus.Error.FileNotFound";
31 };
32 const char* description() const noexcept override
33 {
34 return "path or object not found";
35 };
36 const char* what() const noexcept override
37 {
38 return "org.freedesktop.DBus.Error.FileNotFound: "
39 "The requested object was not found";
40 };
41};
42
Ed Tanous60520632018-06-11 17:46:52 -070043void update_owners(sdbusplus::asio::connection* conn,
44 boost::container::flat_map<std::string, std::string>& owners,
45 const std::string& new_object)
46{
47 if (boost::starts_with(new_object, ":"))
48 {
49 return;
50 }
51 conn->async_method_call(
52 [&, new_object](const boost::system::error_code ec,
53 const std::string& nameOwner) {
54 if (ec)
55 {
56 std::cerr << "Error getting owner of " << new_object << " : "
57 << ec << "\n";
58 return;
59 }
60 owners[nameOwner] = new_object;
61 },
62 "org.freedesktop.DBus", "/", "org.freedesktop.DBus", "GetNameOwner",
63 new_object);
64}
65
66void send_introspection_complete_signal(sdbusplus::asio::connection* system_bus,
67 const std::string& process_name)
68{
69 // TODO(ed) This signal doesn't get exposed properly in the
70 // introspect right now. Find out how to register signals in
71 // sdbusplus
72 sdbusplus::message::message m = system_bus->new_signal(
73 "/xyz/openbmc_project/object_mapper",
74 "xyz.openbmc_project.ObjectMapper.Private", "IntrospectionComplete");
75 m.append(process_name);
76 m.signal_send();
77}
78
79struct InProgressIntrospect
80{
81 InProgressIntrospect(
82 sdbusplus::asio::connection* system_bus, boost::asio::io_service& io,
Matt Spinleraecabe82018-09-19 13:25:42 -050083 const std::string& process_name
84#ifdef DEBUG
85 ,
Ed Tanous60520632018-06-11 17:46:52 -070086 std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>>
Matt Spinleraecabe82018-09-19 13:25:42 -050087 global_start_time
88#endif
89 ) :
Ed Tanous60520632018-06-11 17:46:52 -070090 system_bus(system_bus),
Matt Spinleraecabe82018-09-19 13:25:42 -050091 io(io), process_name(process_name)
92#ifdef DEBUG
93 ,
Ed Tanous60520632018-06-11 17:46:52 -070094 global_start_time(global_start_time),
95 process_start_time(std::chrono::steady_clock::now())
Matt Spinleraecabe82018-09-19 13:25:42 -050096#endif
Ed Tanous60520632018-06-11 17:46:52 -070097 {
98 }
99 ~InProgressIntrospect()
100 {
101 send_introspection_complete_signal(system_bus, process_name);
Matt Spinleraecabe82018-09-19 13:25:42 -0500102
103#ifdef DEBUG
Ed Tanous60520632018-06-11 17:46:52 -0700104 std::chrono::duration<float> diff =
105 std::chrono::steady_clock::now() - process_start_time;
106 std::cout << std::setw(50) << process_name << " scan took "
107 << diff.count() << " seconds\n";
108
109 // If we're the last outstanding caller globally, calculate the
110 // time it took
111 if (global_start_time != nullptr && global_start_time.use_count() == 1)
112 {
113 diff = std::chrono::steady_clock::now() - *global_start_time;
114 std::cout << "Total scan took " << diff.count()
115 << " seconds to complete\n";
116 }
Matt Spinleraecabe82018-09-19 13:25:42 -0500117#endif
Ed Tanous60520632018-06-11 17:46:52 -0700118 }
119 sdbusplus::asio::connection* system_bus;
120 boost::asio::io_service& io;
121 std::string process_name;
Matt Spinleraecabe82018-09-19 13:25:42 -0500122#ifdef DEBUG
Ed Tanous60520632018-06-11 17:46:52 -0700123 std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>>
124 global_start_time;
125 std::chrono::time_point<std::chrono::steady_clock> process_start_time;
Matt Spinleraecabe82018-09-19 13:25:42 -0500126#endif
Ed Tanous60520632018-06-11 17:46:52 -0700127};
128
Ed Tanous60520632018-06-11 17:46:52 -0700129void do_associations(sdbusplus::asio::connection* system_bus,
130 sdbusplus::asio::object_server& objectServer,
131 const std::string& processName, const std::string& path)
132{
133 system_bus->async_method_call(
Matt Spinler937a2322019-01-23 13:54:22 -0600134 [&objectServer, path, processName](
135 const boost::system::error_code ec,
136 const sdbusplus::message::variant<std::vector<Association>>&
137 variantAssociations) {
Ed Tanous60520632018-06-11 17:46:52 -0700138 if (ec)
139 {
140 std::cerr << "Error getting associations from " << path << "\n";
141 }
142 std::vector<Association> associations =
143 sdbusplus::message::variant_ns::get<std::vector<Association>>(
144 variantAssociations);
Andrew Geissler4511b332019-02-21 15:40:40 -0600145 associationChanged(objectServer, associations, path, processName,
146 associationOwners, associationInterfaces);
Ed Tanous60520632018-06-11 17:46:52 -0700147 },
148 processName, path, "org.freedesktop.DBus.Properties", "Get",
149 ASSOCIATIONS_INTERFACE, "associations");
150}
151
152void do_introspect(sdbusplus::asio::connection* system_bus,
153 std::shared_ptr<InProgressIntrospect> transaction,
154 interface_map_type& interface_map,
155 sdbusplus::asio::object_server& objectServer,
156 std::string path)
157{
158 system_bus->async_method_call(
159 [&interface_map, &objectServer, transaction, path,
160 system_bus](const boost::system::error_code ec,
161 const std::string& introspect_xml) {
162 if (ec)
163 {
164 std::cerr << "Introspect call failed with error: " << ec << ", "
165 << ec.message()
166 << " on process: " << transaction->process_name
167 << " path: " << path << "\n";
168 return;
169 }
170
171 tinyxml2::XMLDocument doc;
172
173 tinyxml2::XMLError e = doc.Parse(introspect_xml.c_str());
174 if (e != tinyxml2::XMLError::XML_SUCCESS)
175 {
176 std::cerr << "XML parsing failed\n";
177 return;
178 }
179
180 tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node");
181 if (pRoot == nullptr)
182 {
183 std::cerr << "XML document did not contain any data\n";
184 return;
185 }
186 auto& thisPathMap = interface_map[path];
Ed Tanous60520632018-06-11 17:46:52 -0700187 tinyxml2::XMLElement* pElement =
188 pRoot->FirstChildElement("interface");
189 while (pElement != nullptr)
190 {
191 const char* iface_name = pElement->Attribute("name");
192 if (iface_name == nullptr)
193 {
194 continue;
195 }
196
Matt Spinlerdd945862018-09-07 12:41:05 -0500197 std::string iface{iface_name};
198
Ed Tanousd4dd96a2018-11-12 11:37:44 -0800199 thisPathMap[transaction->process_name].emplace(iface_name);
200
Ed Tanous60520632018-06-11 17:46:52 -0700201 if (std::strcmp(iface_name, ASSOCIATIONS_INTERFACE) == 0)
202 {
203 do_associations(system_bus, objectServer,
204 transaction->process_name, path);
205 }
Ed Tanous60520632018-06-11 17:46:52 -0700206
207 pElement = pElement->NextSiblingElement("interface");
208 }
209
Ed Tanous50232cd2018-11-12 11:34:43 -0800210 pElement = pRoot->FirstChildElement("node");
211 while (pElement != nullptr)
Ed Tanous60520632018-06-11 17:46:52 -0700212 {
Ed Tanous50232cd2018-11-12 11:34:43 -0800213 const char* child_path = pElement->Attribute("name");
214 if (child_path != nullptr)
Ed Tanous60520632018-06-11 17:46:52 -0700215 {
Ed Tanous50232cd2018-11-12 11:34:43 -0800216 std::string parent_path(path);
217 if (parent_path == "/")
Ed Tanous60520632018-06-11 17:46:52 -0700218 {
Ed Tanous50232cd2018-11-12 11:34:43 -0800219 parent_path.clear();
Ed Tanous60520632018-06-11 17:46:52 -0700220 }
Ed Tanous50232cd2018-11-12 11:34:43 -0800221
222 do_introspect(system_bus, transaction, interface_map,
223 objectServer, parent_path + "/" + child_path);
Ed Tanous60520632018-06-11 17:46:52 -0700224 }
Ed Tanous50232cd2018-11-12 11:34:43 -0800225 pElement = pElement->NextSiblingElement("node");
Ed Tanous60520632018-06-11 17:46:52 -0700226 }
227 },
228 transaction->process_name, path, "org.freedesktop.DBus.Introspectable",
229 "Introspect");
230}
231
Ed Tanous60520632018-06-11 17:46:52 -0700232void start_new_introspect(
233 sdbusplus::asio::connection* system_bus, boost::asio::io_service& io,
234 interface_map_type& interface_map, const std::string& process_name,
Matt Spinleraecabe82018-09-19 13:25:42 -0500235#ifdef DEBUG
Ed Tanous60520632018-06-11 17:46:52 -0700236 std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>>
237 global_start_time,
Matt Spinleraecabe82018-09-19 13:25:42 -0500238#endif
Ed Tanous60520632018-06-11 17:46:52 -0700239 sdbusplus::asio::object_server& objectServer)
240{
Andrew Geissler82815da2019-02-04 12:19:41 -0600241 if (needToIntrospect(process_name, service_whitelist, service_blacklist))
Ed Tanous60520632018-06-11 17:46:52 -0700242 {
Ed Tanous60520632018-06-11 17:46:52 -0700243 std::shared_ptr<InProgressIntrospect> transaction =
Matt Spinleraecabe82018-09-19 13:25:42 -0500244 std::make_shared<InProgressIntrospect>(system_bus, io, process_name
245#ifdef DEBUG
246 ,
247 global_start_time
248#endif
249 );
Ed Tanous60520632018-06-11 17:46:52 -0700250
251 do_introspect(system_bus, transaction, interface_map, objectServer,
252 "/");
253 }
254}
255
256// TODO(ed) replace with std::set_intersection once c++17 is available
257template <class InputIt1, class InputIt2>
258bool intersect(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2)
259{
260 while (first1 != last1 && first2 != last2)
261 {
262 if (*first1 < *first2)
263 {
264 ++first1;
265 continue;
266 }
267 if (*first2 < *first1)
268 {
269 ++first2;
270 continue;
271 }
272 return true;
273 }
274 return false;
275}
276
277void doListNames(
278 boost::asio::io_service& io, interface_map_type& interface_map,
279 sdbusplus::asio::connection* system_bus,
280 boost::container::flat_map<std::string, std::string>& name_owners,
281 sdbusplus::asio::object_server& objectServer)
282{
283 system_bus->async_method_call(
284 [&io, &interface_map, &name_owners, &objectServer,
285 system_bus](const boost::system::error_code ec,
286 std::vector<std::string> process_names) {
287 if (ec)
288 {
289 std::cerr << "Error getting names: " << ec << "\n";
290 std::exit(EXIT_FAILURE);
291 return;
292 }
Ed Tanous60520632018-06-11 17:46:52 -0700293 // Try to make startup consistent
294 std::sort(process_names.begin(), process_names.end());
Matt Spinleraecabe82018-09-19 13:25:42 -0500295#ifdef DEBUG
Ed Tanous60520632018-06-11 17:46:52 -0700296 std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>>
297 global_start_time = std::make_shared<
298 std::chrono::time_point<std::chrono::steady_clock>>(
299 std::chrono::steady_clock::now());
Matt Spinleraecabe82018-09-19 13:25:42 -0500300#endif
Ed Tanous60520632018-06-11 17:46:52 -0700301 for (const std::string& process_name : process_names)
302 {
Andrew Geissler82815da2019-02-04 12:19:41 -0600303 if (needToIntrospect(process_name, service_whitelist,
304 service_blacklist))
Ed Tanous60520632018-06-11 17:46:52 -0700305 {
306 start_new_introspect(system_bus, io, interface_map,
Matt Spinleraecabe82018-09-19 13:25:42 -0500307 process_name,
308#ifdef DEBUG
309 global_start_time,
310#endif
Ed Tanous60520632018-06-11 17:46:52 -0700311 objectServer);
312 update_owners(system_bus, name_owners, process_name);
313 }
314 }
315 },
316 "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus",
317 "ListNames");
318}
319
Matt Spinlerdd945862018-09-07 12:41:05 -0500320void splitArgs(const std::string& stringArgs,
321 boost::container::flat_set<std::string>& listArgs)
322{
323 std::istringstream args;
324 std::string arg;
325
326 args.str(stringArgs);
327
328 while (!args.eof())
329 {
330 args >> arg;
331 if (!arg.empty())
332 {
333 listArgs.insert(arg);
334 }
335 }
336}
337
Matt Spinler47c09752018-11-29 14:54:13 -0600338void addObjectMapResult(
339 std::vector<interface_map_type::value_type>& objectMap,
Matt Spinler9f0958e2018-09-11 08:26:10 -0500340 const std::string& objectPath,
341 const std::pair<std::string, boost::container::flat_set<std::string>>&
342 interfaceMap)
343{
344 // Adds an object path/service name/interface list entry to
Matt Spinler47c09752018-11-29 14:54:13 -0600345 // the results of GetSubTree and GetAncestors.
Matt Spinler9f0958e2018-09-11 08:26:10 -0500346 // If an entry for the object path already exists, just add the
347 // service name and interfaces to that entry, otherwise create
348 // a new entry.
349 auto entry = std::find_if(
Matt Spinler47c09752018-11-29 14:54:13 -0600350 objectMap.begin(), objectMap.end(),
Matt Spinler9f0958e2018-09-11 08:26:10 -0500351 [&objectPath](const auto& i) { return objectPath == i.first; });
352
Matt Spinler47c09752018-11-29 14:54:13 -0600353 if (entry != objectMap.end())
Matt Spinler9f0958e2018-09-11 08:26:10 -0500354 {
355 entry->second.emplace(interfaceMap);
356 }
357 else
358 {
359 interface_map_type::value_type object;
360 object.first = objectPath;
361 object.second.emplace(interfaceMap);
Matt Spinler47c09752018-11-29 14:54:13 -0600362 objectMap.push_back(object);
Matt Spinler9f0958e2018-09-11 08:26:10 -0500363 }
364}
365
Matt Spinlera82779f2019-01-09 12:39:42 -0600366// Remove parents of the passed in path that:
367// 1) Only have the 3 default interfaces on them
368// - Means D-Bus created these, not application code,
369// with the Properties, Introspectable, and Peer ifaces
370// 2) Have no other child for this owner
371void removeUnneededParents(const std::string& objectPath,
372 const std::string& owner,
373 interface_map_type& interface_map)
374{
375 auto parent = objectPath;
376
377 while (true)
378 {
379 auto pos = parent.find_last_of('/');
380 if ((pos == std::string::npos) || (pos == 0))
381 {
382 break;
383 }
384 parent = parent.substr(0, pos);
385
386 auto parent_it = interface_map.find(parent);
387 if (parent_it == interface_map.end())
388 {
389 break;
390 }
391
392 auto ifaces_it = parent_it->second.find(owner);
393 if (ifaces_it == parent_it->second.end())
394 {
395 break;
396 }
397
398 if (ifaces_it->second.size() != 3)
399 {
400 break;
401 }
402
403 auto child_path = parent + '/';
404
405 // Remove this parent if there isn't a remaining child on this owner
406 auto child = std::find_if(
407 interface_map.begin(), interface_map.end(),
408 [&owner, &child_path](const auto& entry) {
409 return boost::starts_with(entry.first, child_path) &&
410 (entry.second.find(owner) != entry.second.end());
411 });
412
413 if (child == interface_map.end())
414 {
415 parent_it->second.erase(ifaces_it);
416 if (parent_it->second.empty())
417 {
418 interface_map.erase(parent_it);
419 }
420 }
421 else
422 {
423 break;
424 }
425 }
426}
427
Ed Tanous60520632018-06-11 17:46:52 -0700428int main(int argc, char** argv)
429{
Matt Spinlerdd945862018-09-07 12:41:05 -0500430 auto options = ArgumentParser(argc, argv);
Ed Tanous60520632018-06-11 17:46:52 -0700431 boost::asio::io_service io;
432 std::shared_ptr<sdbusplus::asio::connection> system_bus =
433 std::make_shared<sdbusplus::asio::connection>(io);
434
Matt Spinlerdd945862018-09-07 12:41:05 -0500435 splitArgs(options["service-namespaces"], service_whitelist);
Matt Spinlerdd945862018-09-07 12:41:05 -0500436 splitArgs(options["service-blacklists"], service_blacklist);
437
Ed Tanousd4dd96a2018-11-12 11:37:44 -0800438 // TODO(Ed) Remove this once all service files are updated to not use this.
439 // For now, simply squash the input, and ignore it.
440 boost::container::flat_set<std::string> iface_whitelist;
441 splitArgs(options["interface-namespaces"], iface_whitelist);
442
Ed Tanous60520632018-06-11 17:46:52 -0700443 system_bus->request_name(OBJECT_MAPPER_DBUS_NAME);
444 sdbusplus::asio::object_server server(system_bus);
445
446 // Construct a signal set registered for process termination.
447 boost::asio::signal_set signals(io, SIGINT, SIGTERM);
448 signals.async_wait([&io](const boost::system::error_code& error,
449 int signal_number) { io.stop(); });
450
451 interface_map_type interface_map;
452 boost::container::flat_map<std::string, std::string> name_owners;
453
454 std::function<void(sdbusplus::message::message & message)>
455 nameChangeHandler = [&interface_map, &io, &name_owners, &server,
456 system_bus](sdbusplus::message::message& message) {
Andrew Geissler20679262019-02-11 20:20:40 -0600457 std::string name; // well-known
458 std::string old_owner; // unique-name
459 std::string new_owner; // unique-name
Ed Tanous60520632018-06-11 17:46:52 -0700460
461 message.read(name, old_owner, new_owner);
462
463 if (!old_owner.empty())
464 {
Andrew Geissler20679262019-02-11 20:20:40 -0600465 processNameChangeDelete(name_owners, name, old_owner,
466 interface_map, associationOwners,
467 associationInterfaces, server);
Ed Tanous60520632018-06-11 17:46:52 -0700468 }
469
470 if (!new_owner.empty())
471 {
Matt Spinleraecabe82018-09-19 13:25:42 -0500472#ifdef DEBUG
Ed Tanous60520632018-06-11 17:46:52 -0700473 auto transaction = std::make_shared<
474 std::chrono::time_point<std::chrono::steady_clock>>(
475 std::chrono::steady_clock::now());
Matt Spinleraecabe82018-09-19 13:25:42 -0500476#endif
Ed Tanous60520632018-06-11 17:46:52 -0700477 // New daemon added
Andrew Geissler82815da2019-02-04 12:19:41 -0600478 if (needToIntrospect(name, service_whitelist,
479 service_blacklist))
Ed Tanous60520632018-06-11 17:46:52 -0700480 {
481 name_owners[new_owner] = name;
482 start_new_introspect(system_bus.get(), io, interface_map,
Matt Spinleraecabe82018-09-19 13:25:42 -0500483 name,
484#ifdef DEBUG
485 transaction,
486#endif
487 server);
Ed Tanous60520632018-06-11 17:46:52 -0700488 }
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;
500 std::vector<std::pair<
501 std::string, std::vector<std::pair<
502 std::string, sdbusplus::message::variant<
503 std::vector<Association>>>>>>
504 interfaces_added;
505 message.read(obj_path, interfaces_added);
506 std::string well_known;
Andrew Geissler3b025e62019-02-01 10:33:54 -0600507 if (!getWellKnown(name_owners, message.get_sender(), well_known))
Ed Tanous60520632018-06-11 17:46:52 -0700508 {
509 return; // only introspect well-known
510 }
Andrew Geissler82815da2019-02-04 12:19:41 -0600511 if (needToIntrospect(well_known, service_whitelist,
512 service_blacklist))
Ed Tanous60520632018-06-11 17:46:52 -0700513 {
514 auto& iface_list = interface_map[obj_path.str];
515
Matt Spinlercc74e4b2018-09-18 16:21:54 -0500516 for (const auto& interface_pair : interfaces_added)
Ed Tanous60520632018-06-11 17:46:52 -0700517 {
518 iface_list[well_known].emplace(interface_pair.first);
519
520 if (interface_pair.first == ASSOCIATIONS_INTERFACE)
521 {
522 const sdbusplus::message::variant<
523 std::vector<Association>>* variantAssociations =
524 nullptr;
525 for (const auto& interface : interface_pair.second)
526 {
527 if (interface.first == "associations")
528 {
529 variantAssociations = &(interface.second);
530 }
531 }
532 if (variantAssociations == nullptr)
533 {
534 std::cerr << "Illegal association found on "
535 << well_known << "\n";
536 continue;
537 }
538 std::vector<Association> associations =
539 sdbusplus::message::variant_ns::get<
540 std::vector<Association>>(*variantAssociations);
Matt Spinler09be5762019-01-23 14:28:17 -0600541 associationChanged(server, associations, obj_path.str,
Andrew Geissler4511b332019-02-21 15:40:40 -0600542 well_known, associationOwners,
543 associationInterfaces);
Ed Tanous60520632018-06-11 17:46:52 -0700544 }
545 }
Matt Spinler1b4a5022019-01-03 14:48:33 -0600546
547 // To handle the case where an object path is being created
548 // with 2 or more new path segments, check if the parent paths
549 // of this path are already in the interface map, and add them
550 // if they aren't with just the default freedesktop interfaces.
551 // This would be done via introspection if they would have
552 // already existed at startup. While we could also introspect
553 // them now to do the work, we know there aren't any other
554 // interfaces or we would have gotten signals for them as well,
555 // so take a shortcut to speed things up.
556 //
557 // This is all needed so that mapper operations can be done
558 // on the new parent paths.
559 using iface_map_iterator = interface_map_type::iterator;
560 using iface_map_value_type = boost::container::flat_map<
561 std::string, boost::container::flat_set<std::string>>;
562 using name_map_iterator = iface_map_value_type::iterator;
563
564 static const boost::container::flat_set<std::string>
565 default_ifaces{"org.freedesktop.DBus.Introspectable",
566 "org.freedesktop.DBus.Peer",
567 "org.freedesktop.DBus.Properties"};
568
569 std::string parent = obj_path.str;
570 auto pos = parent.find_last_of('/');
571
572 while (pos != std::string::npos)
573 {
574 parent = parent.substr(0, pos);
575
576 std::pair<iface_map_iterator, bool> parentEntry =
577 interface_map.insert(
578 std::make_pair(parent, iface_map_value_type{}));
579
580 std::pair<name_map_iterator, bool> ifaceEntry =
581 parentEntry.first->second.insert(
582 std::make_pair(well_known, default_ifaces));
583
584 if (!ifaceEntry.second)
585 {
586 // Entry was already there for this name so done.
587 break;
588 }
589
590 pos = parent.find_last_of('/');
591 }
Ed Tanous60520632018-06-11 17:46:52 -0700592 }
593 };
594
595 sdbusplus::bus::match::match interfacesAdded(
596 static_cast<sdbusplus::bus::bus&>(*system_bus),
597 sdbusplus::bus::match::rules::interfacesAdded(),
598 interfacesAddedHandler);
599
600 std::function<void(sdbusplus::message::message & message)>
601 interfacesRemovedHandler = [&interface_map, &name_owners, &server](
602 sdbusplus::message::message& message) {
603 sdbusplus::message::object_path obj_path;
604 std::vector<std::string> interfaces_removed;
605 message.read(obj_path, interfaces_removed);
606 auto connection_map = interface_map.find(obj_path.str);
607 if (connection_map == interface_map.end())
608 {
609 return;
610 }
611
612 std::string sender;
Andrew Geissler3b025e62019-02-01 10:33:54 -0600613 if (!getWellKnown(name_owners, message.get_sender(), sender))
Ed Tanous60520632018-06-11 17:46:52 -0700614 {
615 return;
616 }
617 for (const std::string& interface : interfaces_removed)
618 {
619 auto interface_set = connection_map->second.find(sender);
620 if (interface_set == connection_map->second.end())
621 {
622 continue;
623 }
624
625 if (interface == ASSOCIATIONS_INTERFACE)
626 {
Andrew Geissler67e5a422019-02-04 13:00:59 -0600627 removeAssociation(obj_path.str, sender, server,
628 associationOwners, associationInterfaces);
Ed Tanous60520632018-06-11 17:46:52 -0700629 }
630
631 interface_set->second.erase(interface);
632 // If this was the last interface on this connection,
633 // erase the connection
634 if (interface_set->second.empty())
635 {
636 connection_map->second.erase(interface_set);
637 }
638 }
639 // If this was the last connection on this object path,
640 // erase the object path
641 if (connection_map->second.empty())
642 {
643 interface_map.erase(connection_map);
644 }
Matt Spinlera82779f2019-01-09 12:39:42 -0600645
646 removeUnneededParents(obj_path.str, sender, interface_map);
Ed Tanous60520632018-06-11 17:46:52 -0700647 };
648
649 sdbusplus::bus::match::match interfacesRemoved(
650 static_cast<sdbusplus::bus::bus&>(*system_bus),
651 sdbusplus::bus::match::rules::interfacesRemoved(),
652 interfacesRemovedHandler);
653
654 std::function<void(sdbusplus::message::message & message)>
655 associationChangedHandler =
Matt Spinler937a2322019-01-23 13:54:22 -0600656 [&server, &name_owners](sdbusplus::message::message& message) {
Ed Tanous60520632018-06-11 17:46:52 -0700657 std::string objectName;
658 boost::container::flat_map<
659 std::string,
660 sdbusplus::message::variant<std::vector<Association>>>
661 values;
662 message.read(objectName, values);
663 auto findAssociations = values.find("associations");
664 if (findAssociations != values.end())
665 {
666 std::vector<Association> associations =
667 sdbusplus::message::variant_ns::get<
668 std::vector<Association>>(findAssociations->second);
Matt Spinler937a2322019-01-23 13:54:22 -0600669
670 std::string well_known;
Andrew Geissler3b025e62019-02-01 10:33:54 -0600671 if (!getWellKnown(name_owners, message.get_sender(),
672 well_known))
Matt Spinler937a2322019-01-23 13:54:22 -0600673 {
674 return;
675 }
Matt Spinler09be5762019-01-23 14:28:17 -0600676 associationChanged(server, associations, message.get_path(),
Andrew Geissler4511b332019-02-21 15:40:40 -0600677 well_known, associationOwners,
678 associationInterfaces);
Ed Tanous60520632018-06-11 17:46:52 -0700679 }
680 };
681 sdbusplus::bus::match::match associationChanged(
682 static_cast<sdbusplus::bus::bus&>(*system_bus),
683 sdbusplus::bus::match::rules::interface(
684 "org.freedesktop.DBus.Properties") +
685 sdbusplus::bus::match::rules::member("PropertiesChanged") +
686 sdbusplus::bus::match::rules::argN(0, ASSOCIATIONS_INTERFACE),
687 associationChangedHandler);
688
689 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
690 server.add_interface("/xyz/openbmc_project/object_mapper",
691 "xyz.openbmc_project.ObjectMapper");
692
693 iface->register_method(
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600694 "GetAncestors", [&interface_map](std::string& req_path,
Ed Tanous60520632018-06-11 17:46:52 -0700695 std::vector<std::string>& interfaces) {
696 // Interfaces need to be sorted for intersect to function
697 std::sort(interfaces.begin(), interfaces.end());
698
James Feistd7322872019-01-28 11:25:00 -0800699 if (boost::ends_with(req_path, "/"))
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600700 {
James Feistd7322872019-01-28 11:25:00 -0800701 req_path.pop_back();
702 }
703 if (req_path.size() &&
704 interface_map.find(req_path) == interface_map.end())
705 {
706 throw NotFoundException();
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600707 }
708
Ed Tanous60520632018-06-11 17:46:52 -0700709 std::vector<interface_map_type::value_type> ret;
710 for (auto& object_path : interface_map)
711 {
712 auto& this_path = object_path.first;
Matt Spinlerea6516c2018-09-25 16:23:13 -0500713 if (boost::starts_with(req_path, this_path) &&
714 (req_path != this_path))
Ed Tanous60520632018-06-11 17:46:52 -0700715 {
716 if (interfaces.empty())
717 {
718 ret.emplace_back(object_path);
719 }
720 else
721 {
722 for (auto& interface_map : object_path.second)
723 {
724
725 if (intersect(interfaces.begin(), interfaces.end(),
726 interface_map.second.begin(),
727 interface_map.second.end()))
728 {
Matt Spinler47c09752018-11-29 14:54:13 -0600729 addObjectMapResult(ret, this_path,
730 interface_map);
Ed Tanous60520632018-06-11 17:46:52 -0700731 }
732 }
733 }
734 }
735 }
Ed Tanous60520632018-06-11 17:46:52 -0700736
737 return ret;
738 });
739
740 iface->register_method(
741 "GetObject", [&interface_map](const std::string& path,
742 std::vector<std::string>& interfaces) {
Matt Spinler7a38d412018-09-18 16:13:25 -0500743 boost::container::flat_map<std::string,
744 boost::container::flat_set<std::string>>
745 results;
746
Ed Tanous60520632018-06-11 17:46:52 -0700747 // Interfaces need to be sorted for intersect to function
748 std::sort(interfaces.begin(), interfaces.end());
749 auto path_ref = interface_map.find(path);
750 if (path_ref == interface_map.end())
751 {
752 throw NotFoundException();
753 }
754 if (interfaces.empty())
755 {
756 return path_ref->second;
757 }
758 for (auto& interface_map : path_ref->second)
759 {
760 if (intersect(interfaces.begin(), interfaces.end(),
761 interface_map.second.begin(),
762 interface_map.second.end()))
763 {
Matt Spinler7a38d412018-09-18 16:13:25 -0500764 results.emplace(interface_map.first, interface_map.second);
Ed Tanous60520632018-06-11 17:46:52 -0700765 }
766 }
Matt Spinler7a38d412018-09-18 16:13:25 -0500767
768 if (results.empty())
769 {
770 throw NotFoundException();
771 }
772
773 return results;
Ed Tanous60520632018-06-11 17:46:52 -0700774 });
775
776 iface->register_method(
Matt Spinler153494e2018-11-07 16:35:40 -0600777 "GetSubTree", [&interface_map](std::string& req_path, int32_t depth,
778 std::vector<std::string>& interfaces) {
Ed Tanous60520632018-06-11 17:46:52 -0700779 if (depth <= 0)
780 {
781 depth = std::numeric_limits<int32_t>::max();
782 }
783 // Interfaces need to be sorted for intersect to function
784 std::sort(interfaces.begin(), interfaces.end());
785 std::vector<interface_map_type::value_type> ret;
786
James Feistd7322872019-01-28 11:25:00 -0800787 if (boost::ends_with(req_path, "/"))
Matt Spinler153494e2018-11-07 16:35:40 -0600788 {
James Feistd7322872019-01-28 11:25:00 -0800789 req_path.pop_back();
790 }
791 if (req_path.size() &&
792 interface_map.find(req_path) == interface_map.end())
793 {
794 throw NotFoundException();
Matt Spinler153494e2018-11-07 16:35:40 -0600795 }
796
Ed Tanous60520632018-06-11 17:46:52 -0700797 for (auto& object_path : interface_map)
798 {
799 auto& this_path = object_path.first;
Matt Spinler153494e2018-11-07 16:35:40 -0600800
801 if (this_path == req_path)
802 {
803 continue;
804 }
805
Ed Tanous60520632018-06-11 17:46:52 -0700806 if (boost::starts_with(this_path, req_path))
807 {
808 // count the number of slashes past the search term
809 int32_t this_depth =
810 std::count(this_path.begin() + req_path.size(),
811 this_path.end(), '/');
812 if (this_depth <= depth)
813 {
Ed Tanous60520632018-06-11 17:46:52 -0700814 for (auto& interface_map : object_path.second)
815 {
816 if (intersect(interfaces.begin(), interfaces.end(),
817 interface_map.second.begin(),
Matt Spinler9f0958e2018-09-11 08:26:10 -0500818 interface_map.second.end()) ||
819 interfaces.empty())
Ed Tanous60520632018-06-11 17:46:52 -0700820 {
Matt Spinler47c09752018-11-29 14:54:13 -0600821 addObjectMapResult(ret, this_path,
822 interface_map);
Ed Tanous60520632018-06-11 17:46:52 -0700823 }
824 }
Ed Tanous60520632018-06-11 17:46:52 -0700825 }
826 }
827 }
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600828
Ed Tanous60520632018-06-11 17:46:52 -0700829 return ret;
830 });
831
832 iface->register_method(
833 "GetSubTreePaths",
Matt Spinler153494e2018-11-07 16:35:40 -0600834 [&interface_map](std::string& req_path, int32_t depth,
Ed Tanous60520632018-06-11 17:46:52 -0700835 std::vector<std::string>& interfaces) {
836 if (depth <= 0)
837 {
838 depth = std::numeric_limits<int32_t>::max();
839 }
840 // Interfaces need to be sorted for intersect to function
841 std::sort(interfaces.begin(), interfaces.end());
842 std::vector<std::string> ret;
Matt Spinler153494e2018-11-07 16:35:40 -0600843
James Feistd7322872019-01-28 11:25:00 -0800844 if (boost::ends_with(req_path, "/"))
Matt Spinler153494e2018-11-07 16:35:40 -0600845 {
James Feistd7322872019-01-28 11:25:00 -0800846 req_path.pop_back();
847 }
848 if (req_path.size() &&
849 interface_map.find(req_path) == interface_map.end())
850 {
851 throw NotFoundException();
Matt Spinler153494e2018-11-07 16:35:40 -0600852 }
853
Ed Tanous60520632018-06-11 17:46:52 -0700854 for (auto& object_path : interface_map)
855 {
856 auto& this_path = object_path.first;
Matt Spinler153494e2018-11-07 16:35:40 -0600857
858 if (this_path == req_path)
859 {
860 continue;
861 }
862
Ed Tanous60520632018-06-11 17:46:52 -0700863 if (boost::starts_with(this_path, req_path))
864 {
865 // count the number of slashes past the search term
866 int this_depth =
867 std::count(this_path.begin() + req_path.size(),
868 this_path.end(), '/');
869 if (this_depth <= depth)
870 {
871 bool add = interfaces.empty();
872 for (auto& interface_map : object_path.second)
873 {
874 if (intersect(interfaces.begin(), interfaces.end(),
875 interface_map.second.begin(),
876 interface_map.second.end()))
877 {
878 add = true;
879 break;
880 }
881 }
882 if (add)
883 {
884 // TODO(ed) this is a copy
885 ret.emplace_back(this_path);
886 }
887 }
888 }
889 }
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600890
Ed Tanous60520632018-06-11 17:46:52 -0700891 return ret;
892 });
893
894 iface->initialize();
895
896 io.post([&]() {
897 doListNames(io, interface_map, system_bus.get(), name_owners, server);
898 });
899
Ed Tanous60520632018-06-11 17:46:52 -0700900 io.run();
901}