blob: 1ed5435faa58ecb0c1548271f807c1179f939ff0 [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
Andrew Geissler67e5a422019-02-04 13:00:59 -060021AssociationInterfaces associationInterfaces;
Matt Spinler937a2322019-01-23 13:54:22 -060022AssociationOwnersType associationOwners;
23
Andrew Geissler82815da2019-02-04 12:19:41 -060024static WhiteBlackList service_whitelist;
25static WhiteBlackList service_blacklist;
Matt Spinlerdd945862018-09-07 12:41:05 -050026
Ed Tanous60520632018-06-11 17:46:52 -070027/** Exception thrown when a path is not found in the object list. */
28struct NotFoundException final : public sdbusplus::exception_t
29{
30 const char* name() const noexcept override
31 {
32 return "org.freedesktop.DBus.Error.FileNotFound";
33 };
34 const char* description() const noexcept override
35 {
36 return "path or object not found";
37 };
38 const char* what() const noexcept override
39 {
40 return "org.freedesktop.DBus.Error.FileNotFound: "
41 "The requested object was not found";
42 };
43};
44
Ed Tanous60520632018-06-11 17:46:52 -070045void update_owners(sdbusplus::asio::connection* conn,
46 boost::container::flat_map<std::string, std::string>& owners,
47 const std::string& new_object)
48{
49 if (boost::starts_with(new_object, ":"))
50 {
51 return;
52 }
53 conn->async_method_call(
54 [&, new_object](const boost::system::error_code ec,
55 const std::string& nameOwner) {
56 if (ec)
57 {
58 std::cerr << "Error getting owner of " << new_object << " : "
59 << ec << "\n";
60 return;
61 }
62 owners[nameOwner] = new_object;
63 },
64 "org.freedesktop.DBus", "/", "org.freedesktop.DBus", "GetNameOwner",
65 new_object);
66}
67
68void send_introspection_complete_signal(sdbusplus::asio::connection* system_bus,
69 const std::string& process_name)
70{
71 // TODO(ed) This signal doesn't get exposed properly in the
72 // introspect right now. Find out how to register signals in
73 // sdbusplus
74 sdbusplus::message::message m = system_bus->new_signal(
75 "/xyz/openbmc_project/object_mapper",
76 "xyz.openbmc_project.ObjectMapper.Private", "IntrospectionComplete");
77 m.append(process_name);
78 m.signal_send();
79}
80
81struct InProgressIntrospect
82{
83 InProgressIntrospect(
84 sdbusplus::asio::connection* system_bus, boost::asio::io_service& io,
Matt Spinleraecabe82018-09-19 13:25:42 -050085 const std::string& process_name
86#ifdef DEBUG
87 ,
Ed Tanous60520632018-06-11 17:46:52 -070088 std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>>
Matt Spinleraecabe82018-09-19 13:25:42 -050089 global_start_time
90#endif
91 ) :
Ed Tanous60520632018-06-11 17:46:52 -070092 system_bus(system_bus),
Matt Spinleraecabe82018-09-19 13:25:42 -050093 io(io), process_name(process_name)
94#ifdef DEBUG
95 ,
Ed Tanous60520632018-06-11 17:46:52 -070096 global_start_time(global_start_time),
97 process_start_time(std::chrono::steady_clock::now())
Matt Spinleraecabe82018-09-19 13:25:42 -050098#endif
Ed Tanous60520632018-06-11 17:46:52 -070099 {
100 }
101 ~InProgressIntrospect()
102 {
103 send_introspection_complete_signal(system_bus, process_name);
Matt Spinleraecabe82018-09-19 13:25:42 -0500104
105#ifdef DEBUG
Ed Tanous60520632018-06-11 17:46:52 -0700106 std::chrono::duration<float> diff =
107 std::chrono::steady_clock::now() - process_start_time;
108 std::cout << std::setw(50) << process_name << " scan took "
109 << diff.count() << " seconds\n";
110
111 // If we're the last outstanding caller globally, calculate the
112 // time it took
113 if (global_start_time != nullptr && global_start_time.use_count() == 1)
114 {
115 diff = std::chrono::steady_clock::now() - *global_start_time;
116 std::cout << "Total scan took " << diff.count()
117 << " seconds to complete\n";
118 }
Matt Spinleraecabe82018-09-19 13:25:42 -0500119#endif
Ed Tanous60520632018-06-11 17:46:52 -0700120 }
121 sdbusplus::asio::connection* system_bus;
122 boost::asio::io_service& io;
123 std::string process_name;
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,
132 sdbusplus::asio::object_server& objectServer,
Matt Spinler8f876a52019-04-15 13:22:50 -0500133 const std::string& processName, const std::string& path,
134 const std::string& assocDefIface)
Ed Tanous60520632018-06-11 17:46:52 -0700135{
136 system_bus->async_method_call(
Matt Spinler937a2322019-01-23 13:54:22 -0600137 [&objectServer, path, processName](
138 const boost::system::error_code ec,
139 const sdbusplus::message::variant<std::vector<Association>>&
140 variantAssociations) {
Ed Tanous60520632018-06-11 17:46:52 -0700141 if (ec)
142 {
143 std::cerr << "Error getting associations from " << path << "\n";
144 }
145 std::vector<Association> associations =
146 sdbusplus::message::variant_ns::get<std::vector<Association>>(
147 variantAssociations);
Andrew Geissler4511b332019-02-21 15:40:40 -0600148 associationChanged(objectServer, associations, path, processName,
149 associationOwners, associationInterfaces);
Ed Tanous60520632018-06-11 17:46:52 -0700150 },
151 processName, path, "org.freedesktop.DBus.Properties", "Get",
Matt Spinler8f876a52019-04-15 13:22:50 -0500152 assocDefIface, getAssocDefPropName(assocDefIface));
Ed Tanous60520632018-06-11 17:46:52 -0700153}
154
155void do_introspect(sdbusplus::asio::connection* system_bus,
156 std::shared_ptr<InProgressIntrospect> transaction,
157 interface_map_type& interface_map,
158 sdbusplus::asio::object_server& objectServer,
159 std::string path)
160{
161 system_bus->async_method_call(
162 [&interface_map, &objectServer, transaction, path,
163 system_bus](const boost::system::error_code ec,
164 const std::string& introspect_xml) {
165 if (ec)
166 {
167 std::cerr << "Introspect call failed with error: " << ec << ", "
168 << ec.message()
169 << " on process: " << transaction->process_name
170 << " path: " << path << "\n";
171 return;
172 }
173
174 tinyxml2::XMLDocument doc;
175
176 tinyxml2::XMLError e = doc.Parse(introspect_xml.c_str());
177 if (e != tinyxml2::XMLError::XML_SUCCESS)
178 {
179 std::cerr << "XML parsing failed\n";
180 return;
181 }
182
183 tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node");
184 if (pRoot == nullptr)
185 {
186 std::cerr << "XML document did not contain any data\n";
187 return;
188 }
189 auto& thisPathMap = interface_map[path];
Ed Tanous60520632018-06-11 17:46:52 -0700190 tinyxml2::XMLElement* pElement =
191 pRoot->FirstChildElement("interface");
192 while (pElement != nullptr)
193 {
194 const char* iface_name = pElement->Attribute("name");
195 if (iface_name == nullptr)
196 {
197 continue;
198 }
199
Ed Tanousd4dd96a2018-11-12 11:37:44 -0800200 thisPathMap[transaction->process_name].emplace(iface_name);
201
Matt Spinler8f876a52019-04-15 13:22:50 -0500202 if (isAssocDefIface(iface_name))
Ed Tanous60520632018-06-11 17:46:52 -0700203 {
204 do_associations(system_bus, objectServer,
Matt Spinler8f876a52019-04-15 13:22:50 -0500205 transaction->process_name, path,
206 iface_name);
Ed Tanous60520632018-06-11 17:46:52 -0700207 }
Ed Tanous60520632018-06-11 17:46:52 -0700208
209 pElement = pElement->NextSiblingElement("interface");
210 }
211
Ed Tanous50232cd2018-11-12 11:34:43 -0800212 pElement = pRoot->FirstChildElement("node");
213 while (pElement != nullptr)
Ed Tanous60520632018-06-11 17:46:52 -0700214 {
Ed Tanous50232cd2018-11-12 11:34:43 -0800215 const char* child_path = pElement->Attribute("name");
216 if (child_path != nullptr)
Ed Tanous60520632018-06-11 17:46:52 -0700217 {
Ed Tanous50232cd2018-11-12 11:34:43 -0800218 std::string parent_path(path);
219 if (parent_path == "/")
Ed Tanous60520632018-06-11 17:46:52 -0700220 {
Ed Tanous50232cd2018-11-12 11:34:43 -0800221 parent_path.clear();
Ed Tanous60520632018-06-11 17:46:52 -0700222 }
Ed Tanous50232cd2018-11-12 11:34:43 -0800223
224 do_introspect(system_bus, transaction, interface_map,
225 objectServer, parent_path + "/" + child_path);
Ed Tanous60520632018-06-11 17:46:52 -0700226 }
Ed Tanous50232cd2018-11-12 11:34:43 -0800227 pElement = pElement->NextSiblingElement("node");
Ed Tanous60520632018-06-11 17:46:52 -0700228 }
229 },
230 transaction->process_name, path, "org.freedesktop.DBus.Introspectable",
231 "Introspect");
232}
233
Ed Tanous60520632018-06-11 17:46:52 -0700234void start_new_introspect(
235 sdbusplus::asio::connection* system_bus, boost::asio::io_service& io,
236 interface_map_type& interface_map, const std::string& process_name,
Matt Spinleraecabe82018-09-19 13:25:42 -0500237#ifdef DEBUG
Ed Tanous60520632018-06-11 17:46:52 -0700238 std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>>
239 global_start_time,
Matt Spinleraecabe82018-09-19 13:25:42 -0500240#endif
Ed Tanous60520632018-06-11 17:46:52 -0700241 sdbusplus::asio::object_server& objectServer)
242{
Andrew Geissler82815da2019-02-04 12:19:41 -0600243 if (needToIntrospect(process_name, service_whitelist, service_blacklist))
Ed Tanous60520632018-06-11 17:46:52 -0700244 {
Ed Tanous60520632018-06-11 17:46:52 -0700245 std::shared_ptr<InProgressIntrospect> transaction =
Matt Spinleraecabe82018-09-19 13:25:42 -0500246 std::make_shared<InProgressIntrospect>(system_bus, io, process_name
247#ifdef DEBUG
248 ,
249 global_start_time
250#endif
251 );
Ed Tanous60520632018-06-11 17:46:52 -0700252
253 do_introspect(system_bus, transaction, interface_map, objectServer,
254 "/");
255 }
256}
257
258// TODO(ed) replace with std::set_intersection once c++17 is available
259template <class InputIt1, class InputIt2>
260bool intersect(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2)
261{
262 while (first1 != last1 && first2 != last2)
263 {
264 if (*first1 < *first2)
265 {
266 ++first1;
267 continue;
268 }
269 if (*first2 < *first1)
270 {
271 ++first2;
272 continue;
273 }
274 return true;
275 }
276 return false;
277}
278
279void doListNames(
280 boost::asio::io_service& io, interface_map_type& interface_map,
281 sdbusplus::asio::connection* system_bus,
282 boost::container::flat_map<std::string, std::string>& name_owners,
283 sdbusplus::asio::object_server& objectServer)
284{
285 system_bus->async_method_call(
286 [&io, &interface_map, &name_owners, &objectServer,
287 system_bus](const boost::system::error_code ec,
288 std::vector<std::string> process_names) {
289 if (ec)
290 {
291 std::cerr << "Error getting names: " << ec << "\n";
292 std::exit(EXIT_FAILURE);
293 return;
294 }
Ed Tanous60520632018-06-11 17:46:52 -0700295 // Try to make startup consistent
296 std::sort(process_names.begin(), process_names.end());
Matt Spinleraecabe82018-09-19 13:25:42 -0500297#ifdef DEBUG
Ed Tanous60520632018-06-11 17:46:52 -0700298 std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>>
299 global_start_time = std::make_shared<
300 std::chrono::time_point<std::chrono::steady_clock>>(
301 std::chrono::steady_clock::now());
Matt Spinleraecabe82018-09-19 13:25:42 -0500302#endif
Ed Tanous60520632018-06-11 17:46:52 -0700303 for (const std::string& process_name : process_names)
304 {
Andrew Geissler82815da2019-02-04 12:19:41 -0600305 if (needToIntrospect(process_name, service_whitelist,
306 service_blacklist))
Ed Tanous60520632018-06-11 17:46:52 -0700307 {
308 start_new_introspect(system_bus, io, interface_map,
Matt Spinleraecabe82018-09-19 13:25:42 -0500309 process_name,
310#ifdef DEBUG
311 global_start_time,
312#endif
Ed Tanous60520632018-06-11 17:46:52 -0700313 objectServer);
314 update_owners(system_bus, name_owners, process_name);
315 }
316 }
317 },
318 "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus",
319 "ListNames");
320}
321
Matt Spinlerdd945862018-09-07 12:41:05 -0500322void splitArgs(const std::string& stringArgs,
323 boost::container::flat_set<std::string>& listArgs)
324{
325 std::istringstream args;
326 std::string arg;
327
328 args.str(stringArgs);
329
330 while (!args.eof())
331 {
332 args >> arg;
333 if (!arg.empty())
334 {
335 listArgs.insert(arg);
336 }
337 }
338}
339
Matt Spinler47c09752018-11-29 14:54:13 -0600340void addObjectMapResult(
341 std::vector<interface_map_type::value_type>& objectMap,
Matt Spinler9f0958e2018-09-11 08:26:10 -0500342 const std::string& objectPath,
343 const std::pair<std::string, boost::container::flat_set<std::string>>&
344 interfaceMap)
345{
346 // Adds an object path/service name/interface list entry to
Matt Spinler47c09752018-11-29 14:54:13 -0600347 // the results of GetSubTree and GetAncestors.
Matt Spinler9f0958e2018-09-11 08:26:10 -0500348 // If an entry for the object path already exists, just add the
349 // service name and interfaces to that entry, otherwise create
350 // a new entry.
351 auto entry = std::find_if(
Matt Spinler47c09752018-11-29 14:54:13 -0600352 objectMap.begin(), objectMap.end(),
Matt Spinler9f0958e2018-09-11 08:26:10 -0500353 [&objectPath](const auto& i) { return objectPath == i.first; });
354
Matt Spinler47c09752018-11-29 14:54:13 -0600355 if (entry != objectMap.end())
Matt Spinler9f0958e2018-09-11 08:26:10 -0500356 {
357 entry->second.emplace(interfaceMap);
358 }
359 else
360 {
361 interface_map_type::value_type object;
362 object.first = objectPath;
363 object.second.emplace(interfaceMap);
Matt Spinler47c09752018-11-29 14:54:13 -0600364 objectMap.push_back(object);
Matt Spinler9f0958e2018-09-11 08:26:10 -0500365 }
366}
367
Matt Spinlera82779f2019-01-09 12:39:42 -0600368// Remove parents of the passed in path that:
369// 1) Only have the 3 default interfaces on them
370// - Means D-Bus created these, not application code,
371// with the Properties, Introspectable, and Peer ifaces
372// 2) Have no other child for this owner
373void removeUnneededParents(const std::string& objectPath,
374 const std::string& owner,
375 interface_map_type& interface_map)
376{
377 auto parent = objectPath;
378
379 while (true)
380 {
381 auto pos = parent.find_last_of('/');
382 if ((pos == std::string::npos) || (pos == 0))
383 {
384 break;
385 }
386 parent = parent.substr(0, pos);
387
388 auto parent_it = interface_map.find(parent);
389 if (parent_it == interface_map.end())
390 {
391 break;
392 }
393
394 auto ifaces_it = parent_it->second.find(owner);
395 if (ifaces_it == parent_it->second.end())
396 {
397 break;
398 }
399
400 if (ifaces_it->second.size() != 3)
401 {
402 break;
403 }
404
405 auto child_path = parent + '/';
406
407 // Remove this parent if there isn't a remaining child on this owner
408 auto child = std::find_if(
409 interface_map.begin(), interface_map.end(),
410 [&owner, &child_path](const auto& entry) {
411 return boost::starts_with(entry.first, child_path) &&
412 (entry.second.find(owner) != entry.second.end());
413 });
414
415 if (child == interface_map.end())
416 {
417 parent_it->second.erase(ifaces_it);
418 if (parent_it->second.empty())
419 {
420 interface_map.erase(parent_it);
421 }
422 }
423 else
424 {
425 break;
426 }
427 }
428}
429
Ed Tanous60520632018-06-11 17:46:52 -0700430int main(int argc, char** argv)
431{
Matt Spinlerdd945862018-09-07 12:41:05 -0500432 auto options = ArgumentParser(argc, argv);
Ed Tanous60520632018-06-11 17:46:52 -0700433 boost::asio::io_service io;
434 std::shared_ptr<sdbusplus::asio::connection> system_bus =
435 std::make_shared<sdbusplus::asio::connection>(io);
436
Matt Spinlerdd945862018-09-07 12:41:05 -0500437 splitArgs(options["service-namespaces"], service_whitelist);
Matt Spinlerdd945862018-09-07 12:41:05 -0500438 splitArgs(options["service-blacklists"], service_blacklist);
439
Ed Tanousd4dd96a2018-11-12 11:37:44 -0800440 // TODO(Ed) Remove this once all service files are updated to not use this.
441 // For now, simply squash the input, and ignore it.
442 boost::container::flat_set<std::string> iface_whitelist;
443 splitArgs(options["interface-namespaces"], iface_whitelist);
444
Ed Tanous60520632018-06-11 17:46:52 -0700445 system_bus->request_name(OBJECT_MAPPER_DBUS_NAME);
446 sdbusplus::asio::object_server server(system_bus);
447
448 // Construct a signal set registered for process termination.
449 boost::asio::signal_set signals(io, SIGINT, SIGTERM);
450 signals.async_wait([&io](const boost::system::error_code& error,
451 int signal_number) { io.stop(); });
452
453 interface_map_type interface_map;
454 boost::container::flat_map<std::string, std::string> name_owners;
455
456 std::function<void(sdbusplus::message::message & message)>
457 nameChangeHandler = [&interface_map, &io, &name_owners, &server,
458 system_bus](sdbusplus::message::message& message) {
Andrew Geissler20679262019-02-11 20:20:40 -0600459 std::string name; // well-known
460 std::string old_owner; // unique-name
461 std::string new_owner; // unique-name
Ed Tanous60520632018-06-11 17:46:52 -0700462
463 message.read(name, old_owner, new_owner);
464
465 if (!old_owner.empty())
466 {
Andrew Geissler20679262019-02-11 20:20:40 -0600467 processNameChangeDelete(name_owners, name, old_owner,
468 interface_map, associationOwners,
469 associationInterfaces, server);
Ed Tanous60520632018-06-11 17:46:52 -0700470 }
471
472 if (!new_owner.empty())
473 {
Matt Spinleraecabe82018-09-19 13:25:42 -0500474#ifdef DEBUG
Ed Tanous60520632018-06-11 17:46:52 -0700475 auto transaction = std::make_shared<
476 std::chrono::time_point<std::chrono::steady_clock>>(
477 std::chrono::steady_clock::now());
Matt Spinleraecabe82018-09-19 13:25:42 -0500478#endif
Ed Tanous60520632018-06-11 17:46:52 -0700479 // New daemon added
Andrew Geissler82815da2019-02-04 12:19:41 -0600480 if (needToIntrospect(name, service_whitelist,
481 service_blacklist))
Ed Tanous60520632018-06-11 17:46:52 -0700482 {
483 name_owners[new_owner] = name;
484 start_new_introspect(system_bus.get(), io, interface_map,
Matt Spinleraecabe82018-09-19 13:25:42 -0500485 name,
486#ifdef DEBUG
487 transaction,
488#endif
489 server);
Ed Tanous60520632018-06-11 17:46:52 -0700490 }
491 }
492 };
493
494 sdbusplus::bus::match::match nameOwnerChanged(
495 static_cast<sdbusplus::bus::bus&>(*system_bus),
496 sdbusplus::bus::match::rules::nameOwnerChanged(), nameChangeHandler);
497
498 std::function<void(sdbusplus::message::message & message)>
499 interfacesAddedHandler = [&interface_map, &name_owners, &server](
500 sdbusplus::message::message& message) {
501 sdbusplus::message::object_path obj_path;
Andrew Geissler70461892019-02-27 09:57:37 -0600502 InterfacesAdded interfaces_added;
Ed Tanous60520632018-06-11 17:46:52 -0700503 message.read(obj_path, interfaces_added);
504 std::string well_known;
Andrew Geissler3b025e62019-02-01 10:33:54 -0600505 if (!getWellKnown(name_owners, message.get_sender(), well_known))
Ed Tanous60520632018-06-11 17:46:52 -0700506 {
507 return; // only introspect well-known
508 }
Andrew Geissler82815da2019-02-04 12:19:41 -0600509 if (needToIntrospect(well_known, service_whitelist,
510 service_blacklist))
Ed Tanous60520632018-06-11 17:46:52 -0700511 {
Andrew Geissler70461892019-02-27 09:57:37 -0600512 processInterfaceAdded(interface_map, obj_path, interfaces_added,
513 well_known, associationOwners,
514 associationInterfaces, server);
Ed Tanous60520632018-06-11 17:46:52 -0700515 }
516 };
517
518 sdbusplus::bus::match::match interfacesAdded(
519 static_cast<sdbusplus::bus::bus&>(*system_bus),
520 sdbusplus::bus::match::rules::interfacesAdded(),
521 interfacesAddedHandler);
522
523 std::function<void(sdbusplus::message::message & message)>
524 interfacesRemovedHandler = [&interface_map, &name_owners, &server](
525 sdbusplus::message::message& message) {
526 sdbusplus::message::object_path obj_path;
527 std::vector<std::string> interfaces_removed;
528 message.read(obj_path, interfaces_removed);
529 auto connection_map = interface_map.find(obj_path.str);
530 if (connection_map == interface_map.end())
531 {
532 return;
533 }
534
535 std::string sender;
Andrew Geissler3b025e62019-02-01 10:33:54 -0600536 if (!getWellKnown(name_owners, message.get_sender(), sender))
Ed Tanous60520632018-06-11 17:46:52 -0700537 {
538 return;
539 }
540 for (const std::string& interface : interfaces_removed)
541 {
542 auto interface_set = connection_map->second.find(sender);
543 if (interface_set == connection_map->second.end())
544 {
545 continue;
546 }
547
Matt Spinler8f876a52019-04-15 13:22:50 -0500548 if (isAssocDefIface(interface))
Ed Tanous60520632018-06-11 17:46:52 -0700549 {
Andrew Geissler67e5a422019-02-04 13:00:59 -0600550 removeAssociation(obj_path.str, sender, server,
551 associationOwners, associationInterfaces);
Ed Tanous60520632018-06-11 17:46:52 -0700552 }
553
554 interface_set->second.erase(interface);
555 // If this was the last interface on this connection,
556 // erase the connection
557 if (interface_set->second.empty())
558 {
559 connection_map->second.erase(interface_set);
560 }
561 }
562 // If this was the last connection on this object path,
563 // erase the object path
564 if (connection_map->second.empty())
565 {
566 interface_map.erase(connection_map);
567 }
Matt Spinlera82779f2019-01-09 12:39:42 -0600568
569 removeUnneededParents(obj_path.str, sender, interface_map);
Ed Tanous60520632018-06-11 17:46:52 -0700570 };
571
572 sdbusplus::bus::match::match interfacesRemoved(
573 static_cast<sdbusplus::bus::bus&>(*system_bus),
574 sdbusplus::bus::match::rules::interfacesRemoved(),
575 interfacesRemovedHandler);
576
577 std::function<void(sdbusplus::message::message & message)>
Matt Spinler8f876a52019-04-15 13:22:50 -0500578 associationChangedHandler = [&server, &name_owners](
579 sdbusplus::message::message& message) {
580 std::string objectName;
581 boost::container::flat_map<
582 std::string,
583 sdbusplus::message::variant<std::vector<Association>>>
584 values;
585 message.read(objectName, values);
Matt Spinler937a2322019-01-23 13:54:22 -0600586
Matt Spinler8f876a52019-04-15 13:22:50 -0500587 auto prop =
588 std::find_if(values.begin(), values.end(), [](const auto& v) {
589 using namespace boost::algorithm;
590 return to_lower_copy(v.first) == "associations";
591 });
592
593 if (prop != values.end())
594 {
595 std::vector<Association> associations =
596 sdbusplus::message::variant_ns::get<
597 std::vector<Association>>(prop->second);
598
599 std::string well_known;
600 if (!getWellKnown(name_owners, message.get_sender(),
601 well_known))
602 {
603 return;
Ed Tanous60520632018-06-11 17:46:52 -0700604 }
Matt Spinler8f876a52019-04-15 13:22:50 -0500605 associationChanged(server, associations, message.get_path(),
606 well_known, associationOwners,
607 associationInterfaces);
608 }
609 };
610 sdbusplus::bus::match::match assocChangedMatch(
Ed Tanous60520632018-06-11 17:46:52 -0700611 static_cast<sdbusplus::bus::bus&>(*system_bus),
612 sdbusplus::bus::match::rules::interface(
613 "org.freedesktop.DBus.Properties") +
614 sdbusplus::bus::match::rules::member("PropertiesChanged") +
Matt Spinler8f876a52019-04-15 13:22:50 -0500615 sdbusplus::bus::match::rules::argN(0, assocDefsInterface),
616 associationChangedHandler);
617
618 sdbusplus::bus::match::match orgOpenbmcAssocChangedMatch(
619 static_cast<sdbusplus::bus::bus&>(*system_bus),
620 sdbusplus::bus::match::rules::interface(
621 "org.freedesktop.DBus.Properties") +
622 sdbusplus::bus::match::rules::member("PropertiesChanged") +
623 sdbusplus::bus::match::rules::argN(0, orgOpenBMCAssocDefsInterface),
Ed Tanous60520632018-06-11 17:46:52 -0700624 associationChangedHandler);
625
626 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
627 server.add_interface("/xyz/openbmc_project/object_mapper",
628 "xyz.openbmc_project.ObjectMapper");
629
630 iface->register_method(
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600631 "GetAncestors", [&interface_map](std::string& req_path,
Ed Tanous60520632018-06-11 17:46:52 -0700632 std::vector<std::string>& interfaces) {
633 // Interfaces need to be sorted for intersect to function
634 std::sort(interfaces.begin(), interfaces.end());
635
James Feistd7322872019-01-28 11:25:00 -0800636 if (boost::ends_with(req_path, "/"))
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600637 {
James Feistd7322872019-01-28 11:25:00 -0800638 req_path.pop_back();
639 }
640 if (req_path.size() &&
641 interface_map.find(req_path) == interface_map.end())
642 {
643 throw NotFoundException();
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600644 }
645
Ed Tanous60520632018-06-11 17:46:52 -0700646 std::vector<interface_map_type::value_type> ret;
647 for (auto& object_path : interface_map)
648 {
649 auto& this_path = object_path.first;
Matt Spinlerea6516c2018-09-25 16:23:13 -0500650 if (boost::starts_with(req_path, this_path) &&
651 (req_path != this_path))
Ed Tanous60520632018-06-11 17:46:52 -0700652 {
653 if (interfaces.empty())
654 {
655 ret.emplace_back(object_path);
656 }
657 else
658 {
659 for (auto& interface_map : object_path.second)
660 {
661
662 if (intersect(interfaces.begin(), interfaces.end(),
663 interface_map.second.begin(),
664 interface_map.second.end()))
665 {
Matt Spinler47c09752018-11-29 14:54:13 -0600666 addObjectMapResult(ret, this_path,
667 interface_map);
Ed Tanous60520632018-06-11 17:46:52 -0700668 }
669 }
670 }
671 }
672 }
Ed Tanous60520632018-06-11 17:46:52 -0700673
674 return ret;
675 });
676
677 iface->register_method(
678 "GetObject", [&interface_map](const std::string& path,
679 std::vector<std::string>& interfaces) {
Matt Spinler7a38d412018-09-18 16:13:25 -0500680 boost::container::flat_map<std::string,
681 boost::container::flat_set<std::string>>
682 results;
683
Ed Tanous60520632018-06-11 17:46:52 -0700684 // Interfaces need to be sorted for intersect to function
685 std::sort(interfaces.begin(), interfaces.end());
686 auto path_ref = interface_map.find(path);
687 if (path_ref == interface_map.end())
688 {
689 throw NotFoundException();
690 }
691 if (interfaces.empty())
692 {
693 return path_ref->second;
694 }
695 for (auto& interface_map : path_ref->second)
696 {
697 if (intersect(interfaces.begin(), interfaces.end(),
698 interface_map.second.begin(),
699 interface_map.second.end()))
700 {
Matt Spinler7a38d412018-09-18 16:13:25 -0500701 results.emplace(interface_map.first, interface_map.second);
Ed Tanous60520632018-06-11 17:46:52 -0700702 }
703 }
Matt Spinler7a38d412018-09-18 16:13:25 -0500704
705 if (results.empty())
706 {
707 throw NotFoundException();
708 }
709
710 return results;
Ed Tanous60520632018-06-11 17:46:52 -0700711 });
712
713 iface->register_method(
Matt Spinler153494e2018-11-07 16:35:40 -0600714 "GetSubTree", [&interface_map](std::string& req_path, int32_t depth,
715 std::vector<std::string>& interfaces) {
Ed Tanous60520632018-06-11 17:46:52 -0700716 if (depth <= 0)
717 {
718 depth = std::numeric_limits<int32_t>::max();
719 }
720 // Interfaces need to be sorted for intersect to function
721 std::sort(interfaces.begin(), interfaces.end());
722 std::vector<interface_map_type::value_type> ret;
723
James Feistd7322872019-01-28 11:25:00 -0800724 if (boost::ends_with(req_path, "/"))
Matt Spinler153494e2018-11-07 16:35:40 -0600725 {
James Feistd7322872019-01-28 11:25:00 -0800726 req_path.pop_back();
727 }
728 if (req_path.size() &&
729 interface_map.find(req_path) == interface_map.end())
730 {
731 throw NotFoundException();
Matt Spinler153494e2018-11-07 16:35:40 -0600732 }
733
Ed Tanous60520632018-06-11 17:46:52 -0700734 for (auto& object_path : interface_map)
735 {
736 auto& this_path = object_path.first;
Matt Spinler153494e2018-11-07 16:35:40 -0600737
738 if (this_path == req_path)
739 {
740 continue;
741 }
742
Ed Tanous60520632018-06-11 17:46:52 -0700743 if (boost::starts_with(this_path, req_path))
744 {
745 // count the number of slashes past the search term
746 int32_t this_depth =
747 std::count(this_path.begin() + req_path.size(),
748 this_path.end(), '/');
749 if (this_depth <= depth)
750 {
Ed Tanous60520632018-06-11 17:46:52 -0700751 for (auto& interface_map : object_path.second)
752 {
753 if (intersect(interfaces.begin(), interfaces.end(),
754 interface_map.second.begin(),
Matt Spinler9f0958e2018-09-11 08:26:10 -0500755 interface_map.second.end()) ||
756 interfaces.empty())
Ed Tanous60520632018-06-11 17:46:52 -0700757 {
Matt Spinler47c09752018-11-29 14:54:13 -0600758 addObjectMapResult(ret, this_path,
759 interface_map);
Ed Tanous60520632018-06-11 17:46:52 -0700760 }
761 }
Ed Tanous60520632018-06-11 17:46:52 -0700762 }
763 }
764 }
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600765
Ed Tanous60520632018-06-11 17:46:52 -0700766 return ret;
767 });
768
769 iface->register_method(
770 "GetSubTreePaths",
Matt Spinler153494e2018-11-07 16:35:40 -0600771 [&interface_map](std::string& req_path, int32_t depth,
Ed Tanous60520632018-06-11 17:46:52 -0700772 std::vector<std::string>& interfaces) {
773 if (depth <= 0)
774 {
775 depth = std::numeric_limits<int32_t>::max();
776 }
777 // Interfaces need to be sorted for intersect to function
778 std::sort(interfaces.begin(), interfaces.end());
779 std::vector<std::string> ret;
Matt Spinler153494e2018-11-07 16:35:40 -0600780
James Feistd7322872019-01-28 11:25:00 -0800781 if (boost::ends_with(req_path, "/"))
Matt Spinler153494e2018-11-07 16:35:40 -0600782 {
James Feistd7322872019-01-28 11:25:00 -0800783 req_path.pop_back();
784 }
785 if (req_path.size() &&
786 interface_map.find(req_path) == interface_map.end())
787 {
788 throw NotFoundException();
Matt Spinler153494e2018-11-07 16:35:40 -0600789 }
790
Ed Tanous60520632018-06-11 17:46:52 -0700791 for (auto& object_path : interface_map)
792 {
793 auto& this_path = object_path.first;
Matt Spinler153494e2018-11-07 16:35:40 -0600794
795 if (this_path == req_path)
796 {
797 continue;
798 }
799
Ed Tanous60520632018-06-11 17:46:52 -0700800 if (boost::starts_with(this_path, req_path))
801 {
802 // count the number of slashes past the search term
803 int this_depth =
804 std::count(this_path.begin() + req_path.size(),
805 this_path.end(), '/');
806 if (this_depth <= depth)
807 {
808 bool add = interfaces.empty();
809 for (auto& interface_map : object_path.second)
810 {
811 if (intersect(interfaces.begin(), interfaces.end(),
812 interface_map.second.begin(),
813 interface_map.second.end()))
814 {
815 add = true;
816 break;
817 }
818 }
819 if (add)
820 {
821 // TODO(ed) this is a copy
822 ret.emplace_back(this_path);
823 }
824 }
825 }
826 }
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600827
Ed Tanous60520632018-06-11 17:46:52 -0700828 return ret;
829 });
830
831 iface->initialize();
832
833 io.post([&]() {
834 doListNames(io, interface_map, system_bus.get(), name_owners, server);
835 });
836
Ed Tanous60520632018-06-11 17:46:52 -0700837 io.run();
838}