blob: e6ea2bb47e99217b6095a87d2498010b555c80d3 [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>
Matt Spinler8f876a52019-04-15 13:22:50 -05008#include <boost/algorithm/string/case_conv.hpp>
Ed Tanous60520632018-06-11 17:46:52 -07009#include <boost/algorithm/string/predicate.hpp>
10#include <boost/container/flat_map.hpp>
Ed Tanous60520632018-06-11 17:46:52 -070011#include <chrono>
12#include <iomanip>
13#include <iostream>
14#include <sdbusplus/asio/connection.hpp>
15#include <sdbusplus/asio/object_server.hpp>
16
17constexpr const char* OBJECT_MAPPER_DBUS_NAME =
18 "xyz.openbmc_project.ObjectMapper";
Ed Tanous60520632018-06-11 17:46:52 -070019
Andrew Geissler67e5a422019-02-04 13:00:59 -060020AssociationInterfaces associationInterfaces;
Matt Spinler937a2322019-01-23 13:54:22 -060021AssociationOwnersType associationOwners;
22
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 Spinleraecabe82018-09-19 13:25:42 -050084 const std::string& process_name
85#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 Spinleraecabe82018-09-19 13:25:42 -050092 io(io), process_name(process_name)
93#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 Spinleraecabe82018-09-19 13:25:42 -0500123#ifdef DEBUG
Ed Tanous60520632018-06-11 17:46:52 -0700124 std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>>
125 global_start_time;
126 std::chrono::time_point<std::chrono::steady_clock> process_start_time;
Matt Spinleraecabe82018-09-19 13:25:42 -0500127#endif
Ed Tanous60520632018-06-11 17:46:52 -0700128};
129
Ed Tanous60520632018-06-11 17:46:52 -0700130void do_associations(sdbusplus::asio::connection* system_bus,
131 sdbusplus::asio::object_server& objectServer,
Matt Spinler8f876a52019-04-15 13:22:50 -0500132 const std::string& processName, const std::string& path,
133 const std::string& assocDefIface)
Ed Tanous60520632018-06-11 17:46:52 -0700134{
135 system_bus->async_method_call(
Matt Spinler937a2322019-01-23 13:54:22 -0600136 [&objectServer, path, processName](
137 const boost::system::error_code ec,
138 const sdbusplus::message::variant<std::vector<Association>>&
139 variantAssociations) {
Ed Tanous60520632018-06-11 17:46:52 -0700140 if (ec)
141 {
142 std::cerr << "Error getting associations from " << path << "\n";
143 }
144 std::vector<Association> associations =
145 sdbusplus::message::variant_ns::get<std::vector<Association>>(
146 variantAssociations);
Andrew Geissler4511b332019-02-21 15:40:40 -0600147 associationChanged(objectServer, associations, path, processName,
148 associationOwners, associationInterfaces);
Ed Tanous60520632018-06-11 17:46:52 -0700149 },
150 processName, path, "org.freedesktop.DBus.Properties", "Get",
Matt Spinler8f876a52019-04-15 13:22:50 -0500151 assocDefIface, getAssocDefPropName(assocDefIface));
Ed Tanous60520632018-06-11 17:46:52 -0700152}
153
154void do_introspect(sdbusplus::asio::connection* system_bus,
155 std::shared_ptr<InProgressIntrospect> transaction,
156 interface_map_type& interface_map,
157 sdbusplus::asio::object_server& objectServer,
158 std::string path)
159{
160 system_bus->async_method_call(
161 [&interface_map, &objectServer, transaction, path,
162 system_bus](const boost::system::error_code ec,
163 const std::string& introspect_xml) {
164 if (ec)
165 {
166 std::cerr << "Introspect call failed with error: " << ec << ", "
167 << ec.message()
168 << " on process: " << transaction->process_name
169 << " path: " << path << "\n";
170 return;
171 }
172
173 tinyxml2::XMLDocument doc;
174
175 tinyxml2::XMLError e = doc.Parse(introspect_xml.c_str());
176 if (e != tinyxml2::XMLError::XML_SUCCESS)
177 {
178 std::cerr << "XML parsing failed\n";
179 return;
180 }
181
182 tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node");
183 if (pRoot == nullptr)
184 {
185 std::cerr << "XML document did not contain any data\n";
186 return;
187 }
188 auto& thisPathMap = interface_map[path];
Ed Tanous60520632018-06-11 17:46:52 -0700189 tinyxml2::XMLElement* pElement =
190 pRoot->FirstChildElement("interface");
191 while (pElement != nullptr)
192 {
193 const char* iface_name = pElement->Attribute("name");
194 if (iface_name == nullptr)
195 {
196 continue;
197 }
198
Ed Tanousd4dd96a2018-11-12 11:37:44 -0800199 thisPathMap[transaction->process_name].emplace(iface_name);
200
Matt Spinler8f876a52019-04-15 13:22:50 -0500201 if (isAssocDefIface(iface_name))
Ed Tanous60520632018-06-11 17:46:52 -0700202 {
203 do_associations(system_bus, objectServer,
Matt Spinler8f876a52019-04-15 13:22:50 -0500204 transaction->process_name, path,
205 iface_name);
Ed Tanous60520632018-06-11 17:46:52 -0700206 }
Ed Tanous60520632018-06-11 17:46:52 -0700207
208 pElement = pElement->NextSiblingElement("interface");
209 }
210
Ed Tanous50232cd2018-11-12 11:34:43 -0800211 pElement = pRoot->FirstChildElement("node");
212 while (pElement != nullptr)
Ed Tanous60520632018-06-11 17:46:52 -0700213 {
Ed Tanous50232cd2018-11-12 11:34:43 -0800214 const char* child_path = pElement->Attribute("name");
215 if (child_path != nullptr)
Ed Tanous60520632018-06-11 17:46:52 -0700216 {
Ed Tanous50232cd2018-11-12 11:34:43 -0800217 std::string parent_path(path);
218 if (parent_path == "/")
Ed Tanous60520632018-06-11 17:46:52 -0700219 {
Ed Tanous50232cd2018-11-12 11:34:43 -0800220 parent_path.clear();
Ed Tanous60520632018-06-11 17:46:52 -0700221 }
Ed Tanous50232cd2018-11-12 11:34:43 -0800222
223 do_introspect(system_bus, transaction, interface_map,
224 objectServer, parent_path + "/" + child_path);
Ed Tanous60520632018-06-11 17:46:52 -0700225 }
Ed Tanous50232cd2018-11-12 11:34:43 -0800226 pElement = pElement->NextSiblingElement("node");
Ed Tanous60520632018-06-11 17:46:52 -0700227 }
228 },
229 transaction->process_name, path, "org.freedesktop.DBus.Introspectable",
230 "Introspect");
231}
232
Ed Tanous60520632018-06-11 17:46:52 -0700233void start_new_introspect(
234 sdbusplus::asio::connection* system_bus, boost::asio::io_service& io,
235 interface_map_type& interface_map, const std::string& process_name,
Matt Spinleraecabe82018-09-19 13:25:42 -0500236#ifdef DEBUG
Ed Tanous60520632018-06-11 17:46:52 -0700237 std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>>
238 global_start_time,
Matt Spinleraecabe82018-09-19 13:25:42 -0500239#endif
Ed Tanous60520632018-06-11 17:46:52 -0700240 sdbusplus::asio::object_server& objectServer)
241{
Andrew Geissler82815da2019-02-04 12:19:41 -0600242 if (needToIntrospect(process_name, service_whitelist, service_blacklist))
Ed Tanous60520632018-06-11 17:46:52 -0700243 {
Ed Tanous60520632018-06-11 17:46:52 -0700244 std::shared_ptr<InProgressIntrospect> transaction =
Matt Spinleraecabe82018-09-19 13:25:42 -0500245 std::make_shared<InProgressIntrospect>(system_bus, io, process_name
246#ifdef DEBUG
247 ,
248 global_start_time
249#endif
250 );
Ed Tanous60520632018-06-11 17:46:52 -0700251
252 do_introspect(system_bus, transaction, interface_map, objectServer,
253 "/");
254 }
255}
256
257// TODO(ed) replace with std::set_intersection once c++17 is available
258template <class InputIt1, class InputIt2>
259bool intersect(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2)
260{
261 while (first1 != last1 && first2 != last2)
262 {
263 if (*first1 < *first2)
264 {
265 ++first1;
266 continue;
267 }
268 if (*first2 < *first1)
269 {
270 ++first2;
271 continue;
272 }
273 return true;
274 }
275 return false;
276}
277
278void doListNames(
279 boost::asio::io_service& io, interface_map_type& interface_map,
280 sdbusplus::asio::connection* system_bus,
281 boost::container::flat_map<std::string, std::string>& name_owners,
282 sdbusplus::asio::object_server& objectServer)
283{
284 system_bus->async_method_call(
285 [&io, &interface_map, &name_owners, &objectServer,
286 system_bus](const boost::system::error_code ec,
287 std::vector<std::string> process_names) {
288 if (ec)
289 {
290 std::cerr << "Error getting names: " << ec << "\n";
291 std::exit(EXIT_FAILURE);
292 return;
293 }
Ed Tanous60520632018-06-11 17:46:52 -0700294 // Try to make startup consistent
295 std::sort(process_names.begin(), process_names.end());
Matt Spinleraecabe82018-09-19 13:25:42 -0500296#ifdef DEBUG
Ed Tanous60520632018-06-11 17:46:52 -0700297 std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>>
298 global_start_time = std::make_shared<
299 std::chrono::time_point<std::chrono::steady_clock>>(
300 std::chrono::steady_clock::now());
Matt Spinleraecabe82018-09-19 13:25:42 -0500301#endif
Ed Tanous60520632018-06-11 17:46:52 -0700302 for (const std::string& process_name : process_names)
303 {
Andrew Geissler82815da2019-02-04 12:19:41 -0600304 if (needToIntrospect(process_name, service_whitelist,
305 service_blacklist))
Ed Tanous60520632018-06-11 17:46:52 -0700306 {
307 start_new_introspect(system_bus, io, interface_map,
Matt Spinleraecabe82018-09-19 13:25:42 -0500308 process_name,
309#ifdef DEBUG
310 global_start_time,
311#endif
Ed Tanous60520632018-06-11 17:46:52 -0700312 objectServer);
313 update_owners(system_bus, name_owners, process_name);
314 }
315 }
316 },
317 "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus",
318 "ListNames");
319}
320
Matt Spinlerdd945862018-09-07 12:41:05 -0500321void splitArgs(const std::string& stringArgs,
322 boost::container::flat_set<std::string>& listArgs)
323{
324 std::istringstream args;
325 std::string arg;
326
327 args.str(stringArgs);
328
329 while (!args.eof())
330 {
331 args >> arg;
332 if (!arg.empty())
333 {
334 listArgs.insert(arg);
335 }
336 }
337}
338
Matt Spinler47c09752018-11-29 14:54:13 -0600339void addObjectMapResult(
340 std::vector<interface_map_type::value_type>& objectMap,
Matt Spinler9f0958e2018-09-11 08:26:10 -0500341 const std::string& objectPath,
342 const std::pair<std::string, boost::container::flat_set<std::string>>&
343 interfaceMap)
344{
345 // Adds an object path/service name/interface list entry to
Matt Spinler47c09752018-11-29 14:54:13 -0600346 // the results of GetSubTree and GetAncestors.
Matt Spinler9f0958e2018-09-11 08:26:10 -0500347 // If an entry for the object path already exists, just add the
348 // service name and interfaces to that entry, otherwise create
349 // a new entry.
350 auto entry = std::find_if(
Matt Spinler47c09752018-11-29 14:54:13 -0600351 objectMap.begin(), objectMap.end(),
Matt Spinler9f0958e2018-09-11 08:26:10 -0500352 [&objectPath](const auto& i) { return objectPath == i.first; });
353
Matt Spinler47c09752018-11-29 14:54:13 -0600354 if (entry != objectMap.end())
Matt Spinler9f0958e2018-09-11 08:26:10 -0500355 {
356 entry->second.emplace(interfaceMap);
357 }
358 else
359 {
360 interface_map_type::value_type object;
361 object.first = objectPath;
362 object.second.emplace(interfaceMap);
Matt Spinler47c09752018-11-29 14:54:13 -0600363 objectMap.push_back(object);
Matt Spinler9f0958e2018-09-11 08:26:10 -0500364 }
365}
366
Matt Spinlera82779f2019-01-09 12:39:42 -0600367// Remove parents of the passed in path that:
368// 1) Only have the 3 default interfaces on them
369// - Means D-Bus created these, not application code,
370// with the Properties, Introspectable, and Peer ifaces
371// 2) Have no other child for this owner
372void removeUnneededParents(const std::string& objectPath,
373 const std::string& owner,
374 interface_map_type& interface_map)
375{
376 auto parent = objectPath;
377
378 while (true)
379 {
380 auto pos = parent.find_last_of('/');
381 if ((pos == std::string::npos) || (pos == 0))
382 {
383 break;
384 }
385 parent = parent.substr(0, pos);
386
387 auto parent_it = interface_map.find(parent);
388 if (parent_it == interface_map.end())
389 {
390 break;
391 }
392
393 auto ifaces_it = parent_it->second.find(owner);
394 if (ifaces_it == parent_it->second.end())
395 {
396 break;
397 }
398
399 if (ifaces_it->second.size() != 3)
400 {
401 break;
402 }
403
404 auto child_path = parent + '/';
405
406 // Remove this parent if there isn't a remaining child on this owner
407 auto child = std::find_if(
408 interface_map.begin(), interface_map.end(),
409 [&owner, &child_path](const auto& entry) {
410 return boost::starts_with(entry.first, child_path) &&
411 (entry.second.find(owner) != entry.second.end());
412 });
413
414 if (child == interface_map.end())
415 {
416 parent_it->second.erase(ifaces_it);
417 if (parent_it->second.empty())
418 {
419 interface_map.erase(parent_it);
420 }
421 }
422 else
423 {
424 break;
425 }
426 }
427}
428
Ed Tanous60520632018-06-11 17:46:52 -0700429int main(int argc, char** argv)
430{
Matt Spinlerdd945862018-09-07 12:41:05 -0500431 auto options = ArgumentParser(argc, argv);
Ed Tanous60520632018-06-11 17:46:52 -0700432 boost::asio::io_service io;
433 std::shared_ptr<sdbusplus::asio::connection> system_bus =
434 std::make_shared<sdbusplus::asio::connection>(io);
435
Matt Spinlerdd945862018-09-07 12:41:05 -0500436 splitArgs(options["service-namespaces"], service_whitelist);
Matt Spinlerdd945862018-09-07 12:41:05 -0500437 splitArgs(options["service-blacklists"], service_blacklist);
438
Ed Tanousd4dd96a2018-11-12 11:37:44 -0800439 // TODO(Ed) Remove this once all service files are updated to not use this.
440 // For now, simply squash the input, and ignore it.
441 boost::container::flat_set<std::string> iface_whitelist;
442 splitArgs(options["interface-namespaces"], iface_whitelist);
443
Ed Tanous60520632018-06-11 17:46:52 -0700444 system_bus->request_name(OBJECT_MAPPER_DBUS_NAME);
445 sdbusplus::asio::object_server server(system_bus);
446
447 // Construct a signal set registered for process termination.
448 boost::asio::signal_set signals(io, SIGINT, SIGTERM);
449 signals.async_wait([&io](const boost::system::error_code& error,
450 int signal_number) { io.stop(); });
451
452 interface_map_type interface_map;
453 boost::container::flat_map<std::string, std::string> name_owners;
454
455 std::function<void(sdbusplus::message::message & message)>
456 nameChangeHandler = [&interface_map, &io, &name_owners, &server,
457 system_bus](sdbusplus::message::message& message) {
Andrew Geissler20679262019-02-11 20:20:40 -0600458 std::string name; // well-known
459 std::string old_owner; // unique-name
460 std::string new_owner; // unique-name
Ed Tanous60520632018-06-11 17:46:52 -0700461
462 message.read(name, old_owner, new_owner);
463
464 if (!old_owner.empty())
465 {
Andrew Geissler20679262019-02-11 20:20:40 -0600466 processNameChangeDelete(name_owners, name, old_owner,
467 interface_map, associationOwners,
468 associationInterfaces, server);
Ed Tanous60520632018-06-11 17:46:52 -0700469 }
470
471 if (!new_owner.empty())
472 {
Matt Spinleraecabe82018-09-19 13:25:42 -0500473#ifdef DEBUG
Ed Tanous60520632018-06-11 17:46:52 -0700474 auto transaction = std::make_shared<
475 std::chrono::time_point<std::chrono::steady_clock>>(
476 std::chrono::steady_clock::now());
Matt Spinleraecabe82018-09-19 13:25:42 -0500477#endif
Ed Tanous60520632018-06-11 17:46:52 -0700478 // New daemon added
Andrew Geissler82815da2019-02-04 12:19:41 -0600479 if (needToIntrospect(name, service_whitelist,
480 service_blacklist))
Ed Tanous60520632018-06-11 17:46:52 -0700481 {
482 name_owners[new_owner] = name;
483 start_new_introspect(system_bus.get(), io, interface_map,
Matt Spinleraecabe82018-09-19 13:25:42 -0500484 name,
485#ifdef DEBUG
486 transaction,
487#endif
488 server);
Ed Tanous60520632018-06-11 17:46:52 -0700489 }
490 }
491 };
492
493 sdbusplus::bus::match::match nameOwnerChanged(
494 static_cast<sdbusplus::bus::bus&>(*system_bus),
495 sdbusplus::bus::match::rules::nameOwnerChanged(), nameChangeHandler);
496
497 std::function<void(sdbusplus::message::message & message)>
498 interfacesAddedHandler = [&interface_map, &name_owners, &server](
499 sdbusplus::message::message& message) {
500 sdbusplus::message::object_path obj_path;
Andrew Geissler70461892019-02-27 09:57:37 -0600501 InterfacesAdded interfaces_added;
Ed Tanous60520632018-06-11 17:46:52 -0700502 message.read(obj_path, interfaces_added);
503 std::string well_known;
Andrew Geissler3b025e62019-02-01 10:33:54 -0600504 if (!getWellKnown(name_owners, message.get_sender(), well_known))
Ed Tanous60520632018-06-11 17:46:52 -0700505 {
506 return; // only introspect well-known
507 }
Andrew Geissler82815da2019-02-04 12:19:41 -0600508 if (needToIntrospect(well_known, service_whitelist,
509 service_blacklist))
Ed Tanous60520632018-06-11 17:46:52 -0700510 {
Andrew Geissler70461892019-02-27 09:57:37 -0600511 processInterfaceAdded(interface_map, obj_path, interfaces_added,
512 well_known, associationOwners,
513 associationInterfaces, server);
Ed Tanous60520632018-06-11 17:46:52 -0700514 }
515 };
516
517 sdbusplus::bus::match::match interfacesAdded(
518 static_cast<sdbusplus::bus::bus&>(*system_bus),
519 sdbusplus::bus::match::rules::interfacesAdded(),
520 interfacesAddedHandler);
521
522 std::function<void(sdbusplus::message::message & message)>
523 interfacesRemovedHandler = [&interface_map, &name_owners, &server](
524 sdbusplus::message::message& message) {
525 sdbusplus::message::object_path obj_path;
526 std::vector<std::string> interfaces_removed;
527 message.read(obj_path, interfaces_removed);
528 auto connection_map = interface_map.find(obj_path.str);
529 if (connection_map == interface_map.end())
530 {
531 return;
532 }
533
534 std::string sender;
Andrew Geissler3b025e62019-02-01 10:33:54 -0600535 if (!getWellKnown(name_owners, message.get_sender(), sender))
Ed Tanous60520632018-06-11 17:46:52 -0700536 {
537 return;
538 }
539 for (const std::string& interface : interfaces_removed)
540 {
541 auto interface_set = connection_map->second.find(sender);
542 if (interface_set == connection_map->second.end())
543 {
544 continue;
545 }
546
Matt Spinler8f876a52019-04-15 13:22:50 -0500547 if (isAssocDefIface(interface))
Ed Tanous60520632018-06-11 17:46:52 -0700548 {
Andrew Geissler67e5a422019-02-04 13:00:59 -0600549 removeAssociation(obj_path.str, sender, server,
550 associationOwners, associationInterfaces);
Ed Tanous60520632018-06-11 17:46:52 -0700551 }
552
553 interface_set->second.erase(interface);
554 // If this was the last interface on this connection,
555 // erase the connection
556 if (interface_set->second.empty())
557 {
558 connection_map->second.erase(interface_set);
559 }
560 }
561 // If this was the last connection on this object path,
562 // erase the object path
563 if (connection_map->second.empty())
564 {
565 interface_map.erase(connection_map);
566 }
Matt Spinlera82779f2019-01-09 12:39:42 -0600567
568 removeUnneededParents(obj_path.str, sender, interface_map);
Ed Tanous60520632018-06-11 17:46:52 -0700569 };
570
571 sdbusplus::bus::match::match interfacesRemoved(
572 static_cast<sdbusplus::bus::bus&>(*system_bus),
573 sdbusplus::bus::match::rules::interfacesRemoved(),
574 interfacesRemovedHandler);
575
576 std::function<void(sdbusplus::message::message & message)>
Matt Spinler8f876a52019-04-15 13:22:50 -0500577 associationChangedHandler = [&server, &name_owners](
578 sdbusplus::message::message& message) {
579 std::string objectName;
580 boost::container::flat_map<
581 std::string,
582 sdbusplus::message::variant<std::vector<Association>>>
583 values;
584 message.read(objectName, values);
Matt Spinler937a2322019-01-23 13:54:22 -0600585
Matt Spinler8f876a52019-04-15 13:22:50 -0500586 auto prop =
587 std::find_if(values.begin(), values.end(), [](const auto& v) {
588 using namespace boost::algorithm;
589 return to_lower_copy(v.first) == "associations";
590 });
591
592 if (prop != values.end())
593 {
594 std::vector<Association> associations =
595 sdbusplus::message::variant_ns::get<
596 std::vector<Association>>(prop->second);
597
598 std::string well_known;
599 if (!getWellKnown(name_owners, message.get_sender(),
600 well_known))
601 {
602 return;
Ed Tanous60520632018-06-11 17:46:52 -0700603 }
Matt Spinler8f876a52019-04-15 13:22:50 -0500604 associationChanged(server, associations, message.get_path(),
605 well_known, associationOwners,
606 associationInterfaces);
607 }
608 };
609 sdbusplus::bus::match::match assocChangedMatch(
Ed Tanous60520632018-06-11 17:46:52 -0700610 static_cast<sdbusplus::bus::bus&>(*system_bus),
611 sdbusplus::bus::match::rules::interface(
612 "org.freedesktop.DBus.Properties") +
613 sdbusplus::bus::match::rules::member("PropertiesChanged") +
Matt Spinler8f876a52019-04-15 13:22:50 -0500614 sdbusplus::bus::match::rules::argN(0, assocDefsInterface),
615 associationChangedHandler);
616
617 sdbusplus::bus::match::match orgOpenbmcAssocChangedMatch(
618 static_cast<sdbusplus::bus::bus&>(*system_bus),
619 sdbusplus::bus::match::rules::interface(
620 "org.freedesktop.DBus.Properties") +
621 sdbusplus::bus::match::rules::member("PropertiesChanged") +
622 sdbusplus::bus::match::rules::argN(0, orgOpenBMCAssocDefsInterface),
Ed Tanous60520632018-06-11 17:46:52 -0700623 associationChangedHandler);
624
625 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
626 server.add_interface("/xyz/openbmc_project/object_mapper",
627 "xyz.openbmc_project.ObjectMapper");
628
629 iface->register_method(
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600630 "GetAncestors", [&interface_map](std::string& req_path,
Ed Tanous60520632018-06-11 17:46:52 -0700631 std::vector<std::string>& interfaces) {
632 // Interfaces need to be sorted for intersect to function
633 std::sort(interfaces.begin(), interfaces.end());
634
James Feistd7322872019-01-28 11:25:00 -0800635 if (boost::ends_with(req_path, "/"))
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600636 {
James Feistd7322872019-01-28 11:25:00 -0800637 req_path.pop_back();
638 }
639 if (req_path.size() &&
640 interface_map.find(req_path) == interface_map.end())
641 {
642 throw NotFoundException();
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600643 }
644
Ed Tanous60520632018-06-11 17:46:52 -0700645 std::vector<interface_map_type::value_type> ret;
646 for (auto& object_path : interface_map)
647 {
648 auto& this_path = object_path.first;
Matt Spinlerea6516c2018-09-25 16:23:13 -0500649 if (boost::starts_with(req_path, this_path) &&
650 (req_path != this_path))
Ed Tanous60520632018-06-11 17:46:52 -0700651 {
652 if (interfaces.empty())
653 {
654 ret.emplace_back(object_path);
655 }
656 else
657 {
658 for (auto& interface_map : object_path.second)
659 {
660
661 if (intersect(interfaces.begin(), interfaces.end(),
662 interface_map.second.begin(),
663 interface_map.second.end()))
664 {
Matt Spinler47c09752018-11-29 14:54:13 -0600665 addObjectMapResult(ret, this_path,
666 interface_map);
Ed Tanous60520632018-06-11 17:46:52 -0700667 }
668 }
669 }
670 }
671 }
Ed Tanous60520632018-06-11 17:46:52 -0700672
673 return ret;
674 });
675
676 iface->register_method(
677 "GetObject", [&interface_map](const std::string& path,
678 std::vector<std::string>& interfaces) {
Matt Spinler7a38d412018-09-18 16:13:25 -0500679 boost::container::flat_map<std::string,
680 boost::container::flat_set<std::string>>
681 results;
682
Ed Tanous60520632018-06-11 17:46:52 -0700683 // Interfaces need to be sorted for intersect to function
684 std::sort(interfaces.begin(), interfaces.end());
685 auto path_ref = interface_map.find(path);
686 if (path_ref == interface_map.end())
687 {
688 throw NotFoundException();
689 }
690 if (interfaces.empty())
691 {
692 return path_ref->second;
693 }
694 for (auto& interface_map : path_ref->second)
695 {
696 if (intersect(interfaces.begin(), interfaces.end(),
697 interface_map.second.begin(),
698 interface_map.second.end()))
699 {
Matt Spinler7a38d412018-09-18 16:13:25 -0500700 results.emplace(interface_map.first, interface_map.second);
Ed Tanous60520632018-06-11 17:46:52 -0700701 }
702 }
Matt Spinler7a38d412018-09-18 16:13:25 -0500703
704 if (results.empty())
705 {
706 throw NotFoundException();
707 }
708
709 return results;
Ed Tanous60520632018-06-11 17:46:52 -0700710 });
711
712 iface->register_method(
Matt Spinler153494e2018-11-07 16:35:40 -0600713 "GetSubTree", [&interface_map](std::string& req_path, int32_t depth,
714 std::vector<std::string>& interfaces) {
Ed Tanous60520632018-06-11 17:46:52 -0700715 if (depth <= 0)
716 {
717 depth = std::numeric_limits<int32_t>::max();
718 }
719 // Interfaces need to be sorted for intersect to function
720 std::sort(interfaces.begin(), interfaces.end());
721 std::vector<interface_map_type::value_type> ret;
722
James Feistd7322872019-01-28 11:25:00 -0800723 if (boost::ends_with(req_path, "/"))
Matt Spinler153494e2018-11-07 16:35:40 -0600724 {
James Feistd7322872019-01-28 11:25:00 -0800725 req_path.pop_back();
726 }
727 if (req_path.size() &&
728 interface_map.find(req_path) == interface_map.end())
729 {
730 throw NotFoundException();
Matt Spinler153494e2018-11-07 16:35:40 -0600731 }
732
Ed Tanous60520632018-06-11 17:46:52 -0700733 for (auto& object_path : interface_map)
734 {
735 auto& this_path = object_path.first;
Matt Spinler153494e2018-11-07 16:35:40 -0600736
737 if (this_path == req_path)
738 {
739 continue;
740 }
741
Ed Tanous60520632018-06-11 17:46:52 -0700742 if (boost::starts_with(this_path, req_path))
743 {
744 // count the number of slashes past the search term
745 int32_t this_depth =
746 std::count(this_path.begin() + req_path.size(),
747 this_path.end(), '/');
748 if (this_depth <= depth)
749 {
Ed Tanous60520632018-06-11 17:46:52 -0700750 for (auto& interface_map : object_path.second)
751 {
752 if (intersect(interfaces.begin(), interfaces.end(),
753 interface_map.second.begin(),
Matt Spinler9f0958e2018-09-11 08:26:10 -0500754 interface_map.second.end()) ||
755 interfaces.empty())
Ed Tanous60520632018-06-11 17:46:52 -0700756 {
Matt Spinler47c09752018-11-29 14:54:13 -0600757 addObjectMapResult(ret, this_path,
758 interface_map);
Ed Tanous60520632018-06-11 17:46:52 -0700759 }
760 }
Ed Tanous60520632018-06-11 17:46:52 -0700761 }
762 }
763 }
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600764
Ed Tanous60520632018-06-11 17:46:52 -0700765 return ret;
766 });
767
768 iface->register_method(
769 "GetSubTreePaths",
Matt Spinler153494e2018-11-07 16:35:40 -0600770 [&interface_map](std::string& req_path, int32_t depth,
Ed Tanous60520632018-06-11 17:46:52 -0700771 std::vector<std::string>& interfaces) {
772 if (depth <= 0)
773 {
774 depth = std::numeric_limits<int32_t>::max();
775 }
776 // Interfaces need to be sorted for intersect to function
777 std::sort(interfaces.begin(), interfaces.end());
778 std::vector<std::string> ret;
Matt Spinler153494e2018-11-07 16:35:40 -0600779
James Feistd7322872019-01-28 11:25:00 -0800780 if (boost::ends_with(req_path, "/"))
Matt Spinler153494e2018-11-07 16:35:40 -0600781 {
James Feistd7322872019-01-28 11:25:00 -0800782 req_path.pop_back();
783 }
784 if (req_path.size() &&
785 interface_map.find(req_path) == interface_map.end())
786 {
787 throw NotFoundException();
Matt Spinler153494e2018-11-07 16:35:40 -0600788 }
789
Ed Tanous60520632018-06-11 17:46:52 -0700790 for (auto& object_path : interface_map)
791 {
792 auto& this_path = object_path.first;
Matt Spinler153494e2018-11-07 16:35:40 -0600793
794 if (this_path == req_path)
795 {
796 continue;
797 }
798
Ed Tanous60520632018-06-11 17:46:52 -0700799 if (boost::starts_with(this_path, req_path))
800 {
801 // count the number of slashes past the search term
802 int this_depth =
803 std::count(this_path.begin() + req_path.size(),
804 this_path.end(), '/');
805 if (this_depth <= depth)
806 {
807 bool add = interfaces.empty();
808 for (auto& interface_map : object_path.second)
809 {
810 if (intersect(interfaces.begin(), interfaces.end(),
811 interface_map.second.begin(),
812 interface_map.second.end()))
813 {
814 add = true;
815 break;
816 }
817 }
818 if (add)
819 {
820 // TODO(ed) this is a copy
821 ret.emplace_back(this_path);
822 }
823 }
824 }
825 }
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600826
Ed Tanous60520632018-06-11 17:46:52 -0700827 return ret;
828 });
829
830 iface->initialize();
831
832 io.post([&]() {
833 doListNames(io, interface_map, system_bus.get(), name_owners, server);
834 });
835
Ed Tanous60520632018-06-11 17:46:52 -0700836 io.run();
837}