blob: 84d5b676c1d4fafbdbe03fd843523f6b3efa54b5 [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"
Matt Spinler35396c12019-04-05 11:46:57 -05004#include "types.hpp"
Matt Spinlerdd945862018-09-07 12:41:05 -05005
Ed Tanous60520632018-06-11 17:46:52 -07006#include <tinyxml2.h>
7
8#include <atomic>
Matt Spinler8f876a52019-04-15 13:22:50 -05009#include <boost/algorithm/string/case_conv.hpp>
Ed Tanous60520632018-06-11 17:46:52 -070010#include <boost/algorithm/string/predicate.hpp>
11#include <boost/container/flat_map.hpp>
Ed Tanous60520632018-06-11 17:46:52 -070012#include <chrono>
13#include <iomanip>
14#include <iostream>
15#include <sdbusplus/asio/connection.hpp>
16#include <sdbusplus/asio/object_server.hpp>
17
18constexpr const char* OBJECT_MAPPER_DBUS_NAME =
19 "xyz.openbmc_project.ObjectMapper";
Ed Tanous60520632018-06-11 17:46:52 -070020
Matt Spinlere2359fb2019-04-05 14:11:33 -050021AssociationMaps associationMaps;
Matt Spinler937a2322019-01-23 13:54:22 -060022
Andrew Geissler82815da2019-02-04 12:19:41 -060023static WhiteBlackList service_whitelist;
24static WhiteBlackList service_blacklist;
Matt Spinlerdd945862018-09-07 12:41:05 -050025
Ed Tanous60520632018-06-11 17:46:52 -070026/** Exception thrown when a path is not found in the object list. */
27struct NotFoundException final : public sdbusplus::exception_t
28{
29 const char* name() const noexcept override
30 {
31 return "org.freedesktop.DBus.Error.FileNotFound";
32 };
33 const char* description() const noexcept override
34 {
35 return "path or object not found";
36 };
37 const char* what() const noexcept override
38 {
39 return "org.freedesktop.DBus.Error.FileNotFound: "
40 "The requested object was not found";
41 };
42};
43
Ed Tanous60520632018-06-11 17:46:52 -070044void 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
67void 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
80struct InProgressIntrospect
81{
82 InProgressIntrospect(
83 sdbusplus::asio::connection* system_bus, boost::asio::io_service& io,
Matt Spinler11401e22019-04-08 13:13:25 -050084 const std::string& process_name, AssociationMaps& am
Matt Spinleraecabe82018-09-19 13:25:42 -050085#ifdef DEBUG
86 ,
Ed Tanous60520632018-06-11 17:46:52 -070087 std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>>
Matt Spinleraecabe82018-09-19 13:25:42 -050088 global_start_time
89#endif
90 ) :
Ed Tanous60520632018-06-11 17:46:52 -070091 system_bus(system_bus),
Matt Spinler11401e22019-04-08 13:13:25 -050092 io(io), process_name(process_name), assocMaps(am)
Matt Spinleraecabe82018-09-19 13:25:42 -050093#ifdef DEBUG
94 ,
Ed Tanous60520632018-06-11 17:46:52 -070095 global_start_time(global_start_time),
96 process_start_time(std::chrono::steady_clock::now())
Matt Spinleraecabe82018-09-19 13:25:42 -050097#endif
Ed Tanous60520632018-06-11 17:46:52 -070098 {
99 }
100 ~InProgressIntrospect()
101 {
102 send_introspection_complete_signal(system_bus, process_name);
Matt Spinleraecabe82018-09-19 13:25:42 -0500103
104#ifdef DEBUG
Ed Tanous60520632018-06-11 17:46:52 -0700105 std::chrono::duration<float> diff =
106 std::chrono::steady_clock::now() - process_start_time;
107 std::cout << std::setw(50) << process_name << " scan took "
108 << diff.count() << " seconds\n";
109
110 // If we're the last outstanding caller globally, calculate the
111 // time it took
112 if (global_start_time != nullptr && global_start_time.use_count() == 1)
113 {
114 diff = std::chrono::steady_clock::now() - *global_start_time;
115 std::cout << "Total scan took " << diff.count()
116 << " seconds to complete\n";
117 }
Matt Spinleraecabe82018-09-19 13:25:42 -0500118#endif
Ed Tanous60520632018-06-11 17:46:52 -0700119 }
120 sdbusplus::asio::connection* system_bus;
121 boost::asio::io_service& io;
122 std::string process_name;
Matt Spinler11401e22019-04-08 13:13:25 -0500123 AssociationMaps& assocMaps;
Matt Spinleraecabe82018-09-19 13:25:42 -0500124#ifdef DEBUG
Ed Tanous60520632018-06-11 17:46:52 -0700125 std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>>
126 global_start_time;
127 std::chrono::time_point<std::chrono::steady_clock> process_start_time;
Matt Spinleraecabe82018-09-19 13:25:42 -0500128#endif
Ed Tanous60520632018-06-11 17:46:52 -0700129};
130
Ed Tanous60520632018-06-11 17:46:52 -0700131void do_associations(sdbusplus::asio::connection* system_bus,
Matt Spinlere0b0e3a2019-04-08 10:39:23 -0500132 interface_map_type& interfaceMap,
Ed Tanous60520632018-06-11 17:46:52 -0700133 sdbusplus::asio::object_server& objectServer,
Matt Spinler8f876a52019-04-15 13:22:50 -0500134 const std::string& processName, const std::string& path,
135 const std::string& assocDefIface)
Ed Tanous60520632018-06-11 17:46:52 -0700136{
137 system_bus->async_method_call(
Matt Spinlere0b0e3a2019-04-08 10:39:23 -0500138 [&objectServer, path, processName, &interfaceMap](
Matt Spinler937a2322019-01-23 13:54:22 -0600139 const boost::system::error_code ec,
140 const sdbusplus::message::variant<std::vector<Association>>&
141 variantAssociations) {
Ed Tanous60520632018-06-11 17:46:52 -0700142 if (ec)
143 {
144 std::cerr << "Error getting associations from " << path << "\n";
145 }
146 std::vector<Association> associations =
147 sdbusplus::message::variant_ns::get<std::vector<Association>>(
148 variantAssociations);
Andrew Geissler4511b332019-02-21 15:40:40 -0600149 associationChanged(objectServer, associations, path, processName,
Matt Spinlere0b0e3a2019-04-08 10:39:23 -0500150 interfaceMap, associationMaps);
Ed Tanous60520632018-06-11 17:46:52 -0700151 },
152 processName, path, "org.freedesktop.DBus.Properties", "Get",
Matt Spinler8f876a52019-04-15 13:22:50 -0500153 assocDefIface, getAssocDefPropName(assocDefIface));
Ed Tanous60520632018-06-11 17:46:52 -0700154}
155
156void do_introspect(sdbusplus::asio::connection* system_bus,
157 std::shared_ptr<InProgressIntrospect> transaction,
158 interface_map_type& interface_map,
159 sdbusplus::asio::object_server& objectServer,
160 std::string path)
161{
162 system_bus->async_method_call(
163 [&interface_map, &objectServer, transaction, path,
164 system_bus](const boost::system::error_code ec,
165 const std::string& introspect_xml) {
166 if (ec)
167 {
168 std::cerr << "Introspect call failed with error: " << ec << ", "
169 << ec.message()
170 << " on process: " << transaction->process_name
171 << " path: " << path << "\n";
172 return;
173 }
174
175 tinyxml2::XMLDocument doc;
176
177 tinyxml2::XMLError e = doc.Parse(introspect_xml.c_str());
178 if (e != tinyxml2::XMLError::XML_SUCCESS)
179 {
180 std::cerr << "XML parsing failed\n";
181 return;
182 }
183
184 tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node");
185 if (pRoot == nullptr)
186 {
187 std::cerr << "XML document did not contain any data\n";
188 return;
189 }
190 auto& thisPathMap = interface_map[path];
Ed Tanous60520632018-06-11 17:46:52 -0700191 tinyxml2::XMLElement* pElement =
192 pRoot->FirstChildElement("interface");
193 while (pElement != nullptr)
194 {
195 const char* iface_name = pElement->Attribute("name");
196 if (iface_name == nullptr)
197 {
198 continue;
199 }
200
Ed Tanousd4dd96a2018-11-12 11:37:44 -0800201 thisPathMap[transaction->process_name].emplace(iface_name);
202
Matt Spinler8f876a52019-04-15 13:22:50 -0500203 if (isAssocDefIface(iface_name))
Ed Tanous60520632018-06-11 17:46:52 -0700204 {
Matt Spinlere0b0e3a2019-04-08 10:39:23 -0500205 do_associations(system_bus, interface_map, objectServer,
Matt Spinler8f876a52019-04-15 13:22:50 -0500206 transaction->process_name, path,
207 iface_name);
Ed Tanous60520632018-06-11 17:46:52 -0700208 }
Ed Tanous60520632018-06-11 17:46:52 -0700209
210 pElement = pElement->NextSiblingElement("interface");
211 }
212
Matt Spinler11401e22019-04-08 13:13:25 -0500213 // Check if this new path has a pending association that can
214 // now be completed.
215 checkIfPendingAssociation(path, interface_map,
216 transaction->assocMaps, objectServer);
217
Ed Tanous50232cd2018-11-12 11:34:43 -0800218 pElement = pRoot->FirstChildElement("node");
219 while (pElement != nullptr)
Ed Tanous60520632018-06-11 17:46:52 -0700220 {
Ed Tanous50232cd2018-11-12 11:34:43 -0800221 const char* child_path = pElement->Attribute("name");
222 if (child_path != nullptr)
Ed Tanous60520632018-06-11 17:46:52 -0700223 {
Ed Tanous50232cd2018-11-12 11:34:43 -0800224 std::string parent_path(path);
225 if (parent_path == "/")
Ed Tanous60520632018-06-11 17:46:52 -0700226 {
Ed Tanous50232cd2018-11-12 11:34:43 -0800227 parent_path.clear();
Ed Tanous60520632018-06-11 17:46:52 -0700228 }
Ed Tanous50232cd2018-11-12 11:34:43 -0800229
230 do_introspect(system_bus, transaction, interface_map,
231 objectServer, parent_path + "/" + child_path);
Ed Tanous60520632018-06-11 17:46:52 -0700232 }
Ed Tanous50232cd2018-11-12 11:34:43 -0800233 pElement = pElement->NextSiblingElement("node");
Ed Tanous60520632018-06-11 17:46:52 -0700234 }
235 },
236 transaction->process_name, path, "org.freedesktop.DBus.Introspectable",
237 "Introspect");
238}
239
Ed Tanous60520632018-06-11 17:46:52 -0700240void start_new_introspect(
241 sdbusplus::asio::connection* system_bus, boost::asio::io_service& io,
242 interface_map_type& interface_map, const std::string& process_name,
Matt Spinler11401e22019-04-08 13:13:25 -0500243 AssociationMaps& assocMaps,
Matt Spinleraecabe82018-09-19 13:25:42 -0500244#ifdef DEBUG
Ed Tanous60520632018-06-11 17:46:52 -0700245 std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>>
246 global_start_time,
Matt Spinleraecabe82018-09-19 13:25:42 -0500247#endif
Ed Tanous60520632018-06-11 17:46:52 -0700248 sdbusplus::asio::object_server& objectServer)
249{
Andrew Geissler82815da2019-02-04 12:19:41 -0600250 if (needToIntrospect(process_name, service_whitelist, service_blacklist))
Ed Tanous60520632018-06-11 17:46:52 -0700251 {
Ed Tanous60520632018-06-11 17:46:52 -0700252 std::shared_ptr<InProgressIntrospect> transaction =
Matt Spinler11401e22019-04-08 13:13:25 -0500253 std::make_shared<InProgressIntrospect>(system_bus, io, process_name,
254 assocMaps
Matt Spinleraecabe82018-09-19 13:25:42 -0500255#ifdef DEBUG
256 ,
257 global_start_time
258#endif
259 );
Ed Tanous60520632018-06-11 17:46:52 -0700260
261 do_introspect(system_bus, transaction, interface_map, objectServer,
262 "/");
263 }
264}
265
266// TODO(ed) replace with std::set_intersection once c++17 is available
267template <class InputIt1, class InputIt2>
268bool intersect(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2)
269{
270 while (first1 != last1 && first2 != last2)
271 {
272 if (*first1 < *first2)
273 {
274 ++first1;
275 continue;
276 }
277 if (*first2 < *first1)
278 {
279 ++first2;
280 continue;
281 }
282 return true;
283 }
284 return false;
285}
286
287void doListNames(
288 boost::asio::io_service& io, interface_map_type& interface_map,
289 sdbusplus::asio::connection* system_bus,
290 boost::container::flat_map<std::string, std::string>& name_owners,
Matt Spinler11401e22019-04-08 13:13:25 -0500291 AssociationMaps& assocMaps, sdbusplus::asio::object_server& objectServer)
Ed Tanous60520632018-06-11 17:46:52 -0700292{
293 system_bus->async_method_call(
Matt Spinler11401e22019-04-08 13:13:25 -0500294 [&io, &interface_map, &name_owners, &objectServer, system_bus,
295 &assocMaps](const boost::system::error_code ec,
Ed Tanous60520632018-06-11 17:46:52 -0700296 std::vector<std::string> process_names) {
297 if (ec)
298 {
299 std::cerr << "Error getting names: " << ec << "\n";
300 std::exit(EXIT_FAILURE);
301 return;
302 }
Ed Tanous60520632018-06-11 17:46:52 -0700303 // Try to make startup consistent
304 std::sort(process_names.begin(), process_names.end());
Matt Spinleraecabe82018-09-19 13:25:42 -0500305#ifdef DEBUG
Ed Tanous60520632018-06-11 17:46:52 -0700306 std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>>
307 global_start_time = std::make_shared<
308 std::chrono::time_point<std::chrono::steady_clock>>(
309 std::chrono::steady_clock::now());
Matt Spinleraecabe82018-09-19 13:25:42 -0500310#endif
Ed Tanous60520632018-06-11 17:46:52 -0700311 for (const std::string& process_name : process_names)
312 {
Andrew Geissler82815da2019-02-04 12:19:41 -0600313 if (needToIntrospect(process_name, service_whitelist,
314 service_blacklist))
Ed Tanous60520632018-06-11 17:46:52 -0700315 {
316 start_new_introspect(system_bus, io, interface_map,
Matt Spinler11401e22019-04-08 13:13:25 -0500317 process_name, assocMaps,
Matt Spinleraecabe82018-09-19 13:25:42 -0500318#ifdef DEBUG
319 global_start_time,
320#endif
Ed Tanous60520632018-06-11 17:46:52 -0700321 objectServer);
322 update_owners(system_bus, name_owners, process_name);
323 }
324 }
325 },
326 "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus",
327 "ListNames");
328}
329
Matt Spinlerdd945862018-09-07 12:41:05 -0500330void splitArgs(const std::string& stringArgs,
331 boost::container::flat_set<std::string>& listArgs)
332{
333 std::istringstream args;
334 std::string arg;
335
336 args.str(stringArgs);
337
338 while (!args.eof())
339 {
340 args >> arg;
341 if (!arg.empty())
342 {
343 listArgs.insert(arg);
344 }
345 }
346}
347
Matt Spinler47c09752018-11-29 14:54:13 -0600348void addObjectMapResult(
349 std::vector<interface_map_type::value_type>& objectMap,
Matt Spinler9f0958e2018-09-11 08:26:10 -0500350 const std::string& objectPath,
351 const std::pair<std::string, boost::container::flat_set<std::string>>&
352 interfaceMap)
353{
354 // Adds an object path/service name/interface list entry to
Matt Spinler47c09752018-11-29 14:54:13 -0600355 // the results of GetSubTree and GetAncestors.
Matt Spinler9f0958e2018-09-11 08:26:10 -0500356 // If an entry for the object path already exists, just add the
357 // service name and interfaces to that entry, otherwise create
358 // a new entry.
359 auto entry = std::find_if(
Matt Spinler47c09752018-11-29 14:54:13 -0600360 objectMap.begin(), objectMap.end(),
Matt Spinler9f0958e2018-09-11 08:26:10 -0500361 [&objectPath](const auto& i) { return objectPath == i.first; });
362
Matt Spinler47c09752018-11-29 14:54:13 -0600363 if (entry != objectMap.end())
Matt Spinler9f0958e2018-09-11 08:26:10 -0500364 {
365 entry->second.emplace(interfaceMap);
366 }
367 else
368 {
369 interface_map_type::value_type object;
370 object.first = objectPath;
371 object.second.emplace(interfaceMap);
Matt Spinler47c09752018-11-29 14:54:13 -0600372 objectMap.push_back(object);
Matt Spinler9f0958e2018-09-11 08:26:10 -0500373 }
374}
375
Matt Spinlera82779f2019-01-09 12:39:42 -0600376// Remove parents of the passed in path that:
377// 1) Only have the 3 default interfaces on them
378// - Means D-Bus created these, not application code,
379// with the Properties, Introspectable, and Peer ifaces
380// 2) Have no other child for this owner
381void removeUnneededParents(const std::string& objectPath,
382 const std::string& owner,
383 interface_map_type& interface_map)
384{
385 auto parent = objectPath;
386
387 while (true)
388 {
389 auto pos = parent.find_last_of('/');
390 if ((pos == std::string::npos) || (pos == 0))
391 {
392 break;
393 }
394 parent = parent.substr(0, pos);
395
396 auto parent_it = interface_map.find(parent);
397 if (parent_it == interface_map.end())
398 {
399 break;
400 }
401
402 auto ifaces_it = parent_it->second.find(owner);
403 if (ifaces_it == parent_it->second.end())
404 {
405 break;
406 }
407
408 if (ifaces_it->second.size() != 3)
409 {
410 break;
411 }
412
413 auto child_path = parent + '/';
414
415 // Remove this parent if there isn't a remaining child on this owner
416 auto child = std::find_if(
417 interface_map.begin(), interface_map.end(),
418 [&owner, &child_path](const auto& entry) {
419 return boost::starts_with(entry.first, child_path) &&
420 (entry.second.find(owner) != entry.second.end());
421 });
422
423 if (child == interface_map.end())
424 {
425 parent_it->second.erase(ifaces_it);
426 if (parent_it->second.empty())
427 {
428 interface_map.erase(parent_it);
429 }
430 }
431 else
432 {
433 break;
434 }
435 }
436}
437
Ed Tanous60520632018-06-11 17:46:52 -0700438int main(int argc, char** argv)
439{
Matt Spinlerdd945862018-09-07 12:41:05 -0500440 auto options = ArgumentParser(argc, argv);
Ed Tanous60520632018-06-11 17:46:52 -0700441 boost::asio::io_service io;
442 std::shared_ptr<sdbusplus::asio::connection> system_bus =
443 std::make_shared<sdbusplus::asio::connection>(io);
444
Matt Spinlerdd945862018-09-07 12:41:05 -0500445 splitArgs(options["service-namespaces"], service_whitelist);
Matt Spinlerdd945862018-09-07 12:41:05 -0500446 splitArgs(options["service-blacklists"], service_blacklist);
447
Ed Tanousd4dd96a2018-11-12 11:37:44 -0800448 // TODO(Ed) Remove this once all service files are updated to not use this.
449 // For now, simply squash the input, and ignore it.
450 boost::container::flat_set<std::string> iface_whitelist;
451 splitArgs(options["interface-namespaces"], iface_whitelist);
452
Ed Tanous60520632018-06-11 17:46:52 -0700453 system_bus->request_name(OBJECT_MAPPER_DBUS_NAME);
454 sdbusplus::asio::object_server server(system_bus);
455
456 // Construct a signal set registered for process termination.
457 boost::asio::signal_set signals(io, SIGINT, SIGTERM);
458 signals.async_wait([&io](const boost::system::error_code& error,
459 int signal_number) { io.stop(); });
460
461 interface_map_type interface_map;
462 boost::container::flat_map<std::string, std::string> name_owners;
463
464 std::function<void(sdbusplus::message::message & message)>
465 nameChangeHandler = [&interface_map, &io, &name_owners, &server,
466 system_bus](sdbusplus::message::message& message) {
Andrew Geissler20679262019-02-11 20:20:40 -0600467 std::string name; // well-known
468 std::string old_owner; // unique-name
469 std::string new_owner; // unique-name
Ed Tanous60520632018-06-11 17:46:52 -0700470
471 message.read(name, old_owner, new_owner);
472
473 if (!old_owner.empty())
474 {
Andrew Geissler20679262019-02-11 20:20:40 -0600475 processNameChangeDelete(name_owners, name, old_owner,
Matt Spinlere2359fb2019-04-05 14:11:33 -0500476 interface_map, associationMaps, server);
Ed Tanous60520632018-06-11 17:46:52 -0700477 }
478
479 if (!new_owner.empty())
480 {
Matt Spinleraecabe82018-09-19 13:25:42 -0500481#ifdef DEBUG
Ed Tanous60520632018-06-11 17:46:52 -0700482 auto transaction = std::make_shared<
483 std::chrono::time_point<std::chrono::steady_clock>>(
484 std::chrono::steady_clock::now());
Matt Spinleraecabe82018-09-19 13:25:42 -0500485#endif
Ed Tanous60520632018-06-11 17:46:52 -0700486 // New daemon added
Andrew Geissler82815da2019-02-04 12:19:41 -0600487 if (needToIntrospect(name, service_whitelist,
488 service_blacklist))
Ed Tanous60520632018-06-11 17:46:52 -0700489 {
490 name_owners[new_owner] = name;
491 start_new_introspect(system_bus.get(), io, interface_map,
Matt Spinler11401e22019-04-08 13:13:25 -0500492 name, associationMaps,
Matt Spinleraecabe82018-09-19 13:25:42 -0500493#ifdef DEBUG
494 transaction,
495#endif
496 server);
Ed Tanous60520632018-06-11 17:46:52 -0700497 }
498 }
499 };
500
501 sdbusplus::bus::match::match nameOwnerChanged(
502 static_cast<sdbusplus::bus::bus&>(*system_bus),
503 sdbusplus::bus::match::rules::nameOwnerChanged(), nameChangeHandler);
504
505 std::function<void(sdbusplus::message::message & message)>
506 interfacesAddedHandler = [&interface_map, &name_owners, &server](
507 sdbusplus::message::message& message) {
508 sdbusplus::message::object_path obj_path;
Andrew Geissler70461892019-02-27 09:57:37 -0600509 InterfacesAdded interfaces_added;
Ed Tanous60520632018-06-11 17:46:52 -0700510 message.read(obj_path, interfaces_added);
511 std::string well_known;
Andrew Geissler3b025e62019-02-01 10:33:54 -0600512 if (!getWellKnown(name_owners, message.get_sender(), well_known))
Ed Tanous60520632018-06-11 17:46:52 -0700513 {
514 return; // only introspect well-known
515 }
Andrew Geissler82815da2019-02-04 12:19:41 -0600516 if (needToIntrospect(well_known, service_whitelist,
517 service_blacklist))
Ed Tanous60520632018-06-11 17:46:52 -0700518 {
Andrew Geissler70461892019-02-27 09:57:37 -0600519 processInterfaceAdded(interface_map, obj_path, interfaces_added,
Matt Spinlere2359fb2019-04-05 14:11:33 -0500520 well_known, associationMaps, server);
Ed Tanous60520632018-06-11 17:46:52 -0700521 }
522 };
523
524 sdbusplus::bus::match::match interfacesAdded(
525 static_cast<sdbusplus::bus::bus&>(*system_bus),
526 sdbusplus::bus::match::rules::interfacesAdded(),
527 interfacesAddedHandler);
528
529 std::function<void(sdbusplus::message::message & message)>
530 interfacesRemovedHandler = [&interface_map, &name_owners, &server](
531 sdbusplus::message::message& message) {
532 sdbusplus::message::object_path obj_path;
533 std::vector<std::string> interfaces_removed;
534 message.read(obj_path, interfaces_removed);
535 auto connection_map = interface_map.find(obj_path.str);
536 if (connection_map == interface_map.end())
537 {
538 return;
539 }
540
541 std::string sender;
Andrew Geissler3b025e62019-02-01 10:33:54 -0600542 if (!getWellKnown(name_owners, message.get_sender(), sender))
Ed Tanous60520632018-06-11 17:46:52 -0700543 {
544 return;
545 }
546 for (const std::string& interface : interfaces_removed)
547 {
548 auto interface_set = connection_map->second.find(sender);
549 if (interface_set == connection_map->second.end())
550 {
551 continue;
552 }
553
Matt Spinler8f876a52019-04-15 13:22:50 -0500554 if (isAssocDefIface(interface))
Ed Tanous60520632018-06-11 17:46:52 -0700555 {
Andrew Geissler67e5a422019-02-04 13:00:59 -0600556 removeAssociation(obj_path.str, sender, server,
Matt Spinlere2359fb2019-04-05 14:11:33 -0500557 associationMaps);
Ed Tanous60520632018-06-11 17:46:52 -0700558 }
559
560 interface_set->second.erase(interface);
561 // If this was the last interface on this connection,
562 // erase the connection
563 if (interface_set->second.empty())
564 {
565 connection_map->second.erase(interface_set);
566 }
567 }
568 // If this was the last connection on this object path,
569 // erase the object path
570 if (connection_map->second.empty())
571 {
572 interface_map.erase(connection_map);
573 }
Matt Spinlera82779f2019-01-09 12:39:42 -0600574
575 removeUnneededParents(obj_path.str, sender, interface_map);
Ed Tanous60520632018-06-11 17:46:52 -0700576 };
577
578 sdbusplus::bus::match::match interfacesRemoved(
579 static_cast<sdbusplus::bus::bus&>(*system_bus),
580 sdbusplus::bus::match::rules::interfacesRemoved(),
581 interfacesRemovedHandler);
582
583 std::function<void(sdbusplus::message::message & message)>
Matt Spinlere0b0e3a2019-04-08 10:39:23 -0500584 associationChangedHandler = [&server, &name_owners, &interface_map](
Matt Spinler8f876a52019-04-15 13:22:50 -0500585 sdbusplus::message::message& message) {
586 std::string objectName;
587 boost::container::flat_map<
588 std::string,
589 sdbusplus::message::variant<std::vector<Association>>>
590 values;
591 message.read(objectName, values);
Matt Spinler937a2322019-01-23 13:54:22 -0600592
Matt Spinler8f876a52019-04-15 13:22:50 -0500593 auto prop =
594 std::find_if(values.begin(), values.end(), [](const auto& v) {
595 using namespace boost::algorithm;
596 return to_lower_copy(v.first) == "associations";
597 });
598
599 if (prop != values.end())
600 {
601 std::vector<Association> associations =
602 sdbusplus::message::variant_ns::get<
603 std::vector<Association>>(prop->second);
604
605 std::string well_known;
606 if (!getWellKnown(name_owners, message.get_sender(),
607 well_known))
608 {
609 return;
Ed Tanous60520632018-06-11 17:46:52 -0700610 }
Matt Spinler8f876a52019-04-15 13:22:50 -0500611 associationChanged(server, associations, message.get_path(),
Matt Spinlere0b0e3a2019-04-08 10:39:23 -0500612 well_known, interface_map, associationMaps);
Matt Spinler8f876a52019-04-15 13:22:50 -0500613 }
614 };
615 sdbusplus::bus::match::match assocChangedMatch(
Ed Tanous60520632018-06-11 17:46:52 -0700616 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") +
Matt Spinler8f876a52019-04-15 13:22:50 -0500620 sdbusplus::bus::match::rules::argN(0, assocDefsInterface),
621 associationChangedHandler);
622
623 sdbusplus::bus::match::match orgOpenbmcAssocChangedMatch(
624 static_cast<sdbusplus::bus::bus&>(*system_bus),
625 sdbusplus::bus::match::rules::interface(
626 "org.freedesktop.DBus.Properties") +
627 sdbusplus::bus::match::rules::member("PropertiesChanged") +
628 sdbusplus::bus::match::rules::argN(0, orgOpenBMCAssocDefsInterface),
Ed Tanous60520632018-06-11 17:46:52 -0700629 associationChangedHandler);
630
631 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
632 server.add_interface("/xyz/openbmc_project/object_mapper",
633 "xyz.openbmc_project.ObjectMapper");
634
635 iface->register_method(
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600636 "GetAncestors", [&interface_map](std::string& req_path,
Ed Tanous60520632018-06-11 17:46:52 -0700637 std::vector<std::string>& interfaces) {
638 // Interfaces need to be sorted for intersect to function
639 std::sort(interfaces.begin(), interfaces.end());
640
James Feistd7322872019-01-28 11:25:00 -0800641 if (boost::ends_with(req_path, "/"))
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600642 {
James Feistd7322872019-01-28 11:25:00 -0800643 req_path.pop_back();
644 }
645 if (req_path.size() &&
646 interface_map.find(req_path) == interface_map.end())
647 {
648 throw NotFoundException();
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600649 }
650
Ed Tanous60520632018-06-11 17:46:52 -0700651 std::vector<interface_map_type::value_type> ret;
652 for (auto& object_path : interface_map)
653 {
654 auto& this_path = object_path.first;
Matt Spinlerea6516c2018-09-25 16:23:13 -0500655 if (boost::starts_with(req_path, this_path) &&
656 (req_path != this_path))
Ed Tanous60520632018-06-11 17:46:52 -0700657 {
658 if (interfaces.empty())
659 {
660 ret.emplace_back(object_path);
661 }
662 else
663 {
664 for (auto& interface_map : object_path.second)
665 {
666
667 if (intersect(interfaces.begin(), interfaces.end(),
668 interface_map.second.begin(),
669 interface_map.second.end()))
670 {
Matt Spinler47c09752018-11-29 14:54:13 -0600671 addObjectMapResult(ret, this_path,
672 interface_map);
Ed Tanous60520632018-06-11 17:46:52 -0700673 }
674 }
675 }
676 }
677 }
Ed Tanous60520632018-06-11 17:46:52 -0700678
679 return ret;
680 });
681
682 iface->register_method(
683 "GetObject", [&interface_map](const std::string& path,
684 std::vector<std::string>& interfaces) {
Matt Spinler7a38d412018-09-18 16:13:25 -0500685 boost::container::flat_map<std::string,
686 boost::container::flat_set<std::string>>
687 results;
688
Ed Tanous60520632018-06-11 17:46:52 -0700689 // Interfaces need to be sorted for intersect to function
690 std::sort(interfaces.begin(), interfaces.end());
691 auto path_ref = interface_map.find(path);
692 if (path_ref == interface_map.end())
693 {
694 throw NotFoundException();
695 }
696 if (interfaces.empty())
697 {
698 return path_ref->second;
699 }
700 for (auto& interface_map : path_ref->second)
701 {
702 if (intersect(interfaces.begin(), interfaces.end(),
703 interface_map.second.begin(),
704 interface_map.second.end()))
705 {
Matt Spinler7a38d412018-09-18 16:13:25 -0500706 results.emplace(interface_map.first, interface_map.second);
Ed Tanous60520632018-06-11 17:46:52 -0700707 }
708 }
Matt Spinler7a38d412018-09-18 16:13:25 -0500709
710 if (results.empty())
711 {
712 throw NotFoundException();
713 }
714
715 return results;
Ed Tanous60520632018-06-11 17:46:52 -0700716 });
717
718 iface->register_method(
Matt Spinler153494e2018-11-07 16:35:40 -0600719 "GetSubTree", [&interface_map](std::string& req_path, int32_t depth,
720 std::vector<std::string>& interfaces) {
Ed Tanous60520632018-06-11 17:46:52 -0700721 if (depth <= 0)
722 {
723 depth = std::numeric_limits<int32_t>::max();
724 }
725 // Interfaces need to be sorted for intersect to function
726 std::sort(interfaces.begin(), interfaces.end());
727 std::vector<interface_map_type::value_type> ret;
728
James Feistd7322872019-01-28 11:25:00 -0800729 if (boost::ends_with(req_path, "/"))
Matt Spinler153494e2018-11-07 16:35:40 -0600730 {
James Feistd7322872019-01-28 11:25:00 -0800731 req_path.pop_back();
732 }
733 if (req_path.size() &&
734 interface_map.find(req_path) == interface_map.end())
735 {
736 throw NotFoundException();
Matt Spinler153494e2018-11-07 16:35:40 -0600737 }
738
Ed Tanous60520632018-06-11 17:46:52 -0700739 for (auto& object_path : interface_map)
740 {
741 auto& this_path = object_path.first;
Matt Spinler153494e2018-11-07 16:35:40 -0600742
743 if (this_path == req_path)
744 {
745 continue;
746 }
747
Ed Tanous60520632018-06-11 17:46:52 -0700748 if (boost::starts_with(this_path, req_path))
749 {
750 // count the number of slashes past the search term
751 int32_t this_depth =
752 std::count(this_path.begin() + req_path.size(),
753 this_path.end(), '/');
754 if (this_depth <= depth)
755 {
Ed Tanous60520632018-06-11 17:46:52 -0700756 for (auto& interface_map : object_path.second)
757 {
758 if (intersect(interfaces.begin(), interfaces.end(),
759 interface_map.second.begin(),
Matt Spinler9f0958e2018-09-11 08:26:10 -0500760 interface_map.second.end()) ||
761 interfaces.empty())
Ed Tanous60520632018-06-11 17:46:52 -0700762 {
Matt Spinler47c09752018-11-29 14:54:13 -0600763 addObjectMapResult(ret, this_path,
764 interface_map);
Ed Tanous60520632018-06-11 17:46:52 -0700765 }
766 }
Ed Tanous60520632018-06-11 17:46:52 -0700767 }
768 }
769 }
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600770
Ed Tanous60520632018-06-11 17:46:52 -0700771 return ret;
772 });
773
774 iface->register_method(
775 "GetSubTreePaths",
Matt Spinler153494e2018-11-07 16:35:40 -0600776 [&interface_map](std::string& req_path, int32_t depth,
Ed Tanous60520632018-06-11 17:46:52 -0700777 std::vector<std::string>& interfaces) {
778 if (depth <= 0)
779 {
780 depth = std::numeric_limits<int32_t>::max();
781 }
782 // Interfaces need to be sorted for intersect to function
783 std::sort(interfaces.begin(), interfaces.end());
784 std::vector<std::string> ret;
Matt Spinler153494e2018-11-07 16:35:40 -0600785
James Feistd7322872019-01-28 11:25:00 -0800786 if (boost::ends_with(req_path, "/"))
Matt Spinler153494e2018-11-07 16:35:40 -0600787 {
James Feistd7322872019-01-28 11:25:00 -0800788 req_path.pop_back();
789 }
790 if (req_path.size() &&
791 interface_map.find(req_path) == interface_map.end())
792 {
793 throw NotFoundException();
Matt Spinler153494e2018-11-07 16:35:40 -0600794 }
795
Ed Tanous60520632018-06-11 17:46:52 -0700796 for (auto& object_path : interface_map)
797 {
798 auto& this_path = object_path.first;
Matt Spinler153494e2018-11-07 16:35:40 -0600799
800 if (this_path == req_path)
801 {
802 continue;
803 }
804
Ed Tanous60520632018-06-11 17:46:52 -0700805 if (boost::starts_with(this_path, req_path))
806 {
807 // count the number of slashes past the search term
808 int this_depth =
809 std::count(this_path.begin() + req_path.size(),
810 this_path.end(), '/');
811 if (this_depth <= depth)
812 {
813 bool add = interfaces.empty();
814 for (auto& interface_map : object_path.second)
815 {
816 if (intersect(interfaces.begin(), interfaces.end(),
817 interface_map.second.begin(),
818 interface_map.second.end()))
819 {
820 add = true;
821 break;
822 }
823 }
824 if (add)
825 {
826 // TODO(ed) this is a copy
827 ret.emplace_back(this_path);
828 }
829 }
830 }
831 }
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600832
Ed Tanous60520632018-06-11 17:46:52 -0700833 return ret;
834 });
835
836 iface->initialize();
837
838 io.post([&]() {
Matt Spinler11401e22019-04-08 13:13:25 -0500839 doListNames(io, interface_map, system_bus.get(), name_owners,
840 associationMaps, server);
Ed Tanous60520632018-06-11 17:46:52 -0700841 });
842
Ed Tanous60520632018-06-11 17:46:52 -0700843 io.run();
844}