blob: be24368a5d3a378b6ce90956655c60e54e6e65b5 [file] [log] [blame]
Matt Spinler5eddf442019-04-10 10:08:56 -05001#include "config.h"
2
Andrew Geisslera80a3af2019-02-04 14:01:49 -06003#include "associations.hpp"
Andrew Geissler3b025e62019-02-01 10:33:54 -06004#include "processing.hpp"
Matt Spinlerdd945862018-09-07 12:41:05 -05005#include "src/argument.hpp"
Matt Spinler35396c12019-04-05 11:46:57 -05006#include "types.hpp"
Matt Spinlerdd945862018-09-07 12:41:05 -05007
Ed Tanous60520632018-06-11 17:46:52 -07008#include <tinyxml2.h>
9
10#include <atomic>
11#include <boost/algorithm/string/predicate.hpp>
Ed Tanous21c60592020-08-17 23:43:46 -070012#include <boost/asio/io_context.hpp>
13#include <boost/asio/signal_set.hpp>
Ed Tanous60520632018-06-11 17:46:52 -070014#include <boost/container/flat_map.hpp>
Ed Tanous60520632018-06-11 17:46:52 -070015#include <chrono>
16#include <iomanip>
17#include <iostream>
18#include <sdbusplus/asio/connection.hpp>
19#include <sdbusplus/asio/object_server.hpp>
20
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 {
Patrick Williams3735ea22020-11-11 13:12:48 -060031 return "xyz.openbmc_project.Common.Error.ResourceNotFound";
Ed Tanous60520632018-06-11 17:46:52 -070032 };
33 const char* description() const noexcept override
34 {
35 return "path or object not found";
36 };
37 const char* what() const noexcept override
38 {
Patrick Williams3735ea22020-11-11 13:12:48 -060039 return "xyz.openbmc_project.Common.Error.ResourceNotFound: "
40 "The resource is not found.";
Ed Tanous60520632018-06-11 17:46:52 -070041 };
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(
Matt Spinler5eddf442019-04-10 10:08:56 -050074 MAPPER_PATH, "xyz.openbmc_project.ObjectMapper.Private",
75 "IntrospectionComplete");
Ed Tanous60520632018-06-11 17:46:52 -070076 m.append(process_name);
77 m.signal_send();
78}
79
80struct InProgressIntrospect
81{
82 InProgressIntrospect(
Ed Tanous21c60592020-08-17 23:43:46 -070083 sdbusplus::asio::connection* system_bus, boost::asio::io_context& io,
Matt Spinler11401e22019-04-08 13:13:25 -050084 const std::string& process_name, AssociationMaps& am
Matt Spinleraecabe82018-09-19 13:25:42 -050085#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 Spinler11401e22019-04-08 13:13:25 -050092 io(io), process_name(process_name), assocMaps(am)
Matt Spinleraecabe82018-09-19 13:25:42 -050093#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;
Ed Tanous21c60592020-08-17 23:43:46 -0700121 boost::asio::io_context& io;
Ed Tanous60520632018-06-11 17:46:52 -0700122 std::string process_name;
Matt Spinler11401e22019-04-08 13:13:25 -0500123 AssociationMaps& assocMaps;
Matt Spinleraecabe82018-09-19 13:25:42 -0500124#ifdef DEBUG
Ed Tanous60520632018-06-11 17:46:52 -0700125 std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>>
126 global_start_time;
127 std::chrono::time_point<std::chrono::steady_clock> process_start_time;
Matt Spinleraecabe82018-09-19 13:25:42 -0500128#endif
Ed Tanous60520632018-06-11 17:46:52 -0700129};
130
Ed Tanous60520632018-06-11 17:46:52 -0700131void do_associations(sdbusplus::asio::connection* system_bus,
Matt Spinlere0b0e3a2019-04-08 10:39:23 -0500132 interface_map_type& interfaceMap,
Ed Tanous60520632018-06-11 17:46:52 -0700133 sdbusplus::asio::object_server& objectServer,
Adrian Ambrożewiczbb40bd32021-02-12 13:36:26 +0100134 const std::string& processName, const std::string& path,
135 int timeoutRetries = 0)
Ed Tanous60520632018-06-11 17:46:52 -0700136{
Adrian Ambrożewiczbb40bd32021-02-12 13:36:26 +0100137 constexpr int maxTimeoutRetries = 3;
Ed Tanous60520632018-06-11 17:46:52 -0700138 system_bus->async_method_call(
Adrian Ambrożewiczbb40bd32021-02-12 13:36:26 +0100139 [&objectServer, path, processName, &interfaceMap, system_bus,
140 timeoutRetries](
Matt Spinler937a2322019-01-23 13:54:22 -0600141 const boost::system::error_code ec,
Patrick Williams2bb2d6b2020-05-13 17:59:02 -0500142 const std::variant<std::vector<Association>>& variantAssociations) {
Ed Tanous60520632018-06-11 17:46:52 -0700143 if (ec)
144 {
Adrian Ambrożewiczbb40bd32021-02-12 13:36:26 +0100145 if (ec.value() == boost::system::errc::timed_out &&
146 timeoutRetries < maxTimeoutRetries)
147 {
148 do_associations(system_bus, interfaceMap, objectServer,
149 processName, path, timeoutRetries + 1);
150 return;
151 }
Ed Tanous60520632018-06-11 17:46:52 -0700152 std::cerr << "Error getting associations from " << path << "\n";
153 }
154 std::vector<Association> associations =
Patrick Williamsb05bc122020-05-13 12:21:00 -0500155 std::get<std::vector<Association>>(variantAssociations);
Andrew Geissler4511b332019-02-21 15:40:40 -0600156 associationChanged(objectServer, associations, path, processName,
Matt Spinlere0b0e3a2019-04-08 10:39:23 -0500157 interfaceMap, associationMaps);
Ed Tanous60520632018-06-11 17:46:52 -0700158 },
159 processName, path, "org.freedesktop.DBus.Properties", "Get",
John Wangd0cf9422019-09-17 16:01:34 +0800160 assocDefsInterface, assocDefsProperty);
Ed Tanous60520632018-06-11 17:46:52 -0700161}
162
163void do_introspect(sdbusplus::asio::connection* system_bus,
164 std::shared_ptr<InProgressIntrospect> transaction,
165 interface_map_type& interface_map,
166 sdbusplus::asio::object_server& objectServer,
Vernon Maueryc52be0d2020-01-14 11:14:25 -0800167 std::string path, int timeoutRetries = 0)
Ed Tanous60520632018-06-11 17:46:52 -0700168{
Vernon Maueryc52be0d2020-01-14 11:14:25 -0800169 constexpr int maxTimeoutRetries = 3;
Ed Tanous60520632018-06-11 17:46:52 -0700170 system_bus->async_method_call(
Vernon Maueryc52be0d2020-01-14 11:14:25 -0800171 [&interface_map, &objectServer, transaction, path, system_bus,
172 timeoutRetries](const boost::system::error_code ec,
173 const std::string& introspect_xml) {
Ed Tanous60520632018-06-11 17:46:52 -0700174 if (ec)
175 {
Vernon Maueryc52be0d2020-01-14 11:14:25 -0800176 if (ec.value() == boost::system::errc::timed_out &&
177 timeoutRetries < maxTimeoutRetries)
178 {
179 do_introspect(system_bus, transaction, interface_map,
180 objectServer, path, timeoutRetries + 1);
181 return;
182 }
Ed Tanous60520632018-06-11 17:46:52 -0700183 std::cerr << "Introspect call failed with error: " << ec << ", "
184 << ec.message()
185 << " on process: " << transaction->process_name
186 << " path: " << path << "\n";
187 return;
188 }
189
190 tinyxml2::XMLDocument doc;
191
192 tinyxml2::XMLError e = doc.Parse(introspect_xml.c_str());
193 if (e != tinyxml2::XMLError::XML_SUCCESS)
194 {
195 std::cerr << "XML parsing failed\n";
196 return;
197 }
198
199 tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node");
200 if (pRoot == nullptr)
201 {
202 std::cerr << "XML document did not contain any data\n";
203 return;
204 }
205 auto& thisPathMap = interface_map[path];
Ed Tanous60520632018-06-11 17:46:52 -0700206 tinyxml2::XMLElement* pElement =
207 pRoot->FirstChildElement("interface");
208 while (pElement != nullptr)
209 {
210 const char* iface_name = pElement->Attribute("name");
211 if (iface_name == nullptr)
212 {
213 continue;
214 }
215
Ed Tanousd4dd96a2018-11-12 11:37:44 -0800216 thisPathMap[transaction->process_name].emplace(iface_name);
217
John Wangd0cf9422019-09-17 16:01:34 +0800218 if (std::strcmp(iface_name, assocDefsInterface) == 0)
Ed Tanous60520632018-06-11 17:46:52 -0700219 {
Matt Spinlere0b0e3a2019-04-08 10:39:23 -0500220 do_associations(system_bus, interface_map, objectServer,
John Wangd0cf9422019-09-17 16:01:34 +0800221 transaction->process_name, path);
Ed Tanous60520632018-06-11 17:46:52 -0700222 }
Ed Tanous60520632018-06-11 17:46:52 -0700223
224 pElement = pElement->NextSiblingElement("interface");
225 }
226
Matt Spinler11401e22019-04-08 13:13:25 -0500227 // Check if this new path has a pending association that can
228 // now be completed.
229 checkIfPendingAssociation(path, interface_map,
230 transaction->assocMaps, objectServer);
231
Ed Tanous50232cd2018-11-12 11:34:43 -0800232 pElement = pRoot->FirstChildElement("node");
233 while (pElement != nullptr)
Ed Tanous60520632018-06-11 17:46:52 -0700234 {
Ed Tanous50232cd2018-11-12 11:34:43 -0800235 const char* child_path = pElement->Attribute("name");
236 if (child_path != nullptr)
Ed Tanous60520632018-06-11 17:46:52 -0700237 {
Ed Tanous50232cd2018-11-12 11:34:43 -0800238 std::string parent_path(path);
239 if (parent_path == "/")
Ed Tanous60520632018-06-11 17:46:52 -0700240 {
Ed Tanous50232cd2018-11-12 11:34:43 -0800241 parent_path.clear();
Ed Tanous60520632018-06-11 17:46:52 -0700242 }
Ed Tanous50232cd2018-11-12 11:34:43 -0800243
244 do_introspect(system_bus, transaction, interface_map,
245 objectServer, parent_path + "/" + child_path);
Ed Tanous60520632018-06-11 17:46:52 -0700246 }
Ed Tanous50232cd2018-11-12 11:34:43 -0800247 pElement = pElement->NextSiblingElement("node");
Ed Tanous60520632018-06-11 17:46:52 -0700248 }
249 },
250 transaction->process_name, path, "org.freedesktop.DBus.Introspectable",
251 "Introspect");
252}
253
Ed Tanous60520632018-06-11 17:46:52 -0700254void start_new_introspect(
Ed Tanous21c60592020-08-17 23:43:46 -0700255 sdbusplus::asio::connection* system_bus, boost::asio::io_context& io,
Ed Tanous60520632018-06-11 17:46:52 -0700256 interface_map_type& interface_map, const std::string& process_name,
Matt Spinler11401e22019-04-08 13:13:25 -0500257 AssociationMaps& assocMaps,
Matt Spinleraecabe82018-09-19 13:25:42 -0500258#ifdef DEBUG
Ed Tanous60520632018-06-11 17:46:52 -0700259 std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>>
260 global_start_time,
Matt Spinleraecabe82018-09-19 13:25:42 -0500261#endif
Ed Tanous60520632018-06-11 17:46:52 -0700262 sdbusplus::asio::object_server& objectServer)
263{
Andrew Geissler82815da2019-02-04 12:19:41 -0600264 if (needToIntrospect(process_name, service_whitelist, service_blacklist))
Ed Tanous60520632018-06-11 17:46:52 -0700265 {
Ed Tanous60520632018-06-11 17:46:52 -0700266 std::shared_ptr<InProgressIntrospect> transaction =
Matt Spinler11401e22019-04-08 13:13:25 -0500267 std::make_shared<InProgressIntrospect>(system_bus, io, process_name,
268 assocMaps
Matt Spinleraecabe82018-09-19 13:25:42 -0500269#ifdef DEBUG
270 ,
271 global_start_time
272#endif
273 );
Ed Tanous60520632018-06-11 17:46:52 -0700274
275 do_introspect(system_bus, transaction, interface_map, objectServer,
276 "/");
277 }
278}
279
280// TODO(ed) replace with std::set_intersection once c++17 is available
281template <class InputIt1, class InputIt2>
282bool intersect(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2)
283{
284 while (first1 != last1 && first2 != last2)
285 {
286 if (*first1 < *first2)
287 {
288 ++first1;
289 continue;
290 }
291 if (*first2 < *first1)
292 {
293 ++first2;
294 continue;
295 }
296 return true;
297 }
298 return false;
299}
300
301void doListNames(
Ed Tanous21c60592020-08-17 23:43:46 -0700302 boost::asio::io_context& io, interface_map_type& interface_map,
Ed Tanous60520632018-06-11 17:46:52 -0700303 sdbusplus::asio::connection* system_bus,
304 boost::container::flat_map<std::string, std::string>& name_owners,
Matt Spinler11401e22019-04-08 13:13:25 -0500305 AssociationMaps& assocMaps, sdbusplus::asio::object_server& objectServer)
Ed Tanous60520632018-06-11 17:46:52 -0700306{
307 system_bus->async_method_call(
Matt Spinler11401e22019-04-08 13:13:25 -0500308 [&io, &interface_map, &name_owners, &objectServer, system_bus,
309 &assocMaps](const boost::system::error_code ec,
Ed Tanous60520632018-06-11 17:46:52 -0700310 std::vector<std::string> process_names) {
311 if (ec)
312 {
313 std::cerr << "Error getting names: " << ec << "\n";
314 std::exit(EXIT_FAILURE);
315 return;
316 }
Ed Tanous60520632018-06-11 17:46:52 -0700317 // Try to make startup consistent
318 std::sort(process_names.begin(), process_names.end());
Matt Spinleraecabe82018-09-19 13:25:42 -0500319#ifdef DEBUG
Ed Tanous60520632018-06-11 17:46:52 -0700320 std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>>
321 global_start_time = std::make_shared<
322 std::chrono::time_point<std::chrono::steady_clock>>(
323 std::chrono::steady_clock::now());
Matt Spinleraecabe82018-09-19 13:25:42 -0500324#endif
Ed Tanous60520632018-06-11 17:46:52 -0700325 for (const std::string& process_name : process_names)
326 {
Andrew Geissler82815da2019-02-04 12:19:41 -0600327 if (needToIntrospect(process_name, service_whitelist,
328 service_blacklist))
Ed Tanous60520632018-06-11 17:46:52 -0700329 {
330 start_new_introspect(system_bus, io, interface_map,
Matt Spinler11401e22019-04-08 13:13:25 -0500331 process_name, assocMaps,
Matt Spinleraecabe82018-09-19 13:25:42 -0500332#ifdef DEBUG
333 global_start_time,
334#endif
Ed Tanous60520632018-06-11 17:46:52 -0700335 objectServer);
336 update_owners(system_bus, name_owners, process_name);
337 }
338 }
339 },
340 "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus",
341 "ListNames");
342}
343
Matt Spinlerdd945862018-09-07 12:41:05 -0500344void splitArgs(const std::string& stringArgs,
345 boost::container::flat_set<std::string>& listArgs)
346{
347 std::istringstream args;
348 std::string arg;
349
350 args.str(stringArgs);
351
352 while (!args.eof())
353 {
354 args >> arg;
355 if (!arg.empty())
356 {
357 listArgs.insert(arg);
358 }
359 }
360}
361
Matt Spinler47c09752018-11-29 14:54:13 -0600362void addObjectMapResult(
363 std::vector<interface_map_type::value_type>& objectMap,
Matt Spinler9f0958e2018-09-11 08:26:10 -0500364 const std::string& objectPath,
365 const std::pair<std::string, boost::container::flat_set<std::string>>&
366 interfaceMap)
367{
368 // Adds an object path/service name/interface list entry to
Matt Spinler47c09752018-11-29 14:54:13 -0600369 // the results of GetSubTree and GetAncestors.
Matt Spinler9f0958e2018-09-11 08:26:10 -0500370 // If an entry for the object path already exists, just add the
371 // service name and interfaces to that entry, otherwise create
372 // a new entry.
373 auto entry = std::find_if(
Matt Spinler47c09752018-11-29 14:54:13 -0600374 objectMap.begin(), objectMap.end(),
Matt Spinler9f0958e2018-09-11 08:26:10 -0500375 [&objectPath](const auto& i) { return objectPath == i.first; });
376
Matt Spinler47c09752018-11-29 14:54:13 -0600377 if (entry != objectMap.end())
Matt Spinler9f0958e2018-09-11 08:26:10 -0500378 {
379 entry->second.emplace(interfaceMap);
380 }
381 else
382 {
383 interface_map_type::value_type object;
384 object.first = objectPath;
385 object.second.emplace(interfaceMap);
Matt Spinler47c09752018-11-29 14:54:13 -0600386 objectMap.push_back(object);
Matt Spinler9f0958e2018-09-11 08:26:10 -0500387 }
388}
389
Matt Spinlera82779f2019-01-09 12:39:42 -0600390// Remove parents of the passed in path that:
391// 1) Only have the 3 default interfaces on them
392// - Means D-Bus created these, not application code,
393// with the Properties, Introspectable, and Peer ifaces
394// 2) Have no other child for this owner
395void removeUnneededParents(const std::string& objectPath,
396 const std::string& owner,
397 interface_map_type& interface_map)
398{
399 auto parent = objectPath;
400
401 while (true)
402 {
403 auto pos = parent.find_last_of('/');
404 if ((pos == std::string::npos) || (pos == 0))
405 {
406 break;
407 }
408 parent = parent.substr(0, pos);
409
410 auto parent_it = interface_map.find(parent);
411 if (parent_it == interface_map.end())
412 {
413 break;
414 }
415
416 auto ifaces_it = parent_it->second.find(owner);
417 if (ifaces_it == parent_it->second.end())
418 {
419 break;
420 }
421
422 if (ifaces_it->second.size() != 3)
423 {
424 break;
425 }
426
427 auto child_path = parent + '/';
428
429 // Remove this parent if there isn't a remaining child on this owner
430 auto child = std::find_if(
431 interface_map.begin(), interface_map.end(),
432 [&owner, &child_path](const auto& entry) {
433 return boost::starts_with(entry.first, child_path) &&
434 (entry.second.find(owner) != entry.second.end());
435 });
436
437 if (child == interface_map.end())
438 {
439 parent_it->second.erase(ifaces_it);
440 if (parent_it->second.empty())
441 {
442 interface_map.erase(parent_it);
443 }
444 }
445 else
446 {
447 break;
448 }
449 }
450}
451
Ed Tanous60520632018-06-11 17:46:52 -0700452int main(int argc, char** argv)
453{
Matt Spinlerdd945862018-09-07 12:41:05 -0500454 auto options = ArgumentParser(argc, argv);
Ed Tanous21c60592020-08-17 23:43:46 -0700455 boost::asio::io_context io;
Ed Tanous60520632018-06-11 17:46:52 -0700456 std::shared_ptr<sdbusplus::asio::connection> system_bus =
457 std::make_shared<sdbusplus::asio::connection>(io);
458
Matt Spinlerdd945862018-09-07 12:41:05 -0500459 splitArgs(options["service-namespaces"], service_whitelist);
Matt Spinlerdd945862018-09-07 12:41:05 -0500460 splitArgs(options["service-blacklists"], service_blacklist);
461
Ed Tanousd4dd96a2018-11-12 11:37:44 -0800462 // TODO(Ed) Remove this once all service files are updated to not use this.
463 // For now, simply squash the input, and ignore it.
464 boost::container::flat_set<std::string> iface_whitelist;
465 splitArgs(options["interface-namespaces"], iface_whitelist);
466
Ed Tanous60520632018-06-11 17:46:52 -0700467 sdbusplus::asio::object_server server(system_bus);
468
469 // Construct a signal set registered for process termination.
470 boost::asio::signal_set signals(io, SIGINT, SIGTERM);
Brad Bishop2d41d6a2021-08-03 08:14:45 -0400471 signals.async_wait(
472 [&io](const boost::system::error_code&, int) { io.stop(); });
Ed Tanous60520632018-06-11 17:46:52 -0700473
474 interface_map_type interface_map;
475 boost::container::flat_map<std::string, std::string> name_owners;
476
477 std::function<void(sdbusplus::message::message & message)>
478 nameChangeHandler = [&interface_map, &io, &name_owners, &server,
479 system_bus](sdbusplus::message::message& message) {
Andrew Geissler20679262019-02-11 20:20:40 -0600480 std::string name; // well-known
481 std::string old_owner; // unique-name
482 std::string new_owner; // unique-name
Ed Tanous60520632018-06-11 17:46:52 -0700483
484 message.read(name, old_owner, new_owner);
485
486 if (!old_owner.empty())
487 {
Andrew Geissler20679262019-02-11 20:20:40 -0600488 processNameChangeDelete(name_owners, name, old_owner,
Matt Spinlere2359fb2019-04-05 14:11:33 -0500489 interface_map, associationMaps, server);
Ed Tanous60520632018-06-11 17:46:52 -0700490 }
491
492 if (!new_owner.empty())
493 {
Matt Spinleraecabe82018-09-19 13:25:42 -0500494#ifdef DEBUG
Ed Tanous60520632018-06-11 17:46:52 -0700495 auto transaction = std::make_shared<
496 std::chrono::time_point<std::chrono::steady_clock>>(
497 std::chrono::steady_clock::now());
Matt Spinleraecabe82018-09-19 13:25:42 -0500498#endif
Ed Tanous60520632018-06-11 17:46:52 -0700499 // New daemon added
Andrew Geissler82815da2019-02-04 12:19:41 -0600500 if (needToIntrospect(name, service_whitelist,
501 service_blacklist))
Ed Tanous60520632018-06-11 17:46:52 -0700502 {
503 name_owners[new_owner] = name;
504 start_new_introspect(system_bus.get(), io, interface_map,
Matt Spinler11401e22019-04-08 13:13:25 -0500505 name, associationMaps,
Matt Spinleraecabe82018-09-19 13:25:42 -0500506#ifdef DEBUG
507 transaction,
508#endif
509 server);
Ed Tanous60520632018-06-11 17:46:52 -0700510 }
511 }
512 };
513
514 sdbusplus::bus::match::match nameOwnerChanged(
515 static_cast<sdbusplus::bus::bus&>(*system_bus),
516 sdbusplus::bus::match::rules::nameOwnerChanged(), nameChangeHandler);
517
518 std::function<void(sdbusplus::message::message & message)>
519 interfacesAddedHandler = [&interface_map, &name_owners, &server](
520 sdbusplus::message::message& message) {
521 sdbusplus::message::object_path obj_path;
Andrew Geissler70461892019-02-27 09:57:37 -0600522 InterfacesAdded interfaces_added;
Ed Tanous60520632018-06-11 17:46:52 -0700523 message.read(obj_path, interfaces_added);
524 std::string well_known;
Andrew Geissler3b025e62019-02-01 10:33:54 -0600525 if (!getWellKnown(name_owners, message.get_sender(), well_known))
Ed Tanous60520632018-06-11 17:46:52 -0700526 {
527 return; // only introspect well-known
528 }
Andrew Geissler82815da2019-02-04 12:19:41 -0600529 if (needToIntrospect(well_known, service_whitelist,
530 service_blacklist))
Ed Tanous60520632018-06-11 17:46:52 -0700531 {
Andrew Geissler70461892019-02-27 09:57:37 -0600532 processInterfaceAdded(interface_map, obj_path, interfaces_added,
Matt Spinlere2359fb2019-04-05 14:11:33 -0500533 well_known, associationMaps, server);
Ed Tanous60520632018-06-11 17:46:52 -0700534 }
535 };
536
537 sdbusplus::bus::match::match interfacesAdded(
538 static_cast<sdbusplus::bus::bus&>(*system_bus),
539 sdbusplus::bus::match::rules::interfacesAdded(),
540 interfacesAddedHandler);
541
542 std::function<void(sdbusplus::message::message & message)>
543 interfacesRemovedHandler = [&interface_map, &name_owners, &server](
544 sdbusplus::message::message& message) {
545 sdbusplus::message::object_path obj_path;
546 std::vector<std::string> interfaces_removed;
547 message.read(obj_path, interfaces_removed);
548 auto connection_map = interface_map.find(obj_path.str);
549 if (connection_map == interface_map.end())
550 {
551 return;
552 }
553
554 std::string sender;
Andrew Geissler3b025e62019-02-01 10:33:54 -0600555 if (!getWellKnown(name_owners, message.get_sender(), sender))
Ed Tanous60520632018-06-11 17:46:52 -0700556 {
557 return;
558 }
559 for (const std::string& interface : interfaces_removed)
560 {
561 auto interface_set = connection_map->second.find(sender);
562 if (interface_set == connection_map->second.end())
563 {
564 continue;
565 }
566
John Wangd0cf9422019-09-17 16:01:34 +0800567 if (interface == assocDefsInterface)
Ed Tanous60520632018-06-11 17:46:52 -0700568 {
Andrew Geissler67e5a422019-02-04 13:00:59 -0600569 removeAssociation(obj_path.str, sender, server,
Matt Spinlere2359fb2019-04-05 14:11:33 -0500570 associationMaps);
Ed Tanous60520632018-06-11 17:46:52 -0700571 }
572
573 interface_set->second.erase(interface);
Matt Spinler9c3d2852019-04-08 15:57:19 -0500574
Ed Tanous60520632018-06-11 17:46:52 -0700575 if (interface_set->second.empty())
576 {
Matt Spinler9c3d2852019-04-08 15:57:19 -0500577 // If this was the last interface on this connection,
578 // erase the connection
Ed Tanous60520632018-06-11 17:46:52 -0700579 connection_map->second.erase(interface_set);
Matt Spinler9c3d2852019-04-08 15:57:19 -0500580
581 // Instead of checking if every single path is the endpoint
582 // of an association that needs to be moved to pending,
583 // only check when the only remaining owner of this path is
584 // ourself, which would be because we still own the
585 // association path.
586 if ((connection_map->second.size() == 1) &&
587 (connection_map->second.begin()->first ==
Matt Spinler5eddf442019-04-10 10:08:56 -0500588 MAPPER_BUSNAME))
Matt Spinler9c3d2852019-04-08 15:57:19 -0500589 {
590 // Remove the 2 association D-Bus paths and move the
591 // association to pending.
592 moveAssociationToPending(obj_path.str, associationMaps,
593 server);
594 }
Ed Tanous60520632018-06-11 17:46:52 -0700595 }
596 }
597 // If this was the last connection on this object path,
598 // erase the object path
599 if (connection_map->second.empty())
600 {
601 interface_map.erase(connection_map);
602 }
Matt Spinlera82779f2019-01-09 12:39:42 -0600603
604 removeUnneededParents(obj_path.str, sender, interface_map);
Ed Tanous60520632018-06-11 17:46:52 -0700605 };
606
607 sdbusplus::bus::match::match interfacesRemoved(
608 static_cast<sdbusplus::bus::bus&>(*system_bus),
609 sdbusplus::bus::match::rules::interfacesRemoved(),
610 interfacesRemovedHandler);
611
612 std::function<void(sdbusplus::message::message & message)>
Matt Spinlere0b0e3a2019-04-08 10:39:23 -0500613 associationChangedHandler = [&server, &name_owners, &interface_map](
Matt Spinler8f876a52019-04-15 13:22:50 -0500614 sdbusplus::message::message& message) {
615 std::string objectName;
Patrick Williams2bb2d6b2020-05-13 17:59:02 -0500616 boost::container::flat_map<std::string,
617 std::variant<std::vector<Association>>>
Matt Spinler8f876a52019-04-15 13:22:50 -0500618 values;
619 message.read(objectName, values);
John Wangd0cf9422019-09-17 16:01:34 +0800620 auto prop = values.find(assocDefsProperty);
Matt Spinler8f876a52019-04-15 13:22:50 -0500621 if (prop != values.end())
622 {
623 std::vector<Association> associations =
Patrick Williamsb05bc122020-05-13 12:21:00 -0500624 std::get<std::vector<Association>>(prop->second);
Matt Spinler8f876a52019-04-15 13:22:50 -0500625
626 std::string well_known;
627 if (!getWellKnown(name_owners, message.get_sender(),
628 well_known))
629 {
630 return;
Ed Tanous60520632018-06-11 17:46:52 -0700631 }
Matt Spinler8f876a52019-04-15 13:22:50 -0500632 associationChanged(server, associations, message.get_path(),
Matt Spinlere0b0e3a2019-04-08 10:39:23 -0500633 well_known, interface_map, associationMaps);
Matt Spinler8f876a52019-04-15 13:22:50 -0500634 }
635 };
636 sdbusplus::bus::match::match assocChangedMatch(
Ed Tanous60520632018-06-11 17:46:52 -0700637 static_cast<sdbusplus::bus::bus&>(*system_bus),
638 sdbusplus::bus::match::rules::interface(
639 "org.freedesktop.DBus.Properties") +
640 sdbusplus::bus::match::rules::member("PropertiesChanged") +
Matt Spinler8f876a52019-04-15 13:22:50 -0500641 sdbusplus::bus::match::rules::argN(0, assocDefsInterface),
642 associationChangedHandler);
643
Ed Tanous60520632018-06-11 17:46:52 -0700644 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
Matt Spinler5eddf442019-04-10 10:08:56 -0500645 server.add_interface(MAPPER_PATH, MAPPER_INTERFACE);
Ed Tanous60520632018-06-11 17:46:52 -0700646
647 iface->register_method(
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600648 "GetAncestors", [&interface_map](std::string& req_path,
Ed Tanous60520632018-06-11 17:46:52 -0700649 std::vector<std::string>& interfaces) {
650 // Interfaces need to be sorted for intersect to function
651 std::sort(interfaces.begin(), interfaces.end());
652
James Feistd7322872019-01-28 11:25:00 -0800653 if (boost::ends_with(req_path, "/"))
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600654 {
James Feistd7322872019-01-28 11:25:00 -0800655 req_path.pop_back();
656 }
657 if (req_path.size() &&
658 interface_map.find(req_path) == interface_map.end())
659 {
660 throw NotFoundException();
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600661 }
662
Ed Tanous60520632018-06-11 17:46:52 -0700663 std::vector<interface_map_type::value_type> ret;
664 for (auto& object_path : interface_map)
665 {
666 auto& this_path = object_path.first;
Matt Spinlerea6516c2018-09-25 16:23:13 -0500667 if (boost::starts_with(req_path, this_path) &&
668 (req_path != this_path))
Ed Tanous60520632018-06-11 17:46:52 -0700669 {
670 if (interfaces.empty())
671 {
672 ret.emplace_back(object_path);
673 }
674 else
675 {
676 for (auto& interface_map : object_path.second)
677 {
678
679 if (intersect(interfaces.begin(), interfaces.end(),
680 interface_map.second.begin(),
681 interface_map.second.end()))
682 {
Matt Spinler47c09752018-11-29 14:54:13 -0600683 addObjectMapResult(ret, this_path,
684 interface_map);
Ed Tanous60520632018-06-11 17:46:52 -0700685 }
686 }
687 }
688 }
689 }
Ed Tanous60520632018-06-11 17:46:52 -0700690
691 return ret;
692 });
693
694 iface->register_method(
695 "GetObject", [&interface_map](const std::string& path,
696 std::vector<std::string>& interfaces) {
Matt Spinler7a38d412018-09-18 16:13:25 -0500697 boost::container::flat_map<std::string,
698 boost::container::flat_set<std::string>>
699 results;
700
Ed Tanous60520632018-06-11 17:46:52 -0700701 // Interfaces need to be sorted for intersect to function
702 std::sort(interfaces.begin(), interfaces.end());
703 auto path_ref = interface_map.find(path);
704 if (path_ref == interface_map.end())
705 {
706 throw NotFoundException();
707 }
708 if (interfaces.empty())
709 {
710 return path_ref->second;
711 }
712 for (auto& interface_map : path_ref->second)
713 {
714 if (intersect(interfaces.begin(), interfaces.end(),
715 interface_map.second.begin(),
716 interface_map.second.end()))
717 {
Matt Spinler7a38d412018-09-18 16:13:25 -0500718 results.emplace(interface_map.first, interface_map.second);
Ed Tanous60520632018-06-11 17:46:52 -0700719 }
720 }
Matt Spinler7a38d412018-09-18 16:13:25 -0500721
722 if (results.empty())
723 {
724 throw NotFoundException();
725 }
726
727 return results;
Ed Tanous60520632018-06-11 17:46:52 -0700728 });
729
730 iface->register_method(
Matt Spinler153494e2018-11-07 16:35:40 -0600731 "GetSubTree", [&interface_map](std::string& req_path, int32_t depth,
732 std::vector<std::string>& interfaces) {
Ed Tanous60520632018-06-11 17:46:52 -0700733 if (depth <= 0)
734 {
735 depth = std::numeric_limits<int32_t>::max();
736 }
737 // Interfaces need to be sorted for intersect to function
738 std::sort(interfaces.begin(), interfaces.end());
739 std::vector<interface_map_type::value_type> ret;
740
James Feistd7322872019-01-28 11:25:00 -0800741 if (boost::ends_with(req_path, "/"))
Matt Spinler153494e2018-11-07 16:35:40 -0600742 {
James Feistd7322872019-01-28 11:25:00 -0800743 req_path.pop_back();
744 }
745 if (req_path.size() &&
746 interface_map.find(req_path) == interface_map.end())
747 {
748 throw NotFoundException();
Matt Spinler153494e2018-11-07 16:35:40 -0600749 }
750
Ed Tanous60520632018-06-11 17:46:52 -0700751 for (auto& object_path : interface_map)
752 {
753 auto& this_path = object_path.first;
Matt Spinler153494e2018-11-07 16:35:40 -0600754
755 if (this_path == req_path)
756 {
757 continue;
758 }
759
Ed Tanous60520632018-06-11 17:46:52 -0700760 if (boost::starts_with(this_path, req_path))
761 {
762 // count the number of slashes past the search term
763 int32_t this_depth =
764 std::count(this_path.begin() + req_path.size(),
765 this_path.end(), '/');
766 if (this_depth <= depth)
767 {
Ed Tanous60520632018-06-11 17:46:52 -0700768 for (auto& interface_map : object_path.second)
769 {
770 if (intersect(interfaces.begin(), interfaces.end(),
771 interface_map.second.begin(),
Matt Spinler9f0958e2018-09-11 08:26:10 -0500772 interface_map.second.end()) ||
773 interfaces.empty())
Ed Tanous60520632018-06-11 17:46:52 -0700774 {
Matt Spinler47c09752018-11-29 14:54:13 -0600775 addObjectMapResult(ret, this_path,
776 interface_map);
Ed Tanous60520632018-06-11 17:46:52 -0700777 }
778 }
Ed Tanous60520632018-06-11 17:46:52 -0700779 }
780 }
781 }
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600782
Ed Tanous60520632018-06-11 17:46:52 -0700783 return ret;
784 });
785
786 iface->register_method(
787 "GetSubTreePaths",
Matt Spinler153494e2018-11-07 16:35:40 -0600788 [&interface_map](std::string& req_path, int32_t depth,
Ed Tanous60520632018-06-11 17:46:52 -0700789 std::vector<std::string>& interfaces) {
790 if (depth <= 0)
791 {
792 depth = std::numeric_limits<int32_t>::max();
793 }
794 // Interfaces need to be sorted for intersect to function
795 std::sort(interfaces.begin(), interfaces.end());
796 std::vector<std::string> ret;
Matt Spinler153494e2018-11-07 16:35:40 -0600797
James Feistd7322872019-01-28 11:25:00 -0800798 if (boost::ends_with(req_path, "/"))
Matt Spinler153494e2018-11-07 16:35:40 -0600799 {
James Feistd7322872019-01-28 11:25:00 -0800800 req_path.pop_back();
801 }
802 if (req_path.size() &&
803 interface_map.find(req_path) == interface_map.end())
804 {
805 throw NotFoundException();
Matt Spinler153494e2018-11-07 16:35:40 -0600806 }
807
Ed Tanous60520632018-06-11 17:46:52 -0700808 for (auto& object_path : interface_map)
809 {
810 auto& this_path = object_path.first;
Matt Spinler153494e2018-11-07 16:35:40 -0600811
812 if (this_path == req_path)
813 {
814 continue;
815 }
816
Ed Tanous60520632018-06-11 17:46:52 -0700817 if (boost::starts_with(this_path, req_path))
818 {
819 // count the number of slashes past the search term
820 int this_depth =
821 std::count(this_path.begin() + req_path.size(),
822 this_path.end(), '/');
823 if (this_depth <= depth)
824 {
825 bool add = interfaces.empty();
826 for (auto& interface_map : object_path.second)
827 {
828 if (intersect(interfaces.begin(), interfaces.end(),
829 interface_map.second.begin(),
830 interface_map.second.end()))
831 {
832 add = true;
833 break;
834 }
835 }
836 if (add)
837 {
838 // TODO(ed) this is a copy
839 ret.emplace_back(this_path);
840 }
841 }
842 }
843 }
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600844
Ed Tanous60520632018-06-11 17:46:52 -0700845 return ret;
846 });
847
848 iface->initialize();
849
850 io.post([&]() {
Matt Spinler11401e22019-04-08 13:13:25 -0500851 doListNames(io, interface_map, system_bus.get(), name_owners,
852 associationMaps, server);
Ed Tanous60520632018-06-11 17:46:52 -0700853 });
854
Vishwanatha Subbanna64354ef2020-08-21 03:35:26 -0500855 system_bus->request_name(MAPPER_BUSNAME);
856
Ed Tanous60520632018-06-11 17:46:52 -0700857 io.run();
858}