blob: 37f216fe3024506b73a98d25386d085abf03e687 [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 };
Patrick Williamsbedd4162021-09-08 13:54:13 -050042
43 int get_errno() const noexcept override
44 {
45 return ENOENT;
46 }
Ed Tanous60520632018-06-11 17:46:52 -070047};
48
Ed Tanous60520632018-06-11 17:46:52 -070049void update_owners(sdbusplus::asio::connection* conn,
50 boost::container::flat_map<std::string, std::string>& owners,
51 const std::string& new_object)
52{
53 if (boost::starts_with(new_object, ":"))
54 {
55 return;
56 }
57 conn->async_method_call(
58 [&, new_object](const boost::system::error_code ec,
59 const std::string& nameOwner) {
60 if (ec)
61 {
62 std::cerr << "Error getting owner of " << new_object << " : "
63 << ec << "\n";
64 return;
65 }
66 owners[nameOwner] = new_object;
67 },
68 "org.freedesktop.DBus", "/", "org.freedesktop.DBus", "GetNameOwner",
69 new_object);
70}
71
72void send_introspection_complete_signal(sdbusplus::asio::connection* system_bus,
73 const std::string& process_name)
74{
75 // TODO(ed) This signal doesn't get exposed properly in the
76 // introspect right now. Find out how to register signals in
77 // sdbusplus
78 sdbusplus::message::message m = system_bus->new_signal(
Matt Spinler5eddf442019-04-10 10:08:56 -050079 MAPPER_PATH, "xyz.openbmc_project.ObjectMapper.Private",
80 "IntrospectionComplete");
Ed Tanous60520632018-06-11 17:46:52 -070081 m.append(process_name);
82 m.signal_send();
83}
84
85struct InProgressIntrospect
86{
87 InProgressIntrospect(
Ed Tanous21c60592020-08-17 23:43:46 -070088 sdbusplus::asio::connection* system_bus, boost::asio::io_context& io,
Matt Spinler11401e22019-04-08 13:13:25 -050089 const std::string& process_name, AssociationMaps& am
Matt Spinleraecabe82018-09-19 13:25:42 -050090#ifdef DEBUG
91 ,
Ed Tanous60520632018-06-11 17:46:52 -070092 std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>>
Matt Spinleraecabe82018-09-19 13:25:42 -050093 global_start_time
94#endif
95 ) :
Ed Tanous60520632018-06-11 17:46:52 -070096 system_bus(system_bus),
Matt Spinler11401e22019-04-08 13:13:25 -050097 io(io), process_name(process_name), assocMaps(am)
Matt Spinleraecabe82018-09-19 13:25:42 -050098#ifdef DEBUG
99 ,
Ed Tanous60520632018-06-11 17:46:52 -0700100 global_start_time(global_start_time),
101 process_start_time(std::chrono::steady_clock::now())
Matt Spinleraecabe82018-09-19 13:25:42 -0500102#endif
Ed Tanous60520632018-06-11 17:46:52 -0700103 {
104 }
105 ~InProgressIntrospect()
106 {
107 send_introspection_complete_signal(system_bus, process_name);
Matt Spinleraecabe82018-09-19 13:25:42 -0500108
109#ifdef DEBUG
Ed Tanous60520632018-06-11 17:46:52 -0700110 std::chrono::duration<float> diff =
111 std::chrono::steady_clock::now() - process_start_time;
112 std::cout << std::setw(50) << process_name << " scan took "
113 << diff.count() << " seconds\n";
114
115 // If we're the last outstanding caller globally, calculate the
116 // time it took
117 if (global_start_time != nullptr && global_start_time.use_count() == 1)
118 {
119 diff = std::chrono::steady_clock::now() - *global_start_time;
120 std::cout << "Total scan took " << diff.count()
121 << " seconds to complete\n";
122 }
Matt Spinleraecabe82018-09-19 13:25:42 -0500123#endif
Ed Tanous60520632018-06-11 17:46:52 -0700124 }
125 sdbusplus::asio::connection* system_bus;
Ed Tanous21c60592020-08-17 23:43:46 -0700126 boost::asio::io_context& io;
Ed Tanous60520632018-06-11 17:46:52 -0700127 std::string process_name;
Matt Spinler11401e22019-04-08 13:13:25 -0500128 AssociationMaps& assocMaps;
Matt Spinleraecabe82018-09-19 13:25:42 -0500129#ifdef DEBUG
Ed Tanous60520632018-06-11 17:46:52 -0700130 std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>>
131 global_start_time;
132 std::chrono::time_point<std::chrono::steady_clock> process_start_time;
Matt Spinleraecabe82018-09-19 13:25:42 -0500133#endif
Ed Tanous60520632018-06-11 17:46:52 -0700134};
135
Ed Tanous60520632018-06-11 17:46:52 -0700136void do_associations(sdbusplus::asio::connection* system_bus,
Matt Spinlere0b0e3a2019-04-08 10:39:23 -0500137 interface_map_type& interfaceMap,
Ed Tanous60520632018-06-11 17:46:52 -0700138 sdbusplus::asio::object_server& objectServer,
Adrian Ambrożewiczbb40bd32021-02-12 13:36:26 +0100139 const std::string& processName, const std::string& path,
140 int timeoutRetries = 0)
Ed Tanous60520632018-06-11 17:46:52 -0700141{
Adrian Ambrożewiczbb40bd32021-02-12 13:36:26 +0100142 constexpr int maxTimeoutRetries = 3;
Ed Tanous60520632018-06-11 17:46:52 -0700143 system_bus->async_method_call(
Adrian Ambrożewiczbb40bd32021-02-12 13:36:26 +0100144 [&objectServer, path, processName, &interfaceMap, system_bus,
145 timeoutRetries](
Matt Spinler937a2322019-01-23 13:54:22 -0600146 const boost::system::error_code ec,
Patrick Williams2bb2d6b2020-05-13 17:59:02 -0500147 const std::variant<std::vector<Association>>& variantAssociations) {
Ed Tanous60520632018-06-11 17:46:52 -0700148 if (ec)
149 {
Adrian Ambrożewiczbb40bd32021-02-12 13:36:26 +0100150 if (ec.value() == boost::system::errc::timed_out &&
151 timeoutRetries < maxTimeoutRetries)
152 {
153 do_associations(system_bus, interfaceMap, objectServer,
154 processName, path, timeoutRetries + 1);
155 return;
156 }
Ed Tanous60520632018-06-11 17:46:52 -0700157 std::cerr << "Error getting associations from " << path << "\n";
158 }
159 std::vector<Association> associations =
Patrick Williamsb05bc122020-05-13 12:21:00 -0500160 std::get<std::vector<Association>>(variantAssociations);
Andrew Geissler4511b332019-02-21 15:40:40 -0600161 associationChanged(objectServer, associations, path, processName,
Matt Spinlere0b0e3a2019-04-08 10:39:23 -0500162 interfaceMap, associationMaps);
Ed Tanous60520632018-06-11 17:46:52 -0700163 },
164 processName, path, "org.freedesktop.DBus.Properties", "Get",
John Wangd0cf9422019-09-17 16:01:34 +0800165 assocDefsInterface, assocDefsProperty);
Ed Tanous60520632018-06-11 17:46:52 -0700166}
167
168void do_introspect(sdbusplus::asio::connection* system_bus,
169 std::shared_ptr<InProgressIntrospect> transaction,
170 interface_map_type& interface_map,
171 sdbusplus::asio::object_server& objectServer,
Vernon Maueryc52be0d2020-01-14 11:14:25 -0800172 std::string path, int timeoutRetries = 0)
Ed Tanous60520632018-06-11 17:46:52 -0700173{
Vernon Maueryc52be0d2020-01-14 11:14:25 -0800174 constexpr int maxTimeoutRetries = 3;
Ed Tanous60520632018-06-11 17:46:52 -0700175 system_bus->async_method_call(
Vernon Maueryc52be0d2020-01-14 11:14:25 -0800176 [&interface_map, &objectServer, transaction, path, system_bus,
177 timeoutRetries](const boost::system::error_code ec,
178 const std::string& introspect_xml) {
Ed Tanous60520632018-06-11 17:46:52 -0700179 if (ec)
180 {
Vernon Maueryc52be0d2020-01-14 11:14:25 -0800181 if (ec.value() == boost::system::errc::timed_out &&
182 timeoutRetries < maxTimeoutRetries)
183 {
184 do_introspect(system_bus, transaction, interface_map,
185 objectServer, path, timeoutRetries + 1);
186 return;
187 }
Ed Tanous60520632018-06-11 17:46:52 -0700188 std::cerr << "Introspect call failed with error: " << ec << ", "
189 << ec.message()
190 << " on process: " << transaction->process_name
191 << " path: " << path << "\n";
192 return;
193 }
194
195 tinyxml2::XMLDocument doc;
196
197 tinyxml2::XMLError e = doc.Parse(introspect_xml.c_str());
198 if (e != tinyxml2::XMLError::XML_SUCCESS)
199 {
200 std::cerr << "XML parsing failed\n";
201 return;
202 }
203
204 tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node");
205 if (pRoot == nullptr)
206 {
207 std::cerr << "XML document did not contain any data\n";
208 return;
209 }
210 auto& thisPathMap = interface_map[path];
Ed Tanous60520632018-06-11 17:46:52 -0700211 tinyxml2::XMLElement* pElement =
212 pRoot->FirstChildElement("interface");
213 while (pElement != nullptr)
214 {
215 const char* iface_name = pElement->Attribute("name");
216 if (iface_name == nullptr)
217 {
218 continue;
219 }
220
Ed Tanousd4dd96a2018-11-12 11:37:44 -0800221 thisPathMap[transaction->process_name].emplace(iface_name);
222
John Wangd0cf9422019-09-17 16:01:34 +0800223 if (std::strcmp(iface_name, assocDefsInterface) == 0)
Ed Tanous60520632018-06-11 17:46:52 -0700224 {
Matt Spinlere0b0e3a2019-04-08 10:39:23 -0500225 do_associations(system_bus, interface_map, objectServer,
John Wangd0cf9422019-09-17 16:01:34 +0800226 transaction->process_name, path);
Ed Tanous60520632018-06-11 17:46:52 -0700227 }
Ed Tanous60520632018-06-11 17:46:52 -0700228
229 pElement = pElement->NextSiblingElement("interface");
230 }
231
Matt Spinler11401e22019-04-08 13:13:25 -0500232 // Check if this new path has a pending association that can
233 // now be completed.
234 checkIfPendingAssociation(path, interface_map,
235 transaction->assocMaps, objectServer);
236
Ed Tanous50232cd2018-11-12 11:34:43 -0800237 pElement = pRoot->FirstChildElement("node");
238 while (pElement != nullptr)
Ed Tanous60520632018-06-11 17:46:52 -0700239 {
Ed Tanous50232cd2018-11-12 11:34:43 -0800240 const char* child_path = pElement->Attribute("name");
241 if (child_path != nullptr)
Ed Tanous60520632018-06-11 17:46:52 -0700242 {
Ed Tanous50232cd2018-11-12 11:34:43 -0800243 std::string parent_path(path);
244 if (parent_path == "/")
Ed Tanous60520632018-06-11 17:46:52 -0700245 {
Ed Tanous50232cd2018-11-12 11:34:43 -0800246 parent_path.clear();
Ed Tanous60520632018-06-11 17:46:52 -0700247 }
Ed Tanous50232cd2018-11-12 11:34:43 -0800248
249 do_introspect(system_bus, transaction, interface_map,
250 objectServer, parent_path + "/" + child_path);
Ed Tanous60520632018-06-11 17:46:52 -0700251 }
Ed Tanous50232cd2018-11-12 11:34:43 -0800252 pElement = pElement->NextSiblingElement("node");
Ed Tanous60520632018-06-11 17:46:52 -0700253 }
254 },
255 transaction->process_name, path, "org.freedesktop.DBus.Introspectable",
256 "Introspect");
257}
258
Ed Tanous60520632018-06-11 17:46:52 -0700259void start_new_introspect(
Ed Tanous21c60592020-08-17 23:43:46 -0700260 sdbusplus::asio::connection* system_bus, boost::asio::io_context& io,
Ed Tanous60520632018-06-11 17:46:52 -0700261 interface_map_type& interface_map, const std::string& process_name,
Matt Spinler11401e22019-04-08 13:13:25 -0500262 AssociationMaps& assocMaps,
Matt Spinleraecabe82018-09-19 13:25:42 -0500263#ifdef DEBUG
Ed Tanous60520632018-06-11 17:46:52 -0700264 std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>>
265 global_start_time,
Matt Spinleraecabe82018-09-19 13:25:42 -0500266#endif
Ed Tanous60520632018-06-11 17:46:52 -0700267 sdbusplus::asio::object_server& objectServer)
268{
Andrew Geissler82815da2019-02-04 12:19:41 -0600269 if (needToIntrospect(process_name, service_whitelist, service_blacklist))
Ed Tanous60520632018-06-11 17:46:52 -0700270 {
Ed Tanous60520632018-06-11 17:46:52 -0700271 std::shared_ptr<InProgressIntrospect> transaction =
Matt Spinler11401e22019-04-08 13:13:25 -0500272 std::make_shared<InProgressIntrospect>(system_bus, io, process_name,
273 assocMaps
Matt Spinleraecabe82018-09-19 13:25:42 -0500274#ifdef DEBUG
275 ,
276 global_start_time
277#endif
278 );
Ed Tanous60520632018-06-11 17:46:52 -0700279
280 do_introspect(system_bus, transaction, interface_map, objectServer,
281 "/");
282 }
283}
284
285// TODO(ed) replace with std::set_intersection once c++17 is available
286template <class InputIt1, class InputIt2>
287bool intersect(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2)
288{
289 while (first1 != last1 && first2 != last2)
290 {
291 if (*first1 < *first2)
292 {
293 ++first1;
294 continue;
295 }
296 if (*first2 < *first1)
297 {
298 ++first2;
299 continue;
300 }
301 return true;
302 }
303 return false;
304}
305
306void doListNames(
Ed Tanous21c60592020-08-17 23:43:46 -0700307 boost::asio::io_context& io, interface_map_type& interface_map,
Ed Tanous60520632018-06-11 17:46:52 -0700308 sdbusplus::asio::connection* system_bus,
309 boost::container::flat_map<std::string, std::string>& name_owners,
Matt Spinler11401e22019-04-08 13:13:25 -0500310 AssociationMaps& assocMaps, sdbusplus::asio::object_server& objectServer)
Ed Tanous60520632018-06-11 17:46:52 -0700311{
312 system_bus->async_method_call(
Matt Spinler11401e22019-04-08 13:13:25 -0500313 [&io, &interface_map, &name_owners, &objectServer, system_bus,
314 &assocMaps](const boost::system::error_code ec,
Ed Tanous60520632018-06-11 17:46:52 -0700315 std::vector<std::string> process_names) {
316 if (ec)
317 {
318 std::cerr << "Error getting names: " << ec << "\n";
319 std::exit(EXIT_FAILURE);
320 return;
321 }
Ed Tanous60520632018-06-11 17:46:52 -0700322 // Try to make startup consistent
323 std::sort(process_names.begin(), process_names.end());
Matt Spinleraecabe82018-09-19 13:25:42 -0500324#ifdef DEBUG
Ed Tanous60520632018-06-11 17:46:52 -0700325 std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>>
326 global_start_time = std::make_shared<
327 std::chrono::time_point<std::chrono::steady_clock>>(
328 std::chrono::steady_clock::now());
Matt Spinleraecabe82018-09-19 13:25:42 -0500329#endif
Ed Tanous60520632018-06-11 17:46:52 -0700330 for (const std::string& process_name : process_names)
331 {
Andrew Geissler82815da2019-02-04 12:19:41 -0600332 if (needToIntrospect(process_name, service_whitelist,
333 service_blacklist))
Ed Tanous60520632018-06-11 17:46:52 -0700334 {
335 start_new_introspect(system_bus, io, interface_map,
Matt Spinler11401e22019-04-08 13:13:25 -0500336 process_name, assocMaps,
Matt Spinleraecabe82018-09-19 13:25:42 -0500337#ifdef DEBUG
338 global_start_time,
339#endif
Ed Tanous60520632018-06-11 17:46:52 -0700340 objectServer);
341 update_owners(system_bus, name_owners, process_name);
342 }
343 }
344 },
345 "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus",
346 "ListNames");
347}
348
Matt Spinlerdd945862018-09-07 12:41:05 -0500349void splitArgs(const std::string& stringArgs,
350 boost::container::flat_set<std::string>& listArgs)
351{
352 std::istringstream args;
353 std::string arg;
354
355 args.str(stringArgs);
356
357 while (!args.eof())
358 {
359 args >> arg;
360 if (!arg.empty())
361 {
362 listArgs.insert(arg);
363 }
364 }
365}
366
Matt Spinler47c09752018-11-29 14:54:13 -0600367void addObjectMapResult(
368 std::vector<interface_map_type::value_type>& objectMap,
Matt Spinler9f0958e2018-09-11 08:26:10 -0500369 const std::string& objectPath,
370 const std::pair<std::string, boost::container::flat_set<std::string>>&
371 interfaceMap)
372{
373 // Adds an object path/service name/interface list entry to
Matt Spinler47c09752018-11-29 14:54:13 -0600374 // the results of GetSubTree and GetAncestors.
Matt Spinler9f0958e2018-09-11 08:26:10 -0500375 // If an entry for the object path already exists, just add the
376 // service name and interfaces to that entry, otherwise create
377 // a new entry.
378 auto entry = std::find_if(
Matt Spinler47c09752018-11-29 14:54:13 -0600379 objectMap.begin(), objectMap.end(),
Matt Spinler9f0958e2018-09-11 08:26:10 -0500380 [&objectPath](const auto& i) { return objectPath == i.first; });
381
Matt Spinler47c09752018-11-29 14:54:13 -0600382 if (entry != objectMap.end())
Matt Spinler9f0958e2018-09-11 08:26:10 -0500383 {
384 entry->second.emplace(interfaceMap);
385 }
386 else
387 {
388 interface_map_type::value_type object;
389 object.first = objectPath;
390 object.second.emplace(interfaceMap);
Matt Spinler47c09752018-11-29 14:54:13 -0600391 objectMap.push_back(object);
Matt Spinler9f0958e2018-09-11 08:26:10 -0500392 }
393}
394
Matt Spinlera82779f2019-01-09 12:39:42 -0600395// Remove parents of the passed in path that:
396// 1) Only have the 3 default interfaces on them
397// - Means D-Bus created these, not application code,
398// with the Properties, Introspectable, and Peer ifaces
399// 2) Have no other child for this owner
400void removeUnneededParents(const std::string& objectPath,
401 const std::string& owner,
402 interface_map_type& interface_map)
403{
404 auto parent = objectPath;
405
406 while (true)
407 {
408 auto pos = parent.find_last_of('/');
409 if ((pos == std::string::npos) || (pos == 0))
410 {
411 break;
412 }
413 parent = parent.substr(0, pos);
414
415 auto parent_it = interface_map.find(parent);
416 if (parent_it == interface_map.end())
417 {
418 break;
419 }
420
421 auto ifaces_it = parent_it->second.find(owner);
422 if (ifaces_it == parent_it->second.end())
423 {
424 break;
425 }
426
427 if (ifaces_it->second.size() != 3)
428 {
429 break;
430 }
431
432 auto child_path = parent + '/';
433
434 // Remove this parent if there isn't a remaining child on this owner
435 auto child = std::find_if(
436 interface_map.begin(), interface_map.end(),
437 [&owner, &child_path](const auto& entry) {
438 return boost::starts_with(entry.first, child_path) &&
439 (entry.second.find(owner) != entry.second.end());
440 });
441
442 if (child == interface_map.end())
443 {
444 parent_it->second.erase(ifaces_it);
445 if (parent_it->second.empty())
446 {
447 interface_map.erase(parent_it);
448 }
449 }
450 else
451 {
452 break;
453 }
454 }
455}
456
Ed Tanous60520632018-06-11 17:46:52 -0700457int main(int argc, char** argv)
458{
Matt Spinlerdd945862018-09-07 12:41:05 -0500459 auto options = ArgumentParser(argc, argv);
Ed Tanous21c60592020-08-17 23:43:46 -0700460 boost::asio::io_context io;
Ed Tanous60520632018-06-11 17:46:52 -0700461 std::shared_ptr<sdbusplus::asio::connection> system_bus =
462 std::make_shared<sdbusplus::asio::connection>(io);
463
Matt Spinlerdd945862018-09-07 12:41:05 -0500464 splitArgs(options["service-namespaces"], service_whitelist);
Matt Spinlerdd945862018-09-07 12:41:05 -0500465 splitArgs(options["service-blacklists"], service_blacklist);
466
Ed Tanousd4dd96a2018-11-12 11:37:44 -0800467 // TODO(Ed) Remove this once all service files are updated to not use this.
468 // For now, simply squash the input, and ignore it.
469 boost::container::flat_set<std::string> iface_whitelist;
470 splitArgs(options["interface-namespaces"], iface_whitelist);
471
Ed Tanous60520632018-06-11 17:46:52 -0700472 sdbusplus::asio::object_server server(system_bus);
473
474 // Construct a signal set registered for process termination.
475 boost::asio::signal_set signals(io, SIGINT, SIGTERM);
Brad Bishop2d41d6a2021-08-03 08:14:45 -0400476 signals.async_wait(
477 [&io](const boost::system::error_code&, int) { io.stop(); });
Ed Tanous60520632018-06-11 17:46:52 -0700478
479 interface_map_type interface_map;
480 boost::container::flat_map<std::string, std::string> name_owners;
481
482 std::function<void(sdbusplus::message::message & message)>
483 nameChangeHandler = [&interface_map, &io, &name_owners, &server,
484 system_bus](sdbusplus::message::message& message) {
Andrew Geissler20679262019-02-11 20:20:40 -0600485 std::string name; // well-known
486 std::string old_owner; // unique-name
487 std::string new_owner; // unique-name
Ed Tanous60520632018-06-11 17:46:52 -0700488
489 message.read(name, old_owner, new_owner);
490
491 if (!old_owner.empty())
492 {
Andrew Geissler20679262019-02-11 20:20:40 -0600493 processNameChangeDelete(name_owners, name, old_owner,
Matt Spinlere2359fb2019-04-05 14:11:33 -0500494 interface_map, associationMaps, server);
Ed Tanous60520632018-06-11 17:46:52 -0700495 }
496
497 if (!new_owner.empty())
498 {
Matt Spinleraecabe82018-09-19 13:25:42 -0500499#ifdef DEBUG
Ed Tanous60520632018-06-11 17:46:52 -0700500 auto transaction = std::make_shared<
501 std::chrono::time_point<std::chrono::steady_clock>>(
502 std::chrono::steady_clock::now());
Matt Spinleraecabe82018-09-19 13:25:42 -0500503#endif
Ed Tanous60520632018-06-11 17:46:52 -0700504 // New daemon added
Andrew Geissler82815da2019-02-04 12:19:41 -0600505 if (needToIntrospect(name, service_whitelist,
506 service_blacklist))
Ed Tanous60520632018-06-11 17:46:52 -0700507 {
508 name_owners[new_owner] = name;
509 start_new_introspect(system_bus.get(), io, interface_map,
Matt Spinler11401e22019-04-08 13:13:25 -0500510 name, associationMaps,
Matt Spinleraecabe82018-09-19 13:25:42 -0500511#ifdef DEBUG
512 transaction,
513#endif
514 server);
Ed Tanous60520632018-06-11 17:46:52 -0700515 }
516 }
517 };
518
519 sdbusplus::bus::match::match nameOwnerChanged(
520 static_cast<sdbusplus::bus::bus&>(*system_bus),
521 sdbusplus::bus::match::rules::nameOwnerChanged(), nameChangeHandler);
522
523 std::function<void(sdbusplus::message::message & message)>
524 interfacesAddedHandler = [&interface_map, &name_owners, &server](
525 sdbusplus::message::message& message) {
526 sdbusplus::message::object_path obj_path;
Andrew Geissler70461892019-02-27 09:57:37 -0600527 InterfacesAdded interfaces_added;
Ed Tanous60520632018-06-11 17:46:52 -0700528 message.read(obj_path, interfaces_added);
529 std::string well_known;
Andrew Geissler3b025e62019-02-01 10:33:54 -0600530 if (!getWellKnown(name_owners, message.get_sender(), well_known))
Ed Tanous60520632018-06-11 17:46:52 -0700531 {
532 return; // only introspect well-known
533 }
Andrew Geissler82815da2019-02-04 12:19:41 -0600534 if (needToIntrospect(well_known, service_whitelist,
535 service_blacklist))
Ed Tanous60520632018-06-11 17:46:52 -0700536 {
Andrew Geissler70461892019-02-27 09:57:37 -0600537 processInterfaceAdded(interface_map, obj_path, interfaces_added,
Matt Spinlere2359fb2019-04-05 14:11:33 -0500538 well_known, associationMaps, server);
Ed Tanous60520632018-06-11 17:46:52 -0700539 }
540 };
541
542 sdbusplus::bus::match::match interfacesAdded(
543 static_cast<sdbusplus::bus::bus&>(*system_bus),
544 sdbusplus::bus::match::rules::interfacesAdded(),
545 interfacesAddedHandler);
546
547 std::function<void(sdbusplus::message::message & message)>
548 interfacesRemovedHandler = [&interface_map, &name_owners, &server](
549 sdbusplus::message::message& message) {
550 sdbusplus::message::object_path obj_path;
551 std::vector<std::string> interfaces_removed;
552 message.read(obj_path, interfaces_removed);
553 auto connection_map = interface_map.find(obj_path.str);
554 if (connection_map == interface_map.end())
555 {
556 return;
557 }
558
559 std::string sender;
Andrew Geissler3b025e62019-02-01 10:33:54 -0600560 if (!getWellKnown(name_owners, message.get_sender(), sender))
Ed Tanous60520632018-06-11 17:46:52 -0700561 {
562 return;
563 }
564 for (const std::string& interface : interfaces_removed)
565 {
566 auto interface_set = connection_map->second.find(sender);
567 if (interface_set == connection_map->second.end())
568 {
569 continue;
570 }
571
John Wangd0cf9422019-09-17 16:01:34 +0800572 if (interface == assocDefsInterface)
Ed Tanous60520632018-06-11 17:46:52 -0700573 {
Andrew Geissler67e5a422019-02-04 13:00:59 -0600574 removeAssociation(obj_path.str, sender, server,
Matt Spinlere2359fb2019-04-05 14:11:33 -0500575 associationMaps);
Ed Tanous60520632018-06-11 17:46:52 -0700576 }
577
578 interface_set->second.erase(interface);
Matt Spinler9c3d2852019-04-08 15:57:19 -0500579
Ed Tanous60520632018-06-11 17:46:52 -0700580 if (interface_set->second.empty())
581 {
Matt Spinler9c3d2852019-04-08 15:57:19 -0500582 // If this was the last interface on this connection,
583 // erase the connection
Ed Tanous60520632018-06-11 17:46:52 -0700584 connection_map->second.erase(interface_set);
Matt Spinler9c3d2852019-04-08 15:57:19 -0500585
586 // Instead of checking if every single path is the endpoint
587 // of an association that needs to be moved to pending,
588 // only check when the only remaining owner of this path is
589 // ourself, which would be because we still own the
590 // association path.
591 if ((connection_map->second.size() == 1) &&
592 (connection_map->second.begin()->first ==
Matt Spinler5eddf442019-04-10 10:08:56 -0500593 MAPPER_BUSNAME))
Matt Spinler9c3d2852019-04-08 15:57:19 -0500594 {
595 // Remove the 2 association D-Bus paths and move the
596 // association to pending.
597 moveAssociationToPending(obj_path.str, associationMaps,
598 server);
599 }
Ed Tanous60520632018-06-11 17:46:52 -0700600 }
601 }
602 // If this was the last connection on this object path,
603 // erase the object path
604 if (connection_map->second.empty())
605 {
606 interface_map.erase(connection_map);
607 }
Matt Spinlera82779f2019-01-09 12:39:42 -0600608
609 removeUnneededParents(obj_path.str, sender, interface_map);
Ed Tanous60520632018-06-11 17:46:52 -0700610 };
611
612 sdbusplus::bus::match::match interfacesRemoved(
613 static_cast<sdbusplus::bus::bus&>(*system_bus),
614 sdbusplus::bus::match::rules::interfacesRemoved(),
615 interfacesRemovedHandler);
616
617 std::function<void(sdbusplus::message::message & message)>
Matt Spinlere0b0e3a2019-04-08 10:39:23 -0500618 associationChangedHandler = [&server, &name_owners, &interface_map](
Matt Spinler8f876a52019-04-15 13:22:50 -0500619 sdbusplus::message::message& message) {
620 std::string objectName;
Patrick Williams2bb2d6b2020-05-13 17:59:02 -0500621 boost::container::flat_map<std::string,
622 std::variant<std::vector<Association>>>
Matt Spinler8f876a52019-04-15 13:22:50 -0500623 values;
624 message.read(objectName, values);
John Wangd0cf9422019-09-17 16:01:34 +0800625 auto prop = values.find(assocDefsProperty);
Matt Spinler8f876a52019-04-15 13:22:50 -0500626 if (prop != values.end())
627 {
628 std::vector<Association> associations =
Patrick Williamsb05bc122020-05-13 12:21:00 -0500629 std::get<std::vector<Association>>(prop->second);
Matt Spinler8f876a52019-04-15 13:22:50 -0500630
631 std::string well_known;
632 if (!getWellKnown(name_owners, message.get_sender(),
633 well_known))
634 {
635 return;
Ed Tanous60520632018-06-11 17:46:52 -0700636 }
Matt Spinler8f876a52019-04-15 13:22:50 -0500637 associationChanged(server, associations, message.get_path(),
Matt Spinlere0b0e3a2019-04-08 10:39:23 -0500638 well_known, interface_map, associationMaps);
Matt Spinler8f876a52019-04-15 13:22:50 -0500639 }
640 };
641 sdbusplus::bus::match::match assocChangedMatch(
Ed Tanous60520632018-06-11 17:46:52 -0700642 static_cast<sdbusplus::bus::bus&>(*system_bus),
643 sdbusplus::bus::match::rules::interface(
644 "org.freedesktop.DBus.Properties") +
645 sdbusplus::bus::match::rules::member("PropertiesChanged") +
Matt Spinler8f876a52019-04-15 13:22:50 -0500646 sdbusplus::bus::match::rules::argN(0, assocDefsInterface),
647 associationChangedHandler);
648
Ed Tanous60520632018-06-11 17:46:52 -0700649 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
Matt Spinler5eddf442019-04-10 10:08:56 -0500650 server.add_interface(MAPPER_PATH, MAPPER_INTERFACE);
Ed Tanous60520632018-06-11 17:46:52 -0700651
652 iface->register_method(
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600653 "GetAncestors", [&interface_map](std::string& req_path,
Ed Tanous60520632018-06-11 17:46:52 -0700654 std::vector<std::string>& interfaces) {
655 // Interfaces need to be sorted for intersect to function
656 std::sort(interfaces.begin(), interfaces.end());
657
James Feistd7322872019-01-28 11:25:00 -0800658 if (boost::ends_with(req_path, "/"))
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600659 {
James Feistd7322872019-01-28 11:25:00 -0800660 req_path.pop_back();
661 }
662 if (req_path.size() &&
663 interface_map.find(req_path) == interface_map.end())
664 {
665 throw NotFoundException();
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600666 }
667
Ed Tanous60520632018-06-11 17:46:52 -0700668 std::vector<interface_map_type::value_type> ret;
669 for (auto& object_path : interface_map)
670 {
671 auto& this_path = object_path.first;
Matt Spinlerea6516c2018-09-25 16:23:13 -0500672 if (boost::starts_with(req_path, this_path) &&
673 (req_path != this_path))
Ed Tanous60520632018-06-11 17:46:52 -0700674 {
675 if (interfaces.empty())
676 {
677 ret.emplace_back(object_path);
678 }
679 else
680 {
681 for (auto& interface_map : object_path.second)
682 {
683
684 if (intersect(interfaces.begin(), interfaces.end(),
685 interface_map.second.begin(),
686 interface_map.second.end()))
687 {
Matt Spinler47c09752018-11-29 14:54:13 -0600688 addObjectMapResult(ret, this_path,
689 interface_map);
Ed Tanous60520632018-06-11 17:46:52 -0700690 }
691 }
692 }
693 }
694 }
Ed Tanous60520632018-06-11 17:46:52 -0700695
696 return ret;
697 });
698
699 iface->register_method(
700 "GetObject", [&interface_map](const std::string& path,
701 std::vector<std::string>& interfaces) {
Matt Spinler7a38d412018-09-18 16:13:25 -0500702 boost::container::flat_map<std::string,
703 boost::container::flat_set<std::string>>
704 results;
705
Ed Tanous60520632018-06-11 17:46:52 -0700706 // Interfaces need to be sorted for intersect to function
707 std::sort(interfaces.begin(), interfaces.end());
708 auto path_ref = interface_map.find(path);
709 if (path_ref == interface_map.end())
710 {
711 throw NotFoundException();
712 }
713 if (interfaces.empty())
714 {
715 return path_ref->second;
716 }
717 for (auto& interface_map : path_ref->second)
718 {
719 if (intersect(interfaces.begin(), interfaces.end(),
720 interface_map.second.begin(),
721 interface_map.second.end()))
722 {
Matt Spinler7a38d412018-09-18 16:13:25 -0500723 results.emplace(interface_map.first, interface_map.second);
Ed Tanous60520632018-06-11 17:46:52 -0700724 }
725 }
Matt Spinler7a38d412018-09-18 16:13:25 -0500726
727 if (results.empty())
728 {
729 throw NotFoundException();
730 }
731
732 return results;
Ed Tanous60520632018-06-11 17:46:52 -0700733 });
734
735 iface->register_method(
Matt Spinler153494e2018-11-07 16:35:40 -0600736 "GetSubTree", [&interface_map](std::string& req_path, int32_t depth,
737 std::vector<std::string>& interfaces) {
Ed Tanous60520632018-06-11 17:46:52 -0700738 if (depth <= 0)
739 {
740 depth = std::numeric_limits<int32_t>::max();
741 }
742 // Interfaces need to be sorted for intersect to function
743 std::sort(interfaces.begin(), interfaces.end());
744 std::vector<interface_map_type::value_type> ret;
745
James Feistd7322872019-01-28 11:25:00 -0800746 if (boost::ends_with(req_path, "/"))
Matt Spinler153494e2018-11-07 16:35:40 -0600747 {
James Feistd7322872019-01-28 11:25:00 -0800748 req_path.pop_back();
749 }
750 if (req_path.size() &&
751 interface_map.find(req_path) == interface_map.end())
752 {
753 throw NotFoundException();
Matt Spinler153494e2018-11-07 16:35:40 -0600754 }
755
Ed Tanous60520632018-06-11 17:46:52 -0700756 for (auto& object_path : interface_map)
757 {
758 auto& this_path = object_path.first;
Matt Spinler153494e2018-11-07 16:35:40 -0600759
760 if (this_path == req_path)
761 {
762 continue;
763 }
764
Ed Tanous60520632018-06-11 17:46:52 -0700765 if (boost::starts_with(this_path, req_path))
766 {
767 // count the number of slashes past the search term
768 int32_t this_depth =
769 std::count(this_path.begin() + req_path.size(),
770 this_path.end(), '/');
771 if (this_depth <= depth)
772 {
Ed Tanous60520632018-06-11 17:46:52 -0700773 for (auto& interface_map : object_path.second)
774 {
775 if (intersect(interfaces.begin(), interfaces.end(),
776 interface_map.second.begin(),
Matt Spinler9f0958e2018-09-11 08:26:10 -0500777 interface_map.second.end()) ||
778 interfaces.empty())
Ed Tanous60520632018-06-11 17:46:52 -0700779 {
Matt Spinler47c09752018-11-29 14:54:13 -0600780 addObjectMapResult(ret, this_path,
781 interface_map);
Ed Tanous60520632018-06-11 17:46:52 -0700782 }
783 }
Ed Tanous60520632018-06-11 17:46:52 -0700784 }
785 }
786 }
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600787
Ed Tanous60520632018-06-11 17:46:52 -0700788 return ret;
789 });
790
791 iface->register_method(
792 "GetSubTreePaths",
Matt Spinler153494e2018-11-07 16:35:40 -0600793 [&interface_map](std::string& req_path, int32_t depth,
Ed Tanous60520632018-06-11 17:46:52 -0700794 std::vector<std::string>& interfaces) {
795 if (depth <= 0)
796 {
797 depth = std::numeric_limits<int32_t>::max();
798 }
799 // Interfaces need to be sorted for intersect to function
800 std::sort(interfaces.begin(), interfaces.end());
801 std::vector<std::string> ret;
Matt Spinler153494e2018-11-07 16:35:40 -0600802
James Feistd7322872019-01-28 11:25:00 -0800803 if (boost::ends_with(req_path, "/"))
Matt Spinler153494e2018-11-07 16:35:40 -0600804 {
James Feistd7322872019-01-28 11:25:00 -0800805 req_path.pop_back();
806 }
807 if (req_path.size() &&
808 interface_map.find(req_path) == interface_map.end())
809 {
810 throw NotFoundException();
Matt Spinler153494e2018-11-07 16:35:40 -0600811 }
812
Ed Tanous60520632018-06-11 17:46:52 -0700813 for (auto& object_path : interface_map)
814 {
815 auto& this_path = object_path.first;
Matt Spinler153494e2018-11-07 16:35:40 -0600816
817 if (this_path == req_path)
818 {
819 continue;
820 }
821
Ed Tanous60520632018-06-11 17:46:52 -0700822 if (boost::starts_with(this_path, req_path))
823 {
824 // count the number of slashes past the search term
825 int this_depth =
826 std::count(this_path.begin() + req_path.size(),
827 this_path.end(), '/');
828 if (this_depth <= depth)
829 {
830 bool add = interfaces.empty();
831 for (auto& interface_map : object_path.second)
832 {
833 if (intersect(interfaces.begin(), interfaces.end(),
834 interface_map.second.begin(),
835 interface_map.second.end()))
836 {
837 add = true;
838 break;
839 }
840 }
841 if (add)
842 {
843 // TODO(ed) this is a copy
844 ret.emplace_back(this_path);
845 }
846 }
847 }
848 }
Matt Spinler6a39e8c2018-11-13 11:11:36 -0600849
Ed Tanous60520632018-06-11 17:46:52 -0700850 return ret;
851 });
852
853 iface->initialize();
854
855 io.post([&]() {
Matt Spinler11401e22019-04-08 13:13:25 -0500856 doListNames(io, interface_map, system_bus.get(), name_owners,
857 associationMaps, server);
Ed Tanous60520632018-06-11 17:46:52 -0700858 });
859
Vishwanatha Subbanna64354ef2020-08-21 03:35:26 -0500860 system_bus->request_name(MAPPER_BUSNAME);
861
Ed Tanous60520632018-06-11 17:46:52 -0700862 io.run();
863}