blob: e5d566efda64be91c26b002d3ceff15609d570b9 [file] [log] [blame]
Andrew Geisslera80a3af2019-02-04 14:01:49 -06001#include "associations.hpp"
Andrew Geissler3b025e62019-02-01 10:33:54 -06002#include "processing.hpp"
Matt Spinlerdd945862018-09-07 12:41:05 -05003#include "src/argument.hpp"
4
Ed Tanous60520632018-06-11 17:46:52 -07005#include <tinyxml2.h>
6
7#include <atomic>
8#include <boost/algorithm/string/predicate.hpp>
9#include <boost/container/flat_map.hpp>
Ed Tanous60520632018-06-11 17:46:52 -070010#include <chrono>
11#include <iomanip>
12#include <iostream>
13#include <sdbusplus/asio/connection.hpp>
14#include <sdbusplus/asio/object_server.hpp>
15
16constexpr const char* OBJECT_MAPPER_DBUS_NAME =
17 "xyz.openbmc_project.ObjectMapper";
Matt Spinler1036b4d2018-09-18 16:45:29 -050018constexpr const char* XYZ_ASSOCIATION_INTERFACE =
19 "xyz.openbmc_project.Association";
Ed Tanous60520632018-06-11 17:46:52 -070020
Ed Tanous60520632018-06-11 17:46:52 -070021using Association = std::tuple<std::string, std::string, std::string>;
22
Andrew Geissler67e5a422019-02-04 13:00:59 -060023AssociationInterfaces associationInterfaces;
Matt Spinler937a2322019-01-23 13:54:22 -060024AssociationOwnersType associationOwners;
25
Andrew Geissler82815da2019-02-04 12:19:41 -060026static WhiteBlackList service_whitelist;
27static WhiteBlackList service_blacklist;
Matt Spinlerdd945862018-09-07 12:41:05 -050028
Ed Tanous60520632018-06-11 17:46:52 -070029/** Exception thrown when a path is not found in the object list. */
30struct NotFoundException final : public sdbusplus::exception_t
31{
32 const char* name() const noexcept override
33 {
34 return "org.freedesktop.DBus.Error.FileNotFound";
35 };
36 const char* description() const noexcept override
37 {
38 return "path or object not found";
39 };
40 const char* what() const noexcept override
41 {
42 return "org.freedesktop.DBus.Error.FileNotFound: "
43 "The requested object was not found";
44 };
45};
46
Ed Tanous60520632018-06-11 17:46:52 -070047void update_owners(sdbusplus::asio::connection* conn,
48 boost::container::flat_map<std::string, std::string>& owners,
49 const std::string& new_object)
50{
51 if (boost::starts_with(new_object, ":"))
52 {
53 return;
54 }
55 conn->async_method_call(
56 [&, new_object](const boost::system::error_code ec,
57 const std::string& nameOwner) {
58 if (ec)
59 {
60 std::cerr << "Error getting owner of " << new_object << " : "
61 << ec << "\n";
62 return;
63 }
64 owners[nameOwner] = new_object;
65 },
66 "org.freedesktop.DBus", "/", "org.freedesktop.DBus", "GetNameOwner",
67 new_object);
68}
69
70void send_introspection_complete_signal(sdbusplus::asio::connection* system_bus,
71 const std::string& process_name)
72{
73 // TODO(ed) This signal doesn't get exposed properly in the
74 // introspect right now. Find out how to register signals in
75 // sdbusplus
76 sdbusplus::message::message m = system_bus->new_signal(
77 "/xyz/openbmc_project/object_mapper",
78 "xyz.openbmc_project.ObjectMapper.Private", "IntrospectionComplete");
79 m.append(process_name);
80 m.signal_send();
81}
82
83struct InProgressIntrospect
84{
85 InProgressIntrospect(
86 sdbusplus::asio::connection* system_bus, boost::asio::io_service& io,
Matt Spinleraecabe82018-09-19 13:25:42 -050087 const std::string& process_name
88#ifdef DEBUG
89 ,
Ed Tanous60520632018-06-11 17:46:52 -070090 std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>>
Matt Spinleraecabe82018-09-19 13:25:42 -050091 global_start_time
92#endif
93 ) :
Ed Tanous60520632018-06-11 17:46:52 -070094 system_bus(system_bus),
Matt Spinleraecabe82018-09-19 13:25:42 -050095 io(io), process_name(process_name)
96#ifdef DEBUG
97 ,
Ed Tanous60520632018-06-11 17:46:52 -070098 global_start_time(global_start_time),
99 process_start_time(std::chrono::steady_clock::now())
Matt Spinleraecabe82018-09-19 13:25:42 -0500100#endif
Ed Tanous60520632018-06-11 17:46:52 -0700101 {
102 }
103 ~InProgressIntrospect()
104 {
105 send_introspection_complete_signal(system_bus, process_name);
Matt Spinleraecabe82018-09-19 13:25:42 -0500106
107#ifdef DEBUG
Ed Tanous60520632018-06-11 17:46:52 -0700108 std::chrono::duration<float> diff =
109 std::chrono::steady_clock::now() - process_start_time;
110 std::cout << std::setw(50) << process_name << " scan took "
111 << diff.count() << " seconds\n";
112
113 // If we're the last outstanding caller globally, calculate the
114 // time it took
115 if (global_start_time != nullptr && global_start_time.use_count() == 1)
116 {
117 diff = std::chrono::steady_clock::now() - *global_start_time;
118 std::cout << "Total scan took " << diff.count()
119 << " seconds to complete\n";
120 }
Matt Spinleraecabe82018-09-19 13:25:42 -0500121#endif
Ed Tanous60520632018-06-11 17:46:52 -0700122 }
123 sdbusplus::asio::connection* system_bus;
124 boost::asio::io_service& io;
125 std::string process_name;
Matt Spinleraecabe82018-09-19 13:25:42 -0500126#ifdef DEBUG
Ed Tanous60520632018-06-11 17:46:52 -0700127 std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>>
128 global_start_time;
129 std::chrono::time_point<std::chrono::steady_clock> process_start_time;
Matt Spinleraecabe82018-09-19 13:25:42 -0500130#endif
Ed Tanous60520632018-06-11 17:46:52 -0700131};
132
Matt Spinler09be5762019-01-23 14:28:17 -0600133// Remove paths from the endpoints property of an association.
134// If the last endpoint was removed, then remove the whole
135// association object, otherwise just set the property.
136void removeAssociationEndpoints(
137 sdbusplus::asio::object_server& objectServer, const std::string& assocPath,
138 const std::string& owner,
139 const boost::container::flat_set<std::string>& endpointsToRemove)
140{
141 auto assoc = associationInterfaces.find(assocPath);
142 if (assoc == associationInterfaces.end())
143 {
144 return;
145 }
146
147 auto& endpointsInDBus = std::get<endpointsPos>(assoc->second);
148
149 for (const auto& endpointToRemove : endpointsToRemove)
150 {
151 auto e = std::find(endpointsInDBus.begin(), endpointsInDBus.end(),
152 endpointToRemove);
153
154 if (e != endpointsInDBus.end())
155 {
156 endpointsInDBus.erase(e);
157 }
158 }
159
160 if (endpointsInDBus.empty())
161 {
162 objectServer.remove_interface(std::get<ifacePos>(assoc->second));
163 std::get<ifacePos>(assoc->second) = nullptr;
164 std::get<endpointsPos>(assoc->second).clear();
165 }
166 else
167 {
168 std::get<ifacePos>(assoc->second)
169 ->set_property("endpoints", endpointsInDBus);
170 }
171}
172
173// Based on the latest values of the org.openbmc.Associations.associations
174// property, passed in via the newAssociations param, check if any of the
175// paths in the xyz.openbmc_project.Association.endpoints D-Bus property
176// for that association need to be removed. If the last path is removed
177// from the endpoints property, remove that whole association object from
178// D-Bus.
179void checkAssociationEndpointRemoves(
180 const std::string& sourcePath, const std::string& owner,
181 const AssociationPaths& newAssociations,
182 sdbusplus::asio::object_server& objectServer)
183{
184 // Find the services that have associations on this path.
185 auto originalOwners = associationOwners.find(sourcePath);
186 if (originalOwners == associationOwners.end())
187 {
188 return;
189 }
190
191 // Find the associations for this service
192 auto originalAssociations = originalOwners->second.find(owner);
193 if (originalAssociations == originalOwners->second.end())
194 {
195 return;
196 }
197
198 // Compare the new endpoints versus the original endpoints, and
199 // remove any of the original ones that aren't in the new list.
200 for (const auto& [originalAssocPath, originalEndpoints] :
201 originalAssociations->second)
202 {
203 // Check if this source even still has each association that
204 // was there previously, and if not, remove all of its endpoints
205 // from the D-Bus endpoints property which will cause the whole
206 // association path to be removed if no endpoints remain.
207 auto newEndpoints = newAssociations.find(originalAssocPath);
208 if (newEndpoints == newAssociations.end())
209 {
210 removeAssociationEndpoints(objectServer, originalAssocPath, owner,
211 originalEndpoints);
212 }
213 else
214 {
215 // The association is still there. Check if the endpoints
216 // changed.
217 boost::container::flat_set<std::string> toRemove;
218
219 for (auto& originalEndpoint : originalEndpoints)
220 {
221 if (std::find(newEndpoints->second.begin(),
222 newEndpoints->second.end(),
223 originalEndpoint) == newEndpoints->second.end())
224 {
225 toRemove.emplace(originalEndpoint);
226 }
227 }
228 if (!toRemove.empty())
229 {
230 removeAssociationEndpoints(objectServer, originalAssocPath,
231 owner, toRemove);
232 }
233 }
234 }
235}
236
Matt Spinler937a2322019-01-23 13:54:22 -0600237// Called when either a new org.openbmc.Associations interface was
238// created, or the associations property on that interface changed.
Matt Spinler09be5762019-01-23 14:28:17 -0600239void associationChanged(sdbusplus::asio::object_server& objectServer,
240 const std::vector<Association>& associations,
241 const std::string& path, const std::string& owner)
Ed Tanous60520632018-06-11 17:46:52 -0700242{
Matt Spinler937a2322019-01-23 13:54:22 -0600243 AssociationPaths objects;
244
Ed Tanous60520632018-06-11 17:46:52 -0700245 for (const Association& association : associations)
246 {
247 std::string forward;
248 std::string reverse;
249 std::string endpoint;
250 std::tie(forward, reverse, endpoint) = association;
251
252 if (forward.size())
253 {
254 objects[path + "/" + forward].emplace(endpoint);
255 }
256 if (reverse.size())
257 {
258 if (endpoint.empty())
259 {
260 std::cerr << "Found invalid association on path " << path
261 << "\n";
262 continue;
263 }
264 objects[endpoint + "/" + reverse].emplace(path);
265 }
266 }
267 for (const auto& object : objects)
268 {
269 // the mapper exposes the new association interface but intakes
270 // the old
271
272 auto& iface = associationInterfaces[object.first];
Matt Spinler1036b4d2018-09-18 16:45:29 -0500273 auto& i = std::get<ifacePos>(iface);
274 auto& endpoints = std::get<endpointsPos>(iface);
Matt Spinler1036b4d2018-09-18 16:45:29 -0500275
276 // Only add new endpoints
277 for (auto& e : object.second)
278 {
279 if (std::find(endpoints.begin(), endpoints.end(), e) ==
280 endpoints.end())
281 {
282 endpoints.push_back(e);
283 }
284 }
285
286 // If the interface already exists, only need to update
287 // the property value, otherwise create it
288 if (i)
289 {
290 i->set_property("endpoints", endpoints);
291 }
292 else
293 {
294 i = objectServer.add_interface(object.first,
295 XYZ_ASSOCIATION_INTERFACE);
296 i->register_property("endpoints", endpoints);
297 i->initialize();
298 }
299 }
Matt Spinler1036b4d2018-09-18 16:45:29 -0500300
Matt Spinler09be5762019-01-23 14:28:17 -0600301 // Check for endpoints being removed instead of added
302 checkAssociationEndpointRemoves(path, owner, objects, objectServer);
303
Matt Spinler937a2322019-01-23 13:54:22 -0600304 // Update associationOwners with the latest info
305 auto a = associationOwners.find(path);
306 if (a != associationOwners.end())
Matt Spinler1036b4d2018-09-18 16:45:29 -0500307 {
Matt Spinler937a2322019-01-23 13:54:22 -0600308 auto o = a->second.find(owner);
309 if (o != a->second.end())
Matt Spinler1036b4d2018-09-18 16:45:29 -0500310 {
Matt Spinler937a2322019-01-23 13:54:22 -0600311 o->second = std::move(objects);
Matt Spinler1036b4d2018-09-18 16:45:29 -0500312 }
313 else
314 {
Matt Spinler937a2322019-01-23 13:54:22 -0600315 a->second.emplace(owner, std::move(objects));
316 }
317 }
318 else
319 {
320 boost::container::flat_map<std::string, AssociationPaths> owners;
321 owners.emplace(owner, std::move(objects));
322 associationOwners.emplace(path, owners);
323 }
324}
Matt Spinler1036b4d2018-09-18 16:45:29 -0500325
Ed Tanous60520632018-06-11 17:46:52 -0700326void do_associations(sdbusplus::asio::connection* system_bus,
327 sdbusplus::asio::object_server& objectServer,
328 const std::string& processName, const std::string& path)
329{
330 system_bus->async_method_call(
Matt Spinler937a2322019-01-23 13:54:22 -0600331 [&objectServer, path, processName](
332 const boost::system::error_code ec,
333 const sdbusplus::message::variant<std::vector<Association>>&
334 variantAssociations) {
Ed Tanous60520632018-06-11 17:46:52 -0700335 if (ec)
336 {
337 std::cerr << "Error getting associations from " << path << "\n";
338 }
339 std::vector<Association> associations =
340 sdbusplus::message::variant_ns::get<std::vector<Association>>(
341 variantAssociations);
Matt Spinler09be5762019-01-23 14:28:17 -0600342 associationChanged(objectServer, associations, path, processName);
Ed Tanous60520632018-06-11 17:46:52 -0700343 },
344 processName, path, "org.freedesktop.DBus.Properties", "Get",
345 ASSOCIATIONS_INTERFACE, "associations");
346}
347
348void do_introspect(sdbusplus::asio::connection* system_bus,
349 std::shared_ptr<InProgressIntrospect> transaction,
350 interface_map_type& interface_map,
351 sdbusplus::asio::object_server& objectServer,
352 std::string path)
353{
354 system_bus->async_method_call(
355 [&interface_map, &objectServer, transaction, path,
356 system_bus](const boost::system::error_code ec,
357 const std::string& introspect_xml) {
358 if (ec)
359 {
360 std::cerr << "Introspect call failed with error: " << ec << ", "
361 << ec.message()
362 << " on process: " << transaction->process_name
363 << " path: " << path << "\n";
364 return;
365 }
366
367 tinyxml2::XMLDocument doc;
368
369 tinyxml2::XMLError e = doc.Parse(introspect_xml.c_str());
370 if (e != tinyxml2::XMLError::XML_SUCCESS)
371 {
372 std::cerr << "XML parsing failed\n";
373 return;
374 }
375
376 tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node");
377 if (pRoot == nullptr)
378 {
379 std::cerr << "XML document did not contain any data\n";
380 return;
381 }
382 auto& thisPathMap = interface_map[path];
Ed Tanous60520632018-06-11 17:46:52 -0700383 tinyxml2::XMLElement* pElement =
384 pRoot->FirstChildElement("interface");
385 while (pElement != nullptr)
386 {
387 const char* iface_name = pElement->Attribute("name");
388 if (iface_name == nullptr)
389 {
390 continue;
391 }
392
Matt Spinlerdd945862018-09-07 12:41:05 -0500393 std::string iface{iface_name};
394
Ed Tanousd4dd96a2018-11-12 11:37:44 -0800395 thisPathMap[transaction->process_name].emplace(iface_name);
396
Ed Tanous60520632018-06-11 17:46:52 -0700397 if (std::strcmp(iface_name, ASSOCIATIONS_INTERFACE) == 0)
398 {
399 do_associations(system_bus, objectServer,
400 transaction->process_name, path);
401 }
Ed Tanous60520632018-06-11 17:46:52 -0700402
403 pElement = pElement->NextSiblingElement("interface");
404 }
405
Ed Tanous50232cd2018-11-12 11:34:43 -0800406 pElement = pRoot->FirstChildElement("node");
407 while (pElement != nullptr)
Ed Tanous60520632018-06-11 17:46:52 -0700408 {
Ed Tanous50232cd2018-11-12 11:34:43 -0800409 const char* child_path = pElement->Attribute("name");
410 if (child_path != nullptr)
Ed Tanous60520632018-06-11 17:46:52 -0700411 {
Ed Tanous50232cd2018-11-12 11:34:43 -0800412 std::string parent_path(path);
413 if (parent_path == "/")
Ed Tanous60520632018-06-11 17:46:52 -0700414 {
Ed Tanous50232cd2018-11-12 11:34:43 -0800415 parent_path.clear();
Ed Tanous60520632018-06-11 17:46:52 -0700416 }
Ed Tanous50232cd2018-11-12 11:34:43 -0800417
418 do_introspect(system_bus, transaction, interface_map,
419 objectServer, parent_path + "/" + child_path);
Ed Tanous60520632018-06-11 17:46:52 -0700420 }
Ed Tanous50232cd2018-11-12 11:34:43 -0800421 pElement = pElement->NextSiblingElement("node");
Ed Tanous60520632018-06-11 17:46:52 -0700422 }
423 },
424 transaction->process_name, path, "org.freedesktop.DBus.Introspectable",
425 "Introspect");
426}
427
Ed Tanous60520632018-06-11 17:46:52 -0700428void start_new_introspect(
429 sdbusplus::asio::connection* system_bus, boost::asio::io_service& io,
430 interface_map_type& interface_map, const std::string& process_name,
Matt Spinleraecabe82018-09-19 13:25:42 -0500431#ifdef DEBUG
Ed Tanous60520632018-06-11 17:46:52 -0700432 std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>>
433 global_start_time,
Matt Spinleraecabe82018-09-19 13:25:42 -0500434#endif
Ed Tanous60520632018-06-11 17:46:52 -0700435 sdbusplus::asio::object_server& objectServer)
436{
Andrew Geissler82815da2019-02-04 12:19:41 -0600437 if (needToIntrospect(process_name, service_whitelist, service_blacklist))
Ed Tanous60520632018-06-11 17:46:52 -0700438 {
Ed Tanous60520632018-06-11 17:46:52 -0700439 std::shared_ptr<InProgressIntrospect> transaction =
Matt Spinleraecabe82018-09-19 13:25:42 -0500440 std::make_shared<InProgressIntrospect>(system_bus, io, process_name
441#ifdef DEBUG
442 ,
443 global_start_time
444#endif
445 );
Ed Tanous60520632018-06-11 17:46:52 -0700446
447 do_introspect(system_bus, transaction, interface_map, objectServer,
448 "/");
449 }
450}
451
452// TODO(ed) replace with std::set_intersection once c++17 is available
453template <class InputIt1, class InputIt2>
454bool intersect(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2)
455{
456 while (first1 != last1 && first2 != last2)
457 {
458 if (*first1 < *first2)
459 {
460 ++first1;
461 continue;
462 }
463 if (*first2 < *first1)
464 {
465 ++first2;
466 continue;
467 }
468 return true;
469 }
470 return false;
471}
472
473void doListNames(
474 boost::asio::io_service& io, interface_map_type& interface_map,
475 sdbusplus::asio::connection* system_bus,
476 boost::container::flat_map<std::string, std::string>& name_owners,
477 sdbusplus::asio::object_server& objectServer)
478{
479 system_bus->async_method_call(
480 [&io, &interface_map, &name_owners, &objectServer,
481 system_bus](const boost::system::error_code ec,
482 std::vector<std::string> process_names) {
483 if (ec)
484 {
485 std::cerr << "Error getting names: " << ec << "\n";
486 std::exit(EXIT_FAILURE);
487 return;
488 }
Ed Tanous60520632018-06-11 17:46:52 -0700489 // Try to make startup consistent
490 std::sort(process_names.begin(), process_names.end());
Matt Spinleraecabe82018-09-19 13:25:42 -0500491#ifdef DEBUG
Ed Tanous60520632018-06-11 17:46:52 -0700492 std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>>
493 global_start_time = std::make_shared<
494 std::chrono::time_point<std::chrono::steady_clock>>(
495 std::chrono::steady_clock::now());
Matt Spinleraecabe82018-09-19 13:25:42 -0500496#endif
Ed Tanous60520632018-06-11 17:46:52 -0700497 for (const std::string& process_name : process_names)
498 {
Andrew Geissler82815da2019-02-04 12:19:41 -0600499 if (needToIntrospect(process_name, service_whitelist,
500 service_blacklist))
Ed Tanous60520632018-06-11 17:46:52 -0700501 {
502 start_new_introspect(system_bus, io, interface_map,
Matt Spinleraecabe82018-09-19 13:25:42 -0500503 process_name,
504#ifdef DEBUG
505 global_start_time,
506#endif
Ed Tanous60520632018-06-11 17:46:52 -0700507 objectServer);
508 update_owners(system_bus, name_owners, process_name);
509 }
510 }
511 },
512 "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus",
513 "ListNames");
514}
515
Matt Spinlerdd945862018-09-07 12:41:05 -0500516void splitArgs(const std::string& stringArgs,
517 boost::container::flat_set<std::string>& listArgs)
518{
519 std::istringstream args;
520 std::string arg;
521
522 args.str(stringArgs);
523
524 while (!args.eof())
525 {
526 args >> arg;
527 if (!arg.empty())
528 {
529 listArgs.insert(arg);
530 }
531 }
532}
533
Matt Spinler47c09752018-11-29 14:54:13 -0600534void addObjectMapResult(
535 std::vector<interface_map_type::value_type>& objectMap,
Matt Spinler9f0958e2018-09-11 08:26:10 -0500536 const std::string& objectPath,
537 const std::pair<std::string, boost::container::flat_set<std::string>>&
538 interfaceMap)
539{
540 // Adds an object path/service name/interface list entry to
Matt Spinler47c09752018-11-29 14:54:13 -0600541 // the results of GetSubTree and GetAncestors.
Matt Spinler9f0958e2018-09-11 08:26:10 -0500542 // If an entry for the object path already exists, just add the
543 // service name and interfaces to that entry, otherwise create
544 // a new entry.
545 auto entry = std::find_if(
Matt Spinler47c09752018-11-29 14:54:13 -0600546 objectMap.begin(), objectMap.end(),
Matt Spinler9f0958e2018-09-11 08:26:10 -0500547 [&objectPath](const auto& i) { return objectPath == i.first; });
548
Matt Spinler47c09752018-11-29 14:54:13 -0600549 if (entry != objectMap.end())
Matt Spinler9f0958e2018-09-11 08:26:10 -0500550 {
551 entry->second.emplace(interfaceMap);
552 }
553 else
554 {
555 interface_map_type::value_type object;
556 object.first = objectPath;
557 object.second.emplace(interfaceMap);
Matt Spinler47c09752018-11-29 14:54:13 -0600558 objectMap.push_back(object);
Matt Spinler9f0958e2018-09-11 08:26:10 -0500559 }
560}
561
Matt Spinlera82779f2019-01-09 12:39:42 -0600562// Remove parents of the passed in path that:
563// 1) Only have the 3 default interfaces on them
564// - Means D-Bus created these, not application code,
565// with the Properties, Introspectable, and Peer ifaces
566// 2) Have no other child for this owner
567void removeUnneededParents(const std::string& objectPath,
568 const std::string& owner,
569 interface_map_type& interface_map)
570{
571 auto parent = objectPath;
572
573 while (true)
574 {
575 auto pos = parent.find_last_of('/');
576 if ((pos == std::string::npos) || (pos == 0))
577 {
578 break;
579 }
580 parent = parent.substr(0, pos);
581
582 auto parent_it = interface_map.find(parent);
583 if (parent_it == interface_map.end())
584 {
585 break;
586 }
587
588 auto ifaces_it = parent_it->second.find(owner);
589 if (ifaces_it == parent_it->second.end())
590 {
591 break;
592 }
593
594 if (ifaces_it->second.size() != 3)
595 {
596 break;
597 }
598
599 auto child_path = parent + '/';
600
601 // Remove this parent if there isn't a remaining child on this owner
602 auto child = std::find_if(
603 interface_map.begin(), interface_map.end(),
604 [&owner, &child_path](const auto& entry) {
605 return boost::starts_with(entry.first, child_path) &&
606 (entry.second.find(owner) != entry.second.end());
607 });
608
609 if (child == interface_map.end())
610 {
611 parent_it->second.erase(ifaces_it);
612 if (parent_it->second.empty())
613 {
614 interface_map.erase(parent_it);
615 }
616 }
617 else
618 {
619 break;
620 }
621 }
622}
623
Ed Tanous60520632018-06-11 17:46:52 -0700624int main(int argc, char** argv)
625{
Matt Spinlerdd945862018-09-07 12:41:05 -0500626 auto options = ArgumentParser(argc, argv);
Ed Tanous60520632018-06-11 17:46:52 -0700627 boost::asio::io_service io;
628 std::shared_ptr<sdbusplus::asio::connection> system_bus =
629 std::make_shared<sdbusplus::asio::connection>(io);
630
Matt Spinlerdd945862018-09-07 12:41:05 -0500631 splitArgs(options["service-namespaces"], service_whitelist);
Matt Spinlerdd945862018-09-07 12:41:05 -0500632 splitArgs(options["service-blacklists"], service_blacklist);
633
Ed Tanousd4dd96a2018-11-12 11:37:44 -0800634 // TODO(Ed) Remove this once all service files are updated to not use this.
635 // For now, simply squash the input, and ignore it.
636 boost::container::flat_set<std::string> iface_whitelist;
637 splitArgs(options["interface-namespaces"], iface_whitelist);
638
Ed Tanous60520632018-06-11 17:46:52 -0700639 system_bus->request_name(OBJECT_MAPPER_DBUS_NAME);
640 sdbusplus::asio::object_server server(system_bus);
641
642 // Construct a signal set registered for process termination.
643 boost::asio::signal_set signals(io, SIGINT, SIGTERM);
644 signals.async_wait([&io](const boost::system::error_code& error,
645 int signal_number) { io.stop(); });
646
647 interface_map_type interface_map;
648 boost::container::flat_map<std::string, std::string> name_owners;
649
650 std::function<void(sdbusplus::message::message & message)>
651 nameChangeHandler = [&interface_map, &io, &name_owners, &server,
652 system_bus](sdbusplus::message::message& message) {
Andrew Geissler20679262019-02-11 20:20:40 -0600653 std::string name; // well-known
654 std::string old_owner; // unique-name
655 std::string new_owner; // unique-name
Ed Tanous60520632018-06-11 17:46:52 -0700656
657 message.read(name, old_owner, new_owner);
658
659 if (!old_owner.empty())
660 {
Andrew Geissler20679262019-02-11 20:20:40 -0600661 processNameChangeDelete(name_owners, name, old_owner,
662 interface_map, associationOwners,
663 associationInterfaces, server);
Ed Tanous60520632018-06-11 17:46:52 -0700664 }
665
666 if (!new_owner.empty())
667 {
Matt Spinleraecabe82018-09-19 13:25:42 -0500668#ifdef DEBUG
Ed Tanous60520632018-06-11 17:46:52 -0700669 auto transaction = std::make_shared<
670 std::chrono::time_point<std::chrono::steady_clock>>(
671 std::chrono::steady_clock::now());
Matt Spinleraecabe82018-09-19 13:25:42 -0500672#endif
Ed Tanous60520632018-06-11 17:46:52 -0700673 // New daemon added
Andrew Geissler82815da2019-02-04 12:19:41 -0600674 if (needToIntrospect(name, service_whitelist,
675 service_blacklist))
Ed Tanous60520632018-06-11 17:46:52 -0700676 {
677 name_owners[new_owner] = name;
678 start_new_introspect(system_bus.get(), io, interface_map,
Matt Spinleraecabe82018-09-19 13:25:42 -0500679 name,
680#ifdef DEBUG
681 transaction,
682#endif
683 server);
Ed Tanous60520632018-06-11 17:46:52 -0700684 }
685 }
686 };
687
688 sdbusplus::bus::match::match nameOwnerChanged(
689 static_cast<sdbusplus::bus::bus&>(*system_bus),
690 sdbusplus::bus::match::rules::nameOwnerChanged(), nameChangeHandler);
691
692 std::function<void(sdbusplus::message::message & message)>
693 interfacesAddedHandler = [&interface_map, &name_owners, &server](
694 sdbusplus::message::message& message) {
695 sdbusplus::message::object_path obj_path;
696 std::vector<std::pair<
697 std::string, std::vector<std::pair<
698 std::string, sdbusplus::message::variant<
699 std::vector<Association>>>>>>
700 interfaces_added;
701 message.read(obj_path, interfaces_added);
702 std::string well_known;
Andrew Geissler3b025e62019-02-01 10:33:54 -0600703 if (!getWellKnown(name_owners, message.get_sender(), well_known))
Ed Tanous60520632018-06-11 17:46:52 -0700704 {
705 return; // only introspect well-known
706 }
Andrew Geissler82815da2019-02-04 12:19:41 -0600707 if (needToIntrospect(well_known, service_whitelist,
708 service_blacklist))
Ed Tanous60520632018-06-11 17:46:52 -0700709 {
710 auto& iface_list = interface_map[obj_path.str];
711
Matt Spinlercc74e4b2018-09-18 16:21:54 -0500712 for (const auto& interface_pair : interfaces_added)
Ed Tanous60520632018-06-11 17:46:52 -0700713 {
714 iface_list[well_known].emplace(interface_pair.first);
715
716 if (interface_pair.first == ASSOCIATIONS_INTERFACE)
717 {
718 const sdbusplus::message::variant<
719 std::vector<Association>>* variantAssociations =
720 nullptr;
721 for (const auto& interface : interface_pair.second)
722 {
723 if (interface.first == "associations")
724 {
725 variantAssociations = &(interface.second);
726 }
727 }
728 if (variantAssociations == nullptr)
729 {
730 std::cerr << "Illegal association found on "
731 << well_known << "\n";
732 continue;
733 }
734 std::vector<Association> associations =
735 sdbusplus::message::variant_ns::get<
736 std::vector<Association>>(*variantAssociations);
Matt Spinler09be5762019-01-23 14:28:17 -0600737 associationChanged(server, associations, obj_path.str,
738 well_known);
Ed Tanous60520632018-06-11 17:46:52 -0700739 }
740 }
Matt Spinler1b4a5022019-01-03 14:48:33 -0600741
742 // To handle the case where an object path is being created
743 // with 2 or more new path segments, check if the parent paths
744 // of this path are already in the interface map, and add them
745 // if they aren't with just the default freedesktop interfaces.
746 // This would be done via introspection if they would have
747 // already existed at startup. While we could also introspect
748 // them now to do the work, we know there aren't any other
749 // interfaces or we would have gotten signals for them as well,
750 // so take a shortcut to speed things up.
751 //
752 // This is all needed so that mapper operations can be done
753 // on the new parent paths.
754 using iface_map_iterator = interface_map_type::iterator;
755 using iface_map_value_type = boost::container::flat_map<
756 std::string, boost::container::flat_set<std::string>>;
757 using name_map_iterator = iface_map_value_type::iterator;
758
759 static const boost::container::flat_set<std::string>
760 default_ifaces{"org.freedesktop.DBus.Introspectable",
761 "org.freedesktop.DBus.Peer",
762 "org.freedesktop.DBus.Properties"};
763
764 std::string parent = obj_path.str;
765 auto pos = parent.find_last_of('/');
766
767 while (pos != std::string::npos)
768 {
769 parent = parent.substr(0, pos);
770
771 std::pair<iface_map_iterator, bool> parentEntry =
772 interface_map.insert(
773 std::make_pair(parent, iface_map_value_type{}));
774
775 std::pair<name_map_iterator, bool> ifaceEntry =
776 parentEntry.first->second.insert(
777 std::make_pair(well_known, default_ifaces));
778
779 if (!ifaceEntry.second)
780 {
781 // Entry was already there for this name so done.
782 break;
783 }
784
785 pos = parent.find_last_of('/');
786 }
Ed Tanous60520632018-06-11 17:46:52 -0700787 }
788 };
789
790 sdbusplus::bus::match::match interfacesAdded(
791 static_cast<sdbusplus::bus::bus&>(*system_bus),
792 sdbusplus::bus::match::rules::interfacesAdded(),
793 interfacesAddedHandler);
794
795 std::function<void(sdbusplus::message::message & message)>
796 interfacesRemovedHandler = [&interface_map, &name_owners, &server](
797 sdbusplus::message::message& message) {
798 sdbusplus::message::object_path obj_path;
799 std::vector<std::string> interfaces_removed;
800 message.read(obj_path, interfaces_removed);
801 auto connection_map = interface_map.find(obj_path.str);
802 if (connection_map == interface_map.end())
803 {
804 return;
805 }
806
807 std::string sender;
Andrew Geissler3b025e62019-02-01 10:33:54 -0600808 if (!getWellKnown(name_owners, message.get_sender(), sender))
Ed Tanous60520632018-06-11 17:46:52 -0700809 {
810 return;
811 }
812 for (const std::string& interface : interfaces_removed)
813 {
814 auto interface_set = connection_map->second.find(sender);
815 if (interface_set == connection_map->second.end())
816 {
817 continue;
818 }
819
820 if (interface == ASSOCIATIONS_INTERFACE)
821 {
Andrew Geissler67e5a422019-02-04 13:00:59 -0600822 removeAssociation(obj_path.str, sender, server,
823 associationOwners, associationInterfaces);
Ed Tanous60520632018-06-11 17:46:52 -0700824 }
825
826 interface_set->second.erase(interface);
827 // If this was the last interface on this connection,
828 // erase the connection
829 if (interface_set->second.empty())
830 {
831 connection_map->second.erase(interface_set);
832 }
833 }
834 // If this was the last connection on this object path,
835 // erase the object path
836 if (connection_map->second.empty())
837 {
838 interface_map.erase(connection_map);
839 }
Matt Spinlera82779f2019-01-09 12:39:42 -0600840
841 removeUnneededParents(obj_path.str, sender, interface_map);
Ed Tanous60520632018-06-11 17:46:52 -0700842 };
843
844 sdbusplus::bus::match::match interfacesRemoved(
845 static_cast<sdbusplus::bus::bus&>(*system_bus),
846 sdbusplus::bus::match::rules::interfacesRemoved(),
847 interfacesRemovedHandler);
848
849 std::function<void(sdbusplus::message::message & message)>
850 associationChangedHandler =
Matt Spinler937a2322019-01-23 13:54:22 -0600851 [&server, &name_owners](sdbusplus::message::message& message) {
Ed Tanous60520632018-06-11 17:46:52 -0700852 std::string objectName;
853 boost::container::flat_map<
854 std::string,
855 sdbusplus::message::variant<std::vector<Association>>>
856 values;
857 message.read(objectName, values);
858 auto findAssociations = values.find("associations");
859 if (findAssociations != values.end())
860 {
861 std::vector<Association> associations =
862 sdbusplus::message::variant_ns::get<
863 std::vector<Association>>(findAssociations->second);
Matt Spinler937a2322019-01-23 13:54:22 -0600864
865 std::string well_known;
Andrew Geissler3b025e62019-02-01 10:33:54 -0600866 if (!getWellKnown(name_owners, message.get_sender(),
867 well_known))
Matt Spinler937a2322019-01-23 13:54:22 -0600868 {
869 return;
870 }
Matt Spinler09be5762019-01-23 14:28:17 -0600871 associationChanged(server, associations, message.get_path(),
872 well_known);
Ed Tanous60520632018-06-11 17:46:52 -0700873 }
874 };
875 sdbusplus::bus::match::match associationChanged(
876 static_cast<sdbusplus::bus::bus&>(*system_bus),
877 sdbusplus::bus::match::rules::interface(
878 "org.freedesktop.DBus.Properties") +
879 sdbusplus::bus::match::rules::member("PropertiesChanged") +
880 sdbusplus::bus::match::rules::argN(0, ASSOCIATIONS_INTERFACE),
881 associationChangedHandler);
882
883 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
884 server.add_interface("/xyz/openbmc_project/object_mapper",
885 "xyz.openbmc_project.ObjectMapper");
886
887 iface->register_method(
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600888 "GetAncestors", [&interface_map](std::string& req_path,
Ed Tanous60520632018-06-11 17:46:52 -0700889 std::vector<std::string>& interfaces) {
890 // Interfaces need to be sorted for intersect to function
891 std::sort(interfaces.begin(), interfaces.end());
892
James Feistd7322872019-01-28 11:25:00 -0800893 if (boost::ends_with(req_path, "/"))
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600894 {
James Feistd7322872019-01-28 11:25:00 -0800895 req_path.pop_back();
896 }
897 if (req_path.size() &&
898 interface_map.find(req_path) == interface_map.end())
899 {
900 throw NotFoundException();
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600901 }
902
Ed Tanous60520632018-06-11 17:46:52 -0700903 std::vector<interface_map_type::value_type> ret;
904 for (auto& object_path : interface_map)
905 {
906 auto& this_path = object_path.first;
Matt Spinlerea6516c2018-09-25 16:23:13 -0500907 if (boost::starts_with(req_path, this_path) &&
908 (req_path != this_path))
Ed Tanous60520632018-06-11 17:46:52 -0700909 {
910 if (interfaces.empty())
911 {
912 ret.emplace_back(object_path);
913 }
914 else
915 {
916 for (auto& interface_map : object_path.second)
917 {
918
919 if (intersect(interfaces.begin(), interfaces.end(),
920 interface_map.second.begin(),
921 interface_map.second.end()))
922 {
Matt Spinler47c09752018-11-29 14:54:13 -0600923 addObjectMapResult(ret, this_path,
924 interface_map);
Ed Tanous60520632018-06-11 17:46:52 -0700925 }
926 }
927 }
928 }
929 }
Ed Tanous60520632018-06-11 17:46:52 -0700930
931 return ret;
932 });
933
934 iface->register_method(
935 "GetObject", [&interface_map](const std::string& path,
936 std::vector<std::string>& interfaces) {
Matt Spinler7a38d412018-09-18 16:13:25 -0500937 boost::container::flat_map<std::string,
938 boost::container::flat_set<std::string>>
939 results;
940
Ed Tanous60520632018-06-11 17:46:52 -0700941 // Interfaces need to be sorted for intersect to function
942 std::sort(interfaces.begin(), interfaces.end());
943 auto path_ref = interface_map.find(path);
944 if (path_ref == interface_map.end())
945 {
946 throw NotFoundException();
947 }
948 if (interfaces.empty())
949 {
950 return path_ref->second;
951 }
952 for (auto& interface_map : path_ref->second)
953 {
954 if (intersect(interfaces.begin(), interfaces.end(),
955 interface_map.second.begin(),
956 interface_map.second.end()))
957 {
Matt Spinler7a38d412018-09-18 16:13:25 -0500958 results.emplace(interface_map.first, interface_map.second);
Ed Tanous60520632018-06-11 17:46:52 -0700959 }
960 }
Matt Spinler7a38d412018-09-18 16:13:25 -0500961
962 if (results.empty())
963 {
964 throw NotFoundException();
965 }
966
967 return results;
Ed Tanous60520632018-06-11 17:46:52 -0700968 });
969
970 iface->register_method(
Matt Spinler153494e2018-11-07 16:35:40 -0600971 "GetSubTree", [&interface_map](std::string& req_path, int32_t depth,
972 std::vector<std::string>& interfaces) {
Ed Tanous60520632018-06-11 17:46:52 -0700973 if (depth <= 0)
974 {
975 depth = std::numeric_limits<int32_t>::max();
976 }
977 // Interfaces need to be sorted for intersect to function
978 std::sort(interfaces.begin(), interfaces.end());
979 std::vector<interface_map_type::value_type> ret;
980
James Feistd7322872019-01-28 11:25:00 -0800981 if (boost::ends_with(req_path, "/"))
Matt Spinler153494e2018-11-07 16:35:40 -0600982 {
James Feistd7322872019-01-28 11:25:00 -0800983 req_path.pop_back();
984 }
985 if (req_path.size() &&
986 interface_map.find(req_path) == interface_map.end())
987 {
988 throw NotFoundException();
Matt Spinler153494e2018-11-07 16:35:40 -0600989 }
990
Ed Tanous60520632018-06-11 17:46:52 -0700991 for (auto& object_path : interface_map)
992 {
993 auto& this_path = object_path.first;
Matt Spinler153494e2018-11-07 16:35:40 -0600994
995 if (this_path == req_path)
996 {
997 continue;
998 }
999
Ed Tanous60520632018-06-11 17:46:52 -07001000 if (boost::starts_with(this_path, req_path))
1001 {
1002 // count the number of slashes past the search term
1003 int32_t this_depth =
1004 std::count(this_path.begin() + req_path.size(),
1005 this_path.end(), '/');
1006 if (this_depth <= depth)
1007 {
Ed Tanous60520632018-06-11 17:46:52 -07001008 for (auto& interface_map : object_path.second)
1009 {
1010 if (intersect(interfaces.begin(), interfaces.end(),
1011 interface_map.second.begin(),
Matt Spinler9f0958e2018-09-11 08:26:10 -05001012 interface_map.second.end()) ||
1013 interfaces.empty())
Ed Tanous60520632018-06-11 17:46:52 -07001014 {
Matt Spinler47c09752018-11-29 14:54:13 -06001015 addObjectMapResult(ret, this_path,
1016 interface_map);
Ed Tanous60520632018-06-11 17:46:52 -07001017 }
1018 }
Ed Tanous60520632018-06-11 17:46:52 -07001019 }
1020 }
1021 }
Matt Spinler6a39e8c2018-11-13 11:11:36 -06001022
Ed Tanous60520632018-06-11 17:46:52 -07001023 return ret;
1024 });
1025
1026 iface->register_method(
1027 "GetSubTreePaths",
Matt Spinler153494e2018-11-07 16:35:40 -06001028 [&interface_map](std::string& req_path, int32_t depth,
Ed Tanous60520632018-06-11 17:46:52 -07001029 std::vector<std::string>& interfaces) {
1030 if (depth <= 0)
1031 {
1032 depth = std::numeric_limits<int32_t>::max();
1033 }
1034 // Interfaces need to be sorted for intersect to function
1035 std::sort(interfaces.begin(), interfaces.end());
1036 std::vector<std::string> ret;
Matt Spinler153494e2018-11-07 16:35:40 -06001037
James Feistd7322872019-01-28 11:25:00 -08001038 if (boost::ends_with(req_path, "/"))
Matt Spinler153494e2018-11-07 16:35:40 -06001039 {
James Feistd7322872019-01-28 11:25:00 -08001040 req_path.pop_back();
1041 }
1042 if (req_path.size() &&
1043 interface_map.find(req_path) == interface_map.end())
1044 {
1045 throw NotFoundException();
Matt Spinler153494e2018-11-07 16:35:40 -06001046 }
1047
Ed Tanous60520632018-06-11 17:46:52 -07001048 for (auto& object_path : interface_map)
1049 {
1050 auto& this_path = object_path.first;
Matt Spinler153494e2018-11-07 16:35:40 -06001051
1052 if (this_path == req_path)
1053 {
1054 continue;
1055 }
1056
Ed Tanous60520632018-06-11 17:46:52 -07001057 if (boost::starts_with(this_path, req_path))
1058 {
1059 // count the number of slashes past the search term
1060 int this_depth =
1061 std::count(this_path.begin() + req_path.size(),
1062 this_path.end(), '/');
1063 if (this_depth <= depth)
1064 {
1065 bool add = interfaces.empty();
1066 for (auto& interface_map : object_path.second)
1067 {
1068 if (intersect(interfaces.begin(), interfaces.end(),
1069 interface_map.second.begin(),
1070 interface_map.second.end()))
1071 {
1072 add = true;
1073 break;
1074 }
1075 }
1076 if (add)
1077 {
1078 // TODO(ed) this is a copy
1079 ret.emplace_back(this_path);
1080 }
1081 }
1082 }
1083 }
Matt Spinler6a39e8c2018-11-13 11:11:36 -06001084
Ed Tanous60520632018-06-11 17:46:52 -07001085 return ret;
1086 });
1087
1088 iface->initialize();
1089
1090 io.post([&]() {
1091 doListNames(io, interface_map, system_bus.get(), name_owners, server);
1092 });
1093
Ed Tanous60520632018-06-11 17:46:52 -07001094 io.run();
1095}