blob: 53b36d54b61ac785b4ec979423de9be7503079c2 [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>
9#include <boost/algorithm/string/predicate.hpp>
Ed Tanous21c60592020-08-17 23:43:46 -070010#include <boost/asio/io_context.hpp>
11#include <boost/asio/signal_set.hpp>
Ed Tanous60520632018-06-11 17:46:52 -070012#include <boost/container/flat_map.hpp>
Ed Tanous60520632018-06-11 17:46:52 -070013#include <chrono>
14#include <iomanip>
15#include <iostream>
16#include <sdbusplus/asio/connection.hpp>
17#include <sdbusplus/asio/object_server.hpp>
18
Matt Spinlere2359fb2019-04-05 14:11:33 -050019AssociationMaps associationMaps;
Matt Spinler937a2322019-01-23 13:54:22 -060020
Andrew Geissler82815da2019-02-04 12:19:41 -060021static WhiteBlackList service_whitelist;
22static WhiteBlackList service_blacklist;
Matt Spinlerdd945862018-09-07 12:41:05 -050023
Ed Tanous60520632018-06-11 17:46:52 -070024/** Exception thrown when a path is not found in the object list. */
25struct NotFoundException final : public sdbusplus::exception_t
26{
27 const char* name() const noexcept override
28 {
Patrick Williams3735ea22020-11-11 13:12:48 -060029 return "xyz.openbmc_project.Common.Error.ResourceNotFound";
Ed Tanous60520632018-06-11 17:46:52 -070030 };
31 const char* description() const noexcept override
32 {
33 return "path or object not found";
34 };
35 const char* what() const noexcept override
36 {
Patrick Williams3735ea22020-11-11 13:12:48 -060037 return "xyz.openbmc_project.Common.Error.ResourceNotFound: "
38 "The resource is not found.";
Ed Tanous60520632018-06-11 17:46:52 -070039 };
Patrick Williamsbedd4162021-09-08 13:54:13 -050040
41 int get_errno() const noexcept override
42 {
43 return ENOENT;
44 }
Ed Tanous60520632018-06-11 17:46:52 -070045};
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(
Brad Bishopa02cd542021-10-12 19:12:42 -040077 "/xyz/openbmc_project/object_mapper",
78 "xyz.openbmc_project.ObjectMapper.Private", "IntrospectionComplete");
Ed Tanous60520632018-06-11 17:46:52 -070079 m.append(process_name);
80 m.signal_send();
81}
82
83struct InProgressIntrospect
84{
85 InProgressIntrospect(
Ed Tanous21c60592020-08-17 23:43:46 -070086 sdbusplus::asio::connection* system_bus, boost::asio::io_context& io,
Matt Spinler11401e22019-04-08 13:13:25 -050087 const std::string& process_name, AssociationMaps& am
Matt Spinleraecabe82018-09-19 13:25:42 -050088#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 Spinler11401e22019-04-08 13:13:25 -050095 io(io), process_name(process_name), assocMaps(am)
Matt Spinleraecabe82018-09-19 13:25:42 -050096#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;
Ed Tanous21c60592020-08-17 23:43:46 -0700124 boost::asio::io_context& io;
Ed Tanous60520632018-06-11 17:46:52 -0700125 std::string process_name;
Matt Spinler11401e22019-04-08 13:13:25 -0500126 AssociationMaps& assocMaps;
Matt Spinleraecabe82018-09-19 13:25:42 -0500127#ifdef DEBUG
Ed Tanous60520632018-06-11 17:46:52 -0700128 std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>>
129 global_start_time;
130 std::chrono::time_point<std::chrono::steady_clock> process_start_time;
Matt Spinleraecabe82018-09-19 13:25:42 -0500131#endif
Ed Tanous60520632018-06-11 17:46:52 -0700132};
133
Ed Tanous60520632018-06-11 17:46:52 -0700134void do_associations(sdbusplus::asio::connection* system_bus,
Matt Spinlere0b0e3a2019-04-08 10:39:23 -0500135 interface_map_type& interfaceMap,
Ed Tanous60520632018-06-11 17:46:52 -0700136 sdbusplus::asio::object_server& objectServer,
Adrian Ambrożewiczbb40bd32021-02-12 13:36:26 +0100137 const std::string& processName, const std::string& path,
138 int timeoutRetries = 0)
Ed Tanous60520632018-06-11 17:46:52 -0700139{
Adrian Ambrożewiczbb40bd32021-02-12 13:36:26 +0100140 constexpr int maxTimeoutRetries = 3;
Ed Tanous60520632018-06-11 17:46:52 -0700141 system_bus->async_method_call(
Adrian Ambrożewiczbb40bd32021-02-12 13:36:26 +0100142 [&objectServer, path, processName, &interfaceMap, system_bus,
143 timeoutRetries](
Matt Spinler937a2322019-01-23 13:54:22 -0600144 const boost::system::error_code ec,
Patrick Williams2bb2d6b2020-05-13 17:59:02 -0500145 const std::variant<std::vector<Association>>& variantAssociations) {
Ed Tanous60520632018-06-11 17:46:52 -0700146 if (ec)
147 {
Adrian Ambrożewiczbb40bd32021-02-12 13:36:26 +0100148 if (ec.value() == boost::system::errc::timed_out &&
149 timeoutRetries < maxTimeoutRetries)
150 {
151 do_associations(system_bus, interfaceMap, objectServer,
152 processName, path, timeoutRetries + 1);
153 return;
154 }
Ed Tanous60520632018-06-11 17:46:52 -0700155 std::cerr << "Error getting associations from " << path << "\n";
156 }
157 std::vector<Association> associations =
Patrick Williamsb05bc122020-05-13 12:21:00 -0500158 std::get<std::vector<Association>>(variantAssociations);
Andrew Geissler4511b332019-02-21 15:40:40 -0600159 associationChanged(objectServer, associations, path, processName,
Matt Spinlere0b0e3a2019-04-08 10:39:23 -0500160 interfaceMap, associationMaps);
Ed Tanous60520632018-06-11 17:46:52 -0700161 },
162 processName, path, "org.freedesktop.DBus.Properties", "Get",
John Wangd0cf9422019-09-17 16:01:34 +0800163 assocDefsInterface, assocDefsProperty);
Ed Tanous60520632018-06-11 17:46:52 -0700164}
165
166void do_introspect(sdbusplus::asio::connection* system_bus,
167 std::shared_ptr<InProgressIntrospect> transaction,
168 interface_map_type& interface_map,
169 sdbusplus::asio::object_server& objectServer,
Vernon Maueryc52be0d2020-01-14 11:14:25 -0800170 std::string path, int timeoutRetries = 0)
Ed Tanous60520632018-06-11 17:46:52 -0700171{
Vernon Maueryc52be0d2020-01-14 11:14:25 -0800172 constexpr int maxTimeoutRetries = 3;
Ed Tanous60520632018-06-11 17:46:52 -0700173 system_bus->async_method_call(
Vernon Maueryc52be0d2020-01-14 11:14:25 -0800174 [&interface_map, &objectServer, transaction, path, system_bus,
175 timeoutRetries](const boost::system::error_code ec,
176 const std::string& introspect_xml) {
Ed Tanous60520632018-06-11 17:46:52 -0700177 if (ec)
178 {
Vernon Maueryc52be0d2020-01-14 11:14:25 -0800179 if (ec.value() == boost::system::errc::timed_out &&
180 timeoutRetries < maxTimeoutRetries)
181 {
182 do_introspect(system_bus, transaction, interface_map,
183 objectServer, path, timeoutRetries + 1);
184 return;
185 }
Ed Tanous60520632018-06-11 17:46:52 -0700186 std::cerr << "Introspect call failed with error: " << ec << ", "
187 << ec.message()
188 << " on process: " << transaction->process_name
189 << " path: " << path << "\n";
190 return;
191 }
192
193 tinyxml2::XMLDocument doc;
194
195 tinyxml2::XMLError e = doc.Parse(introspect_xml.c_str());
196 if (e != tinyxml2::XMLError::XML_SUCCESS)
197 {
198 std::cerr << "XML parsing failed\n";
199 return;
200 }
201
202 tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node");
203 if (pRoot == nullptr)
204 {
205 std::cerr << "XML document did not contain any data\n";
206 return;
207 }
208 auto& thisPathMap = interface_map[path];
Ed Tanous60520632018-06-11 17:46:52 -0700209 tinyxml2::XMLElement* pElement =
210 pRoot->FirstChildElement("interface");
211 while (pElement != nullptr)
212 {
213 const char* iface_name = pElement->Attribute("name");
214 if (iface_name == nullptr)
215 {
216 continue;
217 }
218
Ed Tanousd4dd96a2018-11-12 11:37:44 -0800219 thisPathMap[transaction->process_name].emplace(iface_name);
220
John Wangd0cf9422019-09-17 16:01:34 +0800221 if (std::strcmp(iface_name, assocDefsInterface) == 0)
Ed Tanous60520632018-06-11 17:46:52 -0700222 {
Matt Spinlere0b0e3a2019-04-08 10:39:23 -0500223 do_associations(system_bus, interface_map, objectServer,
John Wangd0cf9422019-09-17 16:01:34 +0800224 transaction->process_name, path);
Ed Tanous60520632018-06-11 17:46:52 -0700225 }
Ed Tanous60520632018-06-11 17:46:52 -0700226
227 pElement = pElement->NextSiblingElement("interface");
228 }
229
Matt Spinler11401e22019-04-08 13:13:25 -0500230 // Check if this new path has a pending association that can
231 // now be completed.
232 checkIfPendingAssociation(path, interface_map,
233 transaction->assocMaps, objectServer);
234
Ed Tanous50232cd2018-11-12 11:34:43 -0800235 pElement = pRoot->FirstChildElement("node");
236 while (pElement != nullptr)
Ed Tanous60520632018-06-11 17:46:52 -0700237 {
Ed Tanous50232cd2018-11-12 11:34:43 -0800238 const char* child_path = pElement->Attribute("name");
239 if (child_path != nullptr)
Ed Tanous60520632018-06-11 17:46:52 -0700240 {
Ed Tanous50232cd2018-11-12 11:34:43 -0800241 std::string parent_path(path);
242 if (parent_path == "/")
Ed Tanous60520632018-06-11 17:46:52 -0700243 {
Ed Tanous50232cd2018-11-12 11:34:43 -0800244 parent_path.clear();
Ed Tanous60520632018-06-11 17:46:52 -0700245 }
Ed Tanous50232cd2018-11-12 11:34:43 -0800246
247 do_introspect(system_bus, transaction, interface_map,
248 objectServer, parent_path + "/" + child_path);
Ed Tanous60520632018-06-11 17:46:52 -0700249 }
Ed Tanous50232cd2018-11-12 11:34:43 -0800250 pElement = pElement->NextSiblingElement("node");
Ed Tanous60520632018-06-11 17:46:52 -0700251 }
252 },
253 transaction->process_name, path, "org.freedesktop.DBus.Introspectable",
254 "Introspect");
255}
256
Ed Tanous60520632018-06-11 17:46:52 -0700257void start_new_introspect(
Ed Tanous21c60592020-08-17 23:43:46 -0700258 sdbusplus::asio::connection* system_bus, boost::asio::io_context& io,
Ed Tanous60520632018-06-11 17:46:52 -0700259 interface_map_type& interface_map, const std::string& process_name,
Matt Spinler11401e22019-04-08 13:13:25 -0500260 AssociationMaps& assocMaps,
Matt Spinleraecabe82018-09-19 13:25:42 -0500261#ifdef DEBUG
Ed Tanous60520632018-06-11 17:46:52 -0700262 std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>>
263 global_start_time,
Matt Spinleraecabe82018-09-19 13:25:42 -0500264#endif
Ed Tanous60520632018-06-11 17:46:52 -0700265 sdbusplus::asio::object_server& objectServer)
266{
Andrew Geissler82815da2019-02-04 12:19:41 -0600267 if (needToIntrospect(process_name, service_whitelist, service_blacklist))
Ed Tanous60520632018-06-11 17:46:52 -0700268 {
Ed Tanous60520632018-06-11 17:46:52 -0700269 std::shared_ptr<InProgressIntrospect> transaction =
Matt Spinler11401e22019-04-08 13:13:25 -0500270 std::make_shared<InProgressIntrospect>(system_bus, io, process_name,
271 assocMaps
Matt Spinleraecabe82018-09-19 13:25:42 -0500272#ifdef DEBUG
273 ,
274 global_start_time
275#endif
276 );
Ed Tanous60520632018-06-11 17:46:52 -0700277
278 do_introspect(system_bus, transaction, interface_map, objectServer,
279 "/");
280 }
281}
282
283// TODO(ed) replace with std::set_intersection once c++17 is available
284template <class InputIt1, class InputIt2>
285bool intersect(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2)
286{
287 while (first1 != last1 && first2 != last2)
288 {
289 if (*first1 < *first2)
290 {
291 ++first1;
292 continue;
293 }
294 if (*first2 < *first1)
295 {
296 ++first2;
297 continue;
298 }
299 return true;
300 }
301 return false;
302}
303
304void doListNames(
Ed Tanous21c60592020-08-17 23:43:46 -0700305 boost::asio::io_context& io, interface_map_type& interface_map,
Ed Tanous60520632018-06-11 17:46:52 -0700306 sdbusplus::asio::connection* system_bus,
307 boost::container::flat_map<std::string, std::string>& name_owners,
Matt Spinler11401e22019-04-08 13:13:25 -0500308 AssociationMaps& assocMaps, sdbusplus::asio::object_server& objectServer)
Ed Tanous60520632018-06-11 17:46:52 -0700309{
310 system_bus->async_method_call(
Matt Spinler11401e22019-04-08 13:13:25 -0500311 [&io, &interface_map, &name_owners, &objectServer, system_bus,
312 &assocMaps](const boost::system::error_code ec,
Ed Tanous60520632018-06-11 17:46:52 -0700313 std::vector<std::string> process_names) {
314 if (ec)
315 {
316 std::cerr << "Error getting names: " << ec << "\n";
317 std::exit(EXIT_FAILURE);
318 return;
319 }
Ed Tanous60520632018-06-11 17:46:52 -0700320 // Try to make startup consistent
321 std::sort(process_names.begin(), process_names.end());
Matt Spinleraecabe82018-09-19 13:25:42 -0500322#ifdef DEBUG
Ed Tanous60520632018-06-11 17:46:52 -0700323 std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>>
324 global_start_time = std::make_shared<
325 std::chrono::time_point<std::chrono::steady_clock>>(
326 std::chrono::steady_clock::now());
Matt Spinleraecabe82018-09-19 13:25:42 -0500327#endif
Ed Tanous60520632018-06-11 17:46:52 -0700328 for (const std::string& process_name : process_names)
329 {
Andrew Geissler82815da2019-02-04 12:19:41 -0600330 if (needToIntrospect(process_name, service_whitelist,
331 service_blacklist))
Ed Tanous60520632018-06-11 17:46:52 -0700332 {
333 start_new_introspect(system_bus, io, interface_map,
Matt Spinler11401e22019-04-08 13:13:25 -0500334 process_name, assocMaps,
Matt Spinleraecabe82018-09-19 13:25:42 -0500335#ifdef DEBUG
336 global_start_time,
337#endif
Ed Tanous60520632018-06-11 17:46:52 -0700338 objectServer);
339 update_owners(system_bus, name_owners, process_name);
340 }
341 }
342 },
343 "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus",
344 "ListNames");
345}
346
Matt Spinlerdd945862018-09-07 12:41:05 -0500347void splitArgs(const std::string& stringArgs,
348 boost::container::flat_set<std::string>& listArgs)
349{
350 std::istringstream args;
351 std::string arg;
352
353 args.str(stringArgs);
354
355 while (!args.eof())
356 {
357 args >> arg;
358 if (!arg.empty())
359 {
360 listArgs.insert(arg);
361 }
362 }
363}
364
Matt Spinler47c09752018-11-29 14:54:13 -0600365void addObjectMapResult(
366 std::vector<interface_map_type::value_type>& objectMap,
Matt Spinler9f0958e2018-09-11 08:26:10 -0500367 const std::string& objectPath,
368 const std::pair<std::string, boost::container::flat_set<std::string>>&
369 interfaceMap)
370{
371 // Adds an object path/service name/interface list entry to
Matt Spinler47c09752018-11-29 14:54:13 -0600372 // the results of GetSubTree and GetAncestors.
Matt Spinler9f0958e2018-09-11 08:26:10 -0500373 // If an entry for the object path already exists, just add the
374 // service name and interfaces to that entry, otherwise create
375 // a new entry.
376 auto entry = std::find_if(
Matt Spinler47c09752018-11-29 14:54:13 -0600377 objectMap.begin(), objectMap.end(),
Matt Spinler9f0958e2018-09-11 08:26:10 -0500378 [&objectPath](const auto& i) { return objectPath == i.first; });
379
Matt Spinler47c09752018-11-29 14:54:13 -0600380 if (entry != objectMap.end())
Matt Spinler9f0958e2018-09-11 08:26:10 -0500381 {
382 entry->second.emplace(interfaceMap);
383 }
384 else
385 {
386 interface_map_type::value_type object;
387 object.first = objectPath;
388 object.second.emplace(interfaceMap);
Matt Spinler47c09752018-11-29 14:54:13 -0600389 objectMap.push_back(object);
Matt Spinler9f0958e2018-09-11 08:26:10 -0500390 }
391}
392
Matt Spinlera82779f2019-01-09 12:39:42 -0600393// Remove parents of the passed in path that:
394// 1) Only have the 3 default interfaces on them
395// - Means D-Bus created these, not application code,
396// with the Properties, Introspectable, and Peer ifaces
397// 2) Have no other child for this owner
398void removeUnneededParents(const std::string& objectPath,
399 const std::string& owner,
400 interface_map_type& interface_map)
401{
402 auto parent = objectPath;
403
404 while (true)
405 {
406 auto pos = parent.find_last_of('/');
407 if ((pos == std::string::npos) || (pos == 0))
408 {
409 break;
410 }
411 parent = parent.substr(0, pos);
412
413 auto parent_it = interface_map.find(parent);
414 if (parent_it == interface_map.end())
415 {
416 break;
417 }
418
419 auto ifaces_it = parent_it->second.find(owner);
420 if (ifaces_it == parent_it->second.end())
421 {
422 break;
423 }
424
425 if (ifaces_it->second.size() != 3)
426 {
427 break;
428 }
429
430 auto child_path = parent + '/';
431
432 // Remove this parent if there isn't a remaining child on this owner
433 auto child = std::find_if(
434 interface_map.begin(), interface_map.end(),
435 [&owner, &child_path](const auto& entry) {
436 return boost::starts_with(entry.first, child_path) &&
437 (entry.second.find(owner) != entry.second.end());
438 });
439
440 if (child == interface_map.end())
441 {
442 parent_it->second.erase(ifaces_it);
443 if (parent_it->second.empty())
444 {
445 interface_map.erase(parent_it);
446 }
447 }
448 else
449 {
450 break;
451 }
452 }
453}
454
Ed Tanous0a13c762021-09-28 13:29:25 -0700455std::vector<interface_map_type::value_type>
456 getAncestors(const interface_map_type& interface_map, std::string req_path,
457 std::vector<std::string>& interfaces)
458{
459 // Interfaces need to be sorted for intersect to function
460 std::sort(interfaces.begin(), interfaces.end());
461
462 if (boost::ends_with(req_path, "/"))
463 {
464 req_path.pop_back();
465 }
466 if (req_path.size() && interface_map.find(req_path) == interface_map.end())
467 {
468 throw NotFoundException();
469 }
470
471 std::vector<interface_map_type::value_type> ret;
472 for (auto& object_path : interface_map)
473 {
474 auto& this_path = object_path.first;
475 if (boost::starts_with(req_path, this_path) && (req_path != this_path))
476 {
477 if (interfaces.empty())
478 {
479 ret.emplace_back(object_path);
480 }
481 else
482 {
483 for (auto& interface_map : object_path.second)
484 {
485
486 if (intersect(interfaces.begin(), interfaces.end(),
487 interface_map.second.begin(),
488 interface_map.second.end()))
489 {
490 addObjectMapResult(ret, this_path, interface_map);
491 }
492 }
493 }
494 }
495 }
496
497 return ret;
498}
499
500boost::container::flat_map<std::string, boost::container::flat_set<std::string>>
501 getObject(const interface_map_type& interface_map, const std::string& path,
502 std::vector<std::string>& interfaces)
503{
504 boost::container::flat_map<std::string,
505 boost::container::flat_set<std::string>>
506 results;
507
508 // Interfaces need to be sorted for intersect to function
509 std::sort(interfaces.begin(), interfaces.end());
510 auto path_ref = interface_map.find(path);
511 if (path_ref == interface_map.end())
512 {
513 throw NotFoundException();
514 }
515 if (interfaces.empty())
516 {
517 return path_ref->second;
518 }
519 for (auto& interface_map : path_ref->second)
520 {
521 if (intersect(interfaces.begin(), interfaces.end(),
522 interface_map.second.begin(), interface_map.second.end()))
523 {
524 results.emplace(interface_map.first, interface_map.second);
525 }
526 }
527
528 if (results.empty())
529 {
530 throw NotFoundException();
531 }
532
533 return results;
534}
535
536std::vector<interface_map_type::value_type>
537 getSubTree(const interface_map_type& interface_map, std::string req_path,
538 int32_t depth, std::vector<std::string>& interfaces)
539{
540 if (depth <= 0)
541 {
542 depth = std::numeric_limits<int32_t>::max();
543 }
544 // Interfaces need to be sorted for intersect to function
545 std::sort(interfaces.begin(), interfaces.end());
546 std::vector<interface_map_type::value_type> ret;
547
548 if (boost::ends_with(req_path, "/"))
549 {
550 req_path.pop_back();
551 }
552 if (req_path.size() && interface_map.find(req_path) == interface_map.end())
553 {
554 throw NotFoundException();
555 }
556
557 for (auto& object_path : interface_map)
558 {
559 auto& this_path = object_path.first;
560
561 if (this_path == req_path)
562 {
563 continue;
564 }
565
566 if (boost::starts_with(this_path, req_path))
567 {
568 // count the number of slashes past the search term
569 int32_t this_depth = std::count(this_path.begin() + req_path.size(),
570 this_path.end(), '/');
571 if (this_depth <= depth)
572 {
573 for (auto& interface_map : object_path.second)
574 {
575 if (intersect(interfaces.begin(), interfaces.end(),
576 interface_map.second.begin(),
577 interface_map.second.end()) ||
578 interfaces.empty())
579 {
580 addObjectMapResult(ret, this_path, interface_map);
581 }
582 }
583 }
584 }
585 }
586
587 return ret;
588}
589
590std::vector<std::string>
591 getSubTreePaths(const interface_map_type& interface_map,
592 std::string req_path, int32_t depth,
593 std::vector<std::string>& interfaces)
594{
595 if (depth <= 0)
596 {
597 depth = std::numeric_limits<int32_t>::max();
598 }
599 // Interfaces need to be sorted for intersect to function
600 std::sort(interfaces.begin(), interfaces.end());
601 std::vector<std::string> ret;
602
603 if (boost::ends_with(req_path, "/"))
604 {
605 req_path.pop_back();
606 }
607 if (req_path.size() && interface_map.find(req_path) == interface_map.end())
608 {
609 throw NotFoundException();
610 }
611
612 for (auto& object_path : interface_map)
613 {
614 auto& this_path = object_path.first;
615
616 if (this_path == req_path)
617 {
618 continue;
619 }
620
621 if (boost::starts_with(this_path, req_path))
622 {
623 // count the number of slashes past the search term
624 int this_depth = std::count(this_path.begin() + req_path.size(),
625 this_path.end(), '/');
626 if (this_depth <= depth)
627 {
628 bool add = interfaces.empty();
629 for (auto& interface_map : object_path.second)
630 {
631 if (intersect(interfaces.begin(), interfaces.end(),
632 interface_map.second.begin(),
633 interface_map.second.end()))
634 {
635 add = true;
636 break;
637 }
638 }
639 if (add)
640 {
641 // TODO(ed) this is a copy
642 ret.emplace_back(this_path);
643 }
644 }
645 }
646 }
647
648 return ret;
649}
650
Ed Tanous60520632018-06-11 17:46:52 -0700651int main(int argc, char** argv)
652{
Matt Spinlerdd945862018-09-07 12:41:05 -0500653 auto options = ArgumentParser(argc, argv);
Ed Tanous21c60592020-08-17 23:43:46 -0700654 boost::asio::io_context io;
Ed Tanous60520632018-06-11 17:46:52 -0700655 std::shared_ptr<sdbusplus::asio::connection> system_bus =
656 std::make_shared<sdbusplus::asio::connection>(io);
657
Matt Spinlerdd945862018-09-07 12:41:05 -0500658 splitArgs(options["service-namespaces"], service_whitelist);
Matt Spinlerdd945862018-09-07 12:41:05 -0500659 splitArgs(options["service-blacklists"], service_blacklist);
660
Ed Tanousd4dd96a2018-11-12 11:37:44 -0800661 // TODO(Ed) Remove this once all service files are updated to not use this.
662 // For now, simply squash the input, and ignore it.
663 boost::container::flat_set<std::string> iface_whitelist;
664 splitArgs(options["interface-namespaces"], iface_whitelist);
665
Ed Tanous60520632018-06-11 17:46:52 -0700666 sdbusplus::asio::object_server server(system_bus);
667
668 // Construct a signal set registered for process termination.
669 boost::asio::signal_set signals(io, SIGINT, SIGTERM);
Brad Bishop2d41d6a2021-08-03 08:14:45 -0400670 signals.async_wait(
671 [&io](const boost::system::error_code&, int) { io.stop(); });
Ed Tanous60520632018-06-11 17:46:52 -0700672
673 interface_map_type interface_map;
674 boost::container::flat_map<std::string, std::string> name_owners;
675
676 std::function<void(sdbusplus::message::message & message)>
677 nameChangeHandler = [&interface_map, &io, &name_owners, &server,
678 system_bus](sdbusplus::message::message& message) {
Andrew Geissler20679262019-02-11 20:20:40 -0600679 std::string name; // well-known
680 std::string old_owner; // unique-name
681 std::string new_owner; // unique-name
Ed Tanous60520632018-06-11 17:46:52 -0700682
683 message.read(name, old_owner, new_owner);
684
685 if (!old_owner.empty())
686 {
Andrew Geissler20679262019-02-11 20:20:40 -0600687 processNameChangeDelete(name_owners, name, old_owner,
Matt Spinlere2359fb2019-04-05 14:11:33 -0500688 interface_map, associationMaps, server);
Ed Tanous60520632018-06-11 17:46:52 -0700689 }
690
691 if (!new_owner.empty())
692 {
Matt Spinleraecabe82018-09-19 13:25:42 -0500693#ifdef DEBUG
Ed Tanous60520632018-06-11 17:46:52 -0700694 auto transaction = std::make_shared<
695 std::chrono::time_point<std::chrono::steady_clock>>(
696 std::chrono::steady_clock::now());
Matt Spinleraecabe82018-09-19 13:25:42 -0500697#endif
Ed Tanous60520632018-06-11 17:46:52 -0700698 // New daemon added
Andrew Geissler82815da2019-02-04 12:19:41 -0600699 if (needToIntrospect(name, service_whitelist,
700 service_blacklist))
Ed Tanous60520632018-06-11 17:46:52 -0700701 {
702 name_owners[new_owner] = name;
703 start_new_introspect(system_bus.get(), io, interface_map,
Matt Spinler11401e22019-04-08 13:13:25 -0500704 name, associationMaps,
Matt Spinleraecabe82018-09-19 13:25:42 -0500705#ifdef DEBUG
706 transaction,
707#endif
708 server);
Ed Tanous60520632018-06-11 17:46:52 -0700709 }
710 }
711 };
712
713 sdbusplus::bus::match::match nameOwnerChanged(
714 static_cast<sdbusplus::bus::bus&>(*system_bus),
715 sdbusplus::bus::match::rules::nameOwnerChanged(), nameChangeHandler);
716
717 std::function<void(sdbusplus::message::message & message)>
718 interfacesAddedHandler = [&interface_map, &name_owners, &server](
719 sdbusplus::message::message& message) {
720 sdbusplus::message::object_path obj_path;
Andrew Geissler70461892019-02-27 09:57:37 -0600721 InterfacesAdded interfaces_added;
Ed Tanous60520632018-06-11 17:46:52 -0700722 message.read(obj_path, interfaces_added);
723 std::string well_known;
Andrew Geissler3b025e62019-02-01 10:33:54 -0600724 if (!getWellKnown(name_owners, message.get_sender(), well_known))
Ed Tanous60520632018-06-11 17:46:52 -0700725 {
726 return; // only introspect well-known
727 }
Andrew Geissler82815da2019-02-04 12:19:41 -0600728 if (needToIntrospect(well_known, service_whitelist,
729 service_blacklist))
Ed Tanous60520632018-06-11 17:46:52 -0700730 {
Andrew Geissler70461892019-02-27 09:57:37 -0600731 processInterfaceAdded(interface_map, obj_path, interfaces_added,
Matt Spinlere2359fb2019-04-05 14:11:33 -0500732 well_known, associationMaps, server);
Ed Tanous60520632018-06-11 17:46:52 -0700733 }
734 };
735
736 sdbusplus::bus::match::match interfacesAdded(
737 static_cast<sdbusplus::bus::bus&>(*system_bus),
738 sdbusplus::bus::match::rules::interfacesAdded(),
739 interfacesAddedHandler);
740
741 std::function<void(sdbusplus::message::message & message)>
742 interfacesRemovedHandler = [&interface_map, &name_owners, &server](
743 sdbusplus::message::message& message) {
744 sdbusplus::message::object_path obj_path;
745 std::vector<std::string> interfaces_removed;
746 message.read(obj_path, interfaces_removed);
747 auto connection_map = interface_map.find(obj_path.str);
748 if (connection_map == interface_map.end())
749 {
750 return;
751 }
752
753 std::string sender;
Andrew Geissler3b025e62019-02-01 10:33:54 -0600754 if (!getWellKnown(name_owners, message.get_sender(), sender))
Ed Tanous60520632018-06-11 17:46:52 -0700755 {
756 return;
757 }
758 for (const std::string& interface : interfaces_removed)
759 {
760 auto interface_set = connection_map->second.find(sender);
761 if (interface_set == connection_map->second.end())
762 {
763 continue;
764 }
765
John Wangd0cf9422019-09-17 16:01:34 +0800766 if (interface == assocDefsInterface)
Ed Tanous60520632018-06-11 17:46:52 -0700767 {
Andrew Geissler67e5a422019-02-04 13:00:59 -0600768 removeAssociation(obj_path.str, sender, server,
Matt Spinlere2359fb2019-04-05 14:11:33 -0500769 associationMaps);
Ed Tanous60520632018-06-11 17:46:52 -0700770 }
771
772 interface_set->second.erase(interface);
Matt Spinler9c3d2852019-04-08 15:57:19 -0500773
Ed Tanous60520632018-06-11 17:46:52 -0700774 if (interface_set->second.empty())
775 {
Matt Spinler9c3d2852019-04-08 15:57:19 -0500776 // If this was the last interface on this connection,
777 // erase the connection
Ed Tanous60520632018-06-11 17:46:52 -0700778 connection_map->second.erase(interface_set);
Matt Spinler9c3d2852019-04-08 15:57:19 -0500779
780 // Instead of checking if every single path is the endpoint
781 // of an association that needs to be moved to pending,
782 // only check when the only remaining owner of this path is
783 // ourself, which would be because we still own the
784 // association path.
785 if ((connection_map->second.size() == 1) &&
786 (connection_map->second.begin()->first ==
Brad Bishopa02cd542021-10-12 19:12:42 -0400787 "xyz.openbmc_project.ObjectMapper"))
Matt Spinler9c3d2852019-04-08 15:57:19 -0500788 {
789 // Remove the 2 association D-Bus paths and move the
790 // association to pending.
791 moveAssociationToPending(obj_path.str, associationMaps,
792 server);
793 }
Ed Tanous60520632018-06-11 17:46:52 -0700794 }
795 }
796 // If this was the last connection on this object path,
797 // erase the object path
798 if (connection_map->second.empty())
799 {
800 interface_map.erase(connection_map);
801 }
Matt Spinlera82779f2019-01-09 12:39:42 -0600802
803 removeUnneededParents(obj_path.str, sender, interface_map);
Ed Tanous60520632018-06-11 17:46:52 -0700804 };
805
806 sdbusplus::bus::match::match interfacesRemoved(
807 static_cast<sdbusplus::bus::bus&>(*system_bus),
808 sdbusplus::bus::match::rules::interfacesRemoved(),
809 interfacesRemovedHandler);
810
811 std::function<void(sdbusplus::message::message & message)>
Matt Spinlere0b0e3a2019-04-08 10:39:23 -0500812 associationChangedHandler = [&server, &name_owners, &interface_map](
Matt Spinler8f876a52019-04-15 13:22:50 -0500813 sdbusplus::message::message& message) {
814 std::string objectName;
Patrick Williams2bb2d6b2020-05-13 17:59:02 -0500815 boost::container::flat_map<std::string,
816 std::variant<std::vector<Association>>>
Matt Spinler8f876a52019-04-15 13:22:50 -0500817 values;
818 message.read(objectName, values);
John Wangd0cf9422019-09-17 16:01:34 +0800819 auto prop = values.find(assocDefsProperty);
Matt Spinler8f876a52019-04-15 13:22:50 -0500820 if (prop != values.end())
821 {
822 std::vector<Association> associations =
Patrick Williamsb05bc122020-05-13 12:21:00 -0500823 std::get<std::vector<Association>>(prop->second);
Matt Spinler8f876a52019-04-15 13:22:50 -0500824
825 std::string well_known;
826 if (!getWellKnown(name_owners, message.get_sender(),
827 well_known))
828 {
829 return;
Ed Tanous60520632018-06-11 17:46:52 -0700830 }
Matt Spinler8f876a52019-04-15 13:22:50 -0500831 associationChanged(server, associations, message.get_path(),
Matt Spinlere0b0e3a2019-04-08 10:39:23 -0500832 well_known, interface_map, associationMaps);
Matt Spinler8f876a52019-04-15 13:22:50 -0500833 }
834 };
835 sdbusplus::bus::match::match assocChangedMatch(
Ed Tanous60520632018-06-11 17:46:52 -0700836 static_cast<sdbusplus::bus::bus&>(*system_bus),
837 sdbusplus::bus::match::rules::interface(
838 "org.freedesktop.DBus.Properties") +
839 sdbusplus::bus::match::rules::member("PropertiesChanged") +
Matt Spinler8f876a52019-04-15 13:22:50 -0500840 sdbusplus::bus::match::rules::argN(0, assocDefsInterface),
841 associationChangedHandler);
842
Ed Tanous60520632018-06-11 17:46:52 -0700843 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
Brad Bishopa02cd542021-10-12 19:12:42 -0400844 server.add_interface("/xyz/openbmc_project/object_mapper",
845 "xyz.openbmc_project.ObjectMapper");
Ed Tanous60520632018-06-11 17:46:52 -0700846
847 iface->register_method(
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600848 "GetAncestors", [&interface_map](std::string& req_path,
Ed Tanous60520632018-06-11 17:46:52 -0700849 std::vector<std::string>& interfaces) {
Ed Tanous0a13c762021-09-28 13:29:25 -0700850 return getAncestors(interface_map, req_path, interfaces);
Ed Tanous60520632018-06-11 17:46:52 -0700851 });
852
853 iface->register_method(
854 "GetObject", [&interface_map](const std::string& path,
855 std::vector<std::string>& interfaces) {
Ed Tanous0a13c762021-09-28 13:29:25 -0700856 return getObject(interface_map, path, interfaces);
Ed Tanous60520632018-06-11 17:46:52 -0700857 });
858
859 iface->register_method(
Matt Spinler153494e2018-11-07 16:35:40 -0600860 "GetSubTree", [&interface_map](std::string& req_path, int32_t depth,
861 std::vector<std::string>& interfaces) {
Ed Tanous0a13c762021-09-28 13:29:25 -0700862 return getSubTree(interface_map, req_path, depth, interfaces);
Ed Tanous60520632018-06-11 17:46:52 -0700863 });
864
865 iface->register_method(
866 "GetSubTreePaths",
Matt Spinler153494e2018-11-07 16:35:40 -0600867 [&interface_map](std::string& req_path, int32_t depth,
Ed Tanous60520632018-06-11 17:46:52 -0700868 std::vector<std::string>& interfaces) {
Ed Tanous0a13c762021-09-28 13:29:25 -0700869 return getSubTreePaths(interface_map, req_path, depth, interfaces);
Ed Tanous60520632018-06-11 17:46:52 -0700870 });
871
872 iface->initialize();
873
874 io.post([&]() {
Matt Spinler11401e22019-04-08 13:13:25 -0500875 doListNames(io, interface_map, system_bus.get(), name_owners,
876 associationMaps, server);
Ed Tanous60520632018-06-11 17:46:52 -0700877 });
878
Brad Bishopa02cd542021-10-12 19:12:42 -0400879 system_bus->request_name("xyz.openbmc_project.ObjectMapper");
Vishwanatha Subbanna64354ef2020-08-21 03:35:26 -0500880
Ed Tanous60520632018-06-11 17:46:52 -0700881 io.run();
882}