blob: 49b1344bfdf1af630d549238ad06b28a414e0fc5 [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 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,
Matt Spinlere0b0e3a2019-04-08 10:39:23 -0500131 interface_map_type& interfaceMap,
Ed Tanous60520632018-06-11 17:46:52 -0700132 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 Spinlere0b0e3a2019-04-08 10:39:23 -0500137 [&objectServer, path, processName, &interfaceMap](
Matt Spinler937a2322019-01-23 13:54:22 -0600138 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,
Matt Spinlere0b0e3a2019-04-08 10:39:23 -0500149 interfaceMap, associationMaps);
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 {
Matt Spinlere0b0e3a2019-04-08 10:39:23 -0500204 do_associations(system_bus, interface_map, 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,
Matt Spinlere2359fb2019-04-05 14:11:33 -0500468 interface_map, associationMaps, 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,
Matt Spinlere2359fb2019-04-05 14:11:33 -0500512 well_known, associationMaps, server);
Ed Tanous60520632018-06-11 17:46:52 -0700513 }
514 };
515
516 sdbusplus::bus::match::match interfacesAdded(
517 static_cast<sdbusplus::bus::bus&>(*system_bus),
518 sdbusplus::bus::match::rules::interfacesAdded(),
519 interfacesAddedHandler);
520
521 std::function<void(sdbusplus::message::message & message)>
522 interfacesRemovedHandler = [&interface_map, &name_owners, &server](
523 sdbusplus::message::message& message) {
524 sdbusplus::message::object_path obj_path;
525 std::vector<std::string> interfaces_removed;
526 message.read(obj_path, interfaces_removed);
527 auto connection_map = interface_map.find(obj_path.str);
528 if (connection_map == interface_map.end())
529 {
530 return;
531 }
532
533 std::string sender;
Andrew Geissler3b025e62019-02-01 10:33:54 -0600534 if (!getWellKnown(name_owners, message.get_sender(), sender))
Ed Tanous60520632018-06-11 17:46:52 -0700535 {
536 return;
537 }
538 for (const std::string& interface : interfaces_removed)
539 {
540 auto interface_set = connection_map->second.find(sender);
541 if (interface_set == connection_map->second.end())
542 {
543 continue;
544 }
545
Matt Spinler8f876a52019-04-15 13:22:50 -0500546 if (isAssocDefIface(interface))
Ed Tanous60520632018-06-11 17:46:52 -0700547 {
Andrew Geissler67e5a422019-02-04 13:00:59 -0600548 removeAssociation(obj_path.str, sender, server,
Matt Spinlere2359fb2019-04-05 14:11:33 -0500549 associationMaps);
Ed Tanous60520632018-06-11 17:46:52 -0700550 }
551
552 interface_set->second.erase(interface);
553 // If this was the last interface on this connection,
554 // erase the connection
555 if (interface_set->second.empty())
556 {
557 connection_map->second.erase(interface_set);
558 }
559 }
560 // If this was the last connection on this object path,
561 // erase the object path
562 if (connection_map->second.empty())
563 {
564 interface_map.erase(connection_map);
565 }
Matt Spinlera82779f2019-01-09 12:39:42 -0600566
567 removeUnneededParents(obj_path.str, sender, interface_map);
Ed Tanous60520632018-06-11 17:46:52 -0700568 };
569
570 sdbusplus::bus::match::match interfacesRemoved(
571 static_cast<sdbusplus::bus::bus&>(*system_bus),
572 sdbusplus::bus::match::rules::interfacesRemoved(),
573 interfacesRemovedHandler);
574
575 std::function<void(sdbusplus::message::message & message)>
Matt Spinlere0b0e3a2019-04-08 10:39:23 -0500576 associationChangedHandler = [&server, &name_owners, &interface_map](
Matt Spinler8f876a52019-04-15 13:22:50 -0500577 sdbusplus::message::message& message) {
578 std::string objectName;
579 boost::container::flat_map<
580 std::string,
581 sdbusplus::message::variant<std::vector<Association>>>
582 values;
583 message.read(objectName, values);
Matt Spinler937a2322019-01-23 13:54:22 -0600584
Matt Spinler8f876a52019-04-15 13:22:50 -0500585 auto prop =
586 std::find_if(values.begin(), values.end(), [](const auto& v) {
587 using namespace boost::algorithm;
588 return to_lower_copy(v.first) == "associations";
589 });
590
591 if (prop != values.end())
592 {
593 std::vector<Association> associations =
594 sdbusplus::message::variant_ns::get<
595 std::vector<Association>>(prop->second);
596
597 std::string well_known;
598 if (!getWellKnown(name_owners, message.get_sender(),
599 well_known))
600 {
601 return;
Ed Tanous60520632018-06-11 17:46:52 -0700602 }
Matt Spinler8f876a52019-04-15 13:22:50 -0500603 associationChanged(server, associations, message.get_path(),
Matt Spinlere0b0e3a2019-04-08 10:39:23 -0500604 well_known, interface_map, associationMaps);
Matt Spinler8f876a52019-04-15 13:22:50 -0500605 }
606 };
607 sdbusplus::bus::match::match assocChangedMatch(
Ed Tanous60520632018-06-11 17:46:52 -0700608 static_cast<sdbusplus::bus::bus&>(*system_bus),
609 sdbusplus::bus::match::rules::interface(
610 "org.freedesktop.DBus.Properties") +
611 sdbusplus::bus::match::rules::member("PropertiesChanged") +
Matt Spinler8f876a52019-04-15 13:22:50 -0500612 sdbusplus::bus::match::rules::argN(0, assocDefsInterface),
613 associationChangedHandler);
614
615 sdbusplus::bus::match::match orgOpenbmcAssocChangedMatch(
616 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") +
620 sdbusplus::bus::match::rules::argN(0, orgOpenBMCAssocDefsInterface),
Ed Tanous60520632018-06-11 17:46:52 -0700621 associationChangedHandler);
622
623 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
624 server.add_interface("/xyz/openbmc_project/object_mapper",
625 "xyz.openbmc_project.ObjectMapper");
626
627 iface->register_method(
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600628 "GetAncestors", [&interface_map](std::string& req_path,
Ed Tanous60520632018-06-11 17:46:52 -0700629 std::vector<std::string>& interfaces) {
630 // Interfaces need to be sorted for intersect to function
631 std::sort(interfaces.begin(), interfaces.end());
632
James Feistd7322872019-01-28 11:25:00 -0800633 if (boost::ends_with(req_path, "/"))
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600634 {
James Feistd7322872019-01-28 11:25:00 -0800635 req_path.pop_back();
636 }
637 if (req_path.size() &&
638 interface_map.find(req_path) == interface_map.end())
639 {
640 throw NotFoundException();
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600641 }
642
Ed Tanous60520632018-06-11 17:46:52 -0700643 std::vector<interface_map_type::value_type> ret;
644 for (auto& object_path : interface_map)
645 {
646 auto& this_path = object_path.first;
Matt Spinlerea6516c2018-09-25 16:23:13 -0500647 if (boost::starts_with(req_path, this_path) &&
648 (req_path != this_path))
Ed Tanous60520632018-06-11 17:46:52 -0700649 {
650 if (interfaces.empty())
651 {
652 ret.emplace_back(object_path);
653 }
654 else
655 {
656 for (auto& interface_map : object_path.second)
657 {
658
659 if (intersect(interfaces.begin(), interfaces.end(),
660 interface_map.second.begin(),
661 interface_map.second.end()))
662 {
Matt Spinler47c09752018-11-29 14:54:13 -0600663 addObjectMapResult(ret, this_path,
664 interface_map);
Ed Tanous60520632018-06-11 17:46:52 -0700665 }
666 }
667 }
668 }
669 }
Ed Tanous60520632018-06-11 17:46:52 -0700670
671 return ret;
672 });
673
674 iface->register_method(
675 "GetObject", [&interface_map](const std::string& path,
676 std::vector<std::string>& interfaces) {
Matt Spinler7a38d412018-09-18 16:13:25 -0500677 boost::container::flat_map<std::string,
678 boost::container::flat_set<std::string>>
679 results;
680
Ed Tanous60520632018-06-11 17:46:52 -0700681 // Interfaces need to be sorted for intersect to function
682 std::sort(interfaces.begin(), interfaces.end());
683 auto path_ref = interface_map.find(path);
684 if (path_ref == interface_map.end())
685 {
686 throw NotFoundException();
687 }
688 if (interfaces.empty())
689 {
690 return path_ref->second;
691 }
692 for (auto& interface_map : path_ref->second)
693 {
694 if (intersect(interfaces.begin(), interfaces.end(),
695 interface_map.second.begin(),
696 interface_map.second.end()))
697 {
Matt Spinler7a38d412018-09-18 16:13:25 -0500698 results.emplace(interface_map.first, interface_map.second);
Ed Tanous60520632018-06-11 17:46:52 -0700699 }
700 }
Matt Spinler7a38d412018-09-18 16:13:25 -0500701
702 if (results.empty())
703 {
704 throw NotFoundException();
705 }
706
707 return results;
Ed Tanous60520632018-06-11 17:46:52 -0700708 });
709
710 iface->register_method(
Matt Spinler153494e2018-11-07 16:35:40 -0600711 "GetSubTree", [&interface_map](std::string& req_path, int32_t depth,
712 std::vector<std::string>& interfaces) {
Ed Tanous60520632018-06-11 17:46:52 -0700713 if (depth <= 0)
714 {
715 depth = std::numeric_limits<int32_t>::max();
716 }
717 // Interfaces need to be sorted for intersect to function
718 std::sort(interfaces.begin(), interfaces.end());
719 std::vector<interface_map_type::value_type> ret;
720
James Feistd7322872019-01-28 11:25:00 -0800721 if (boost::ends_with(req_path, "/"))
Matt Spinler153494e2018-11-07 16:35:40 -0600722 {
James Feistd7322872019-01-28 11:25:00 -0800723 req_path.pop_back();
724 }
725 if (req_path.size() &&
726 interface_map.find(req_path) == interface_map.end())
727 {
728 throw NotFoundException();
Matt Spinler153494e2018-11-07 16:35:40 -0600729 }
730
Ed Tanous60520632018-06-11 17:46:52 -0700731 for (auto& object_path : interface_map)
732 {
733 auto& this_path = object_path.first;
Matt Spinler153494e2018-11-07 16:35:40 -0600734
735 if (this_path == req_path)
736 {
737 continue;
738 }
739
Ed Tanous60520632018-06-11 17:46:52 -0700740 if (boost::starts_with(this_path, req_path))
741 {
742 // count the number of slashes past the search term
743 int32_t this_depth =
744 std::count(this_path.begin() + req_path.size(),
745 this_path.end(), '/');
746 if (this_depth <= depth)
747 {
Ed Tanous60520632018-06-11 17:46:52 -0700748 for (auto& interface_map : object_path.second)
749 {
750 if (intersect(interfaces.begin(), interfaces.end(),
751 interface_map.second.begin(),
Matt Spinler9f0958e2018-09-11 08:26:10 -0500752 interface_map.second.end()) ||
753 interfaces.empty())
Ed Tanous60520632018-06-11 17:46:52 -0700754 {
Matt Spinler47c09752018-11-29 14:54:13 -0600755 addObjectMapResult(ret, this_path,
756 interface_map);
Ed Tanous60520632018-06-11 17:46:52 -0700757 }
758 }
Ed Tanous60520632018-06-11 17:46:52 -0700759 }
760 }
761 }
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600762
Ed Tanous60520632018-06-11 17:46:52 -0700763 return ret;
764 });
765
766 iface->register_method(
767 "GetSubTreePaths",
Matt Spinler153494e2018-11-07 16:35:40 -0600768 [&interface_map](std::string& req_path, int32_t depth,
Ed Tanous60520632018-06-11 17:46:52 -0700769 std::vector<std::string>& interfaces) {
770 if (depth <= 0)
771 {
772 depth = std::numeric_limits<int32_t>::max();
773 }
774 // Interfaces need to be sorted for intersect to function
775 std::sort(interfaces.begin(), interfaces.end());
776 std::vector<std::string> ret;
Matt Spinler153494e2018-11-07 16:35:40 -0600777
James Feistd7322872019-01-28 11:25:00 -0800778 if (boost::ends_with(req_path, "/"))
Matt Spinler153494e2018-11-07 16:35:40 -0600779 {
James Feistd7322872019-01-28 11:25:00 -0800780 req_path.pop_back();
781 }
782 if (req_path.size() &&
783 interface_map.find(req_path) == interface_map.end())
784 {
785 throw NotFoundException();
Matt Spinler153494e2018-11-07 16:35:40 -0600786 }
787
Ed Tanous60520632018-06-11 17:46:52 -0700788 for (auto& object_path : interface_map)
789 {
790 auto& this_path = object_path.first;
Matt Spinler153494e2018-11-07 16:35:40 -0600791
792 if (this_path == req_path)
793 {
794 continue;
795 }
796
Ed Tanous60520632018-06-11 17:46:52 -0700797 if (boost::starts_with(this_path, req_path))
798 {
799 // count the number of slashes past the search term
800 int this_depth =
801 std::count(this_path.begin() + req_path.size(),
802 this_path.end(), '/');
803 if (this_depth <= depth)
804 {
805 bool add = interfaces.empty();
806 for (auto& interface_map : object_path.second)
807 {
808 if (intersect(interfaces.begin(), interfaces.end(),
809 interface_map.second.begin(),
810 interface_map.second.end()))
811 {
812 add = true;
813 break;
814 }
815 }
816 if (add)
817 {
818 // TODO(ed) this is a copy
819 ret.emplace_back(this_path);
820 }
821 }
822 }
823 }
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600824
Ed Tanous60520632018-06-11 17:46:52 -0700825 return ret;
826 });
827
828 iface->initialize();
829
830 io.post([&]() {
831 doListNames(io, interface_map, system_bus.get(), name_owners, server);
832 });
833
Ed Tanous60520632018-06-11 17:46:52 -0700834 io.run();
835}