Matt Spinler | 5eddf44 | 2019-04-10 10:08:56 -0500 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
Andrew Geissler | a80a3af | 2019-02-04 14:01:49 -0600 | [diff] [blame] | 3 | #include "associations.hpp" |
Andrew Geissler | 3b025e6 | 2019-02-01 10:33:54 -0600 | [diff] [blame] | 4 | #include "processing.hpp" |
Matt Spinler | dd94586 | 2018-09-07 12:41:05 -0500 | [diff] [blame] | 5 | #include "src/argument.hpp" |
Matt Spinler | 35396c1 | 2019-04-05 11:46:57 -0500 | [diff] [blame] | 6 | #include "types.hpp" |
Matt Spinler | dd94586 | 2018-09-07 12:41:05 -0500 | [diff] [blame] | 7 | |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 8 | #include <tinyxml2.h> |
| 9 | |
| 10 | #include <atomic> |
| 11 | #include <boost/algorithm/string/predicate.hpp> |
Ed Tanous | 21c6059 | 2020-08-17 23:43:46 -0700 | [diff] [blame] | 12 | #include <boost/asio/io_context.hpp> |
| 13 | #include <boost/asio/signal_set.hpp> |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 14 | #include <boost/container/flat_map.hpp> |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 15 | #include <chrono> |
| 16 | #include <iomanip> |
| 17 | #include <iostream> |
| 18 | #include <sdbusplus/asio/connection.hpp> |
| 19 | #include <sdbusplus/asio/object_server.hpp> |
| 20 | |
Matt Spinler | e2359fb | 2019-04-05 14:11:33 -0500 | [diff] [blame] | 21 | AssociationMaps associationMaps; |
Matt Spinler | 937a232 | 2019-01-23 13:54:22 -0600 | [diff] [blame] | 22 | |
Andrew Geissler | 82815da | 2019-02-04 12:19:41 -0600 | [diff] [blame] | 23 | static WhiteBlackList service_whitelist; |
| 24 | static WhiteBlackList service_blacklist; |
Matt Spinler | dd94586 | 2018-09-07 12:41:05 -0500 | [diff] [blame] | 25 | |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 26 | /** Exception thrown when a path is not found in the object list. */ |
| 27 | struct NotFoundException final : public sdbusplus::exception_t |
| 28 | { |
| 29 | const char* name() const noexcept override |
| 30 | { |
Patrick Williams | 3735ea2 | 2020-11-11 13:12:48 -0600 | [diff] [blame] | 31 | return "xyz.openbmc_project.Common.Error.ResourceNotFound"; |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 32 | }; |
| 33 | const char* description() const noexcept override |
| 34 | { |
| 35 | return "path or object not found"; |
| 36 | }; |
| 37 | const char* what() const noexcept override |
| 38 | { |
Patrick Williams | 3735ea2 | 2020-11-11 13:12:48 -0600 | [diff] [blame] | 39 | return "xyz.openbmc_project.Common.Error.ResourceNotFound: " |
| 40 | "The resource is not found."; |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 41 | }; |
| 42 | }; |
| 43 | |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 44 | void 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 | |
| 67 | void 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 Spinler | 5eddf44 | 2019-04-10 10:08:56 -0500 | [diff] [blame] | 74 | MAPPER_PATH, "xyz.openbmc_project.ObjectMapper.Private", |
| 75 | "IntrospectionComplete"); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 76 | m.append(process_name); |
| 77 | m.signal_send(); |
| 78 | } |
| 79 | |
| 80 | struct InProgressIntrospect |
| 81 | { |
| 82 | InProgressIntrospect( |
Ed Tanous | 21c6059 | 2020-08-17 23:43:46 -0700 | [diff] [blame] | 83 | sdbusplus::asio::connection* system_bus, boost::asio::io_context& io, |
Matt Spinler | 11401e2 | 2019-04-08 13:13:25 -0500 | [diff] [blame] | 84 | const std::string& process_name, AssociationMaps& am |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 85 | #ifdef DEBUG |
| 86 | , |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 87 | std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>> |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 88 | global_start_time |
| 89 | #endif |
| 90 | ) : |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 91 | system_bus(system_bus), |
Matt Spinler | 11401e2 | 2019-04-08 13:13:25 -0500 | [diff] [blame] | 92 | io(io), process_name(process_name), assocMaps(am) |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 93 | #ifdef DEBUG |
| 94 | , |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 95 | global_start_time(global_start_time), |
| 96 | process_start_time(std::chrono::steady_clock::now()) |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 97 | #endif |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 98 | { |
| 99 | } |
| 100 | ~InProgressIntrospect() |
| 101 | { |
| 102 | send_introspection_complete_signal(system_bus, process_name); |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 103 | |
| 104 | #ifdef DEBUG |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 105 | 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 Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 118 | #endif |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 119 | } |
| 120 | sdbusplus::asio::connection* system_bus; |
Ed Tanous | 21c6059 | 2020-08-17 23:43:46 -0700 | [diff] [blame] | 121 | boost::asio::io_context& io; |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 122 | std::string process_name; |
Matt Spinler | 11401e2 | 2019-04-08 13:13:25 -0500 | [diff] [blame] | 123 | AssociationMaps& assocMaps; |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 124 | #ifdef DEBUG |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 125 | 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 Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 128 | #endif |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 129 | }; |
| 130 | |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 131 | void do_associations(sdbusplus::asio::connection* system_bus, |
Matt Spinler | e0b0e3a | 2019-04-08 10:39:23 -0500 | [diff] [blame] | 132 | interface_map_type& interfaceMap, |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 133 | sdbusplus::asio::object_server& objectServer, |
Adrian Ambrożewicz | bb40bd3 | 2021-02-12 13:36:26 +0100 | [diff] [blame] | 134 | const std::string& processName, const std::string& path, |
| 135 | int timeoutRetries = 0) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 136 | { |
Adrian Ambrożewicz | bb40bd3 | 2021-02-12 13:36:26 +0100 | [diff] [blame] | 137 | constexpr int maxTimeoutRetries = 3; |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 138 | system_bus->async_method_call( |
Adrian Ambrożewicz | bb40bd3 | 2021-02-12 13:36:26 +0100 | [diff] [blame] | 139 | [&objectServer, path, processName, &interfaceMap, system_bus, |
| 140 | timeoutRetries]( |
Matt Spinler | 937a232 | 2019-01-23 13:54:22 -0600 | [diff] [blame] | 141 | const boost::system::error_code ec, |
Patrick Williams | 2bb2d6b | 2020-05-13 17:59:02 -0500 | [diff] [blame] | 142 | const std::variant<std::vector<Association>>& variantAssociations) { |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 143 | if (ec) |
| 144 | { |
Adrian Ambrożewicz | bb40bd3 | 2021-02-12 13:36:26 +0100 | [diff] [blame] | 145 | 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 Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 152 | std::cerr << "Error getting associations from " << path << "\n"; |
| 153 | } |
| 154 | std::vector<Association> associations = |
Patrick Williams | b05bc12 | 2020-05-13 12:21:00 -0500 | [diff] [blame] | 155 | std::get<std::vector<Association>>(variantAssociations); |
Andrew Geissler | 4511b33 | 2019-02-21 15:40:40 -0600 | [diff] [blame] | 156 | associationChanged(objectServer, associations, path, processName, |
Matt Spinler | e0b0e3a | 2019-04-08 10:39:23 -0500 | [diff] [blame] | 157 | interfaceMap, associationMaps); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 158 | }, |
| 159 | processName, path, "org.freedesktop.DBus.Properties", "Get", |
John Wang | d0cf942 | 2019-09-17 16:01:34 +0800 | [diff] [blame] | 160 | assocDefsInterface, assocDefsProperty); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void 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 Mauery | c52be0d | 2020-01-14 11:14:25 -0800 | [diff] [blame] | 167 | std::string path, int timeoutRetries = 0) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 168 | { |
Vernon Mauery | c52be0d | 2020-01-14 11:14:25 -0800 | [diff] [blame] | 169 | constexpr int maxTimeoutRetries = 3; |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 170 | system_bus->async_method_call( |
Vernon Mauery | c52be0d | 2020-01-14 11:14:25 -0800 | [diff] [blame] | 171 | [&interface_map, &objectServer, transaction, path, system_bus, |
| 172 | timeoutRetries](const boost::system::error_code ec, |
| 173 | const std::string& introspect_xml) { |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 174 | if (ec) |
| 175 | { |
Vernon Mauery | c52be0d | 2020-01-14 11:14:25 -0800 | [diff] [blame] | 176 | 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 Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 183 | 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 Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 206 | 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 Tanous | d4dd96a | 2018-11-12 11:37:44 -0800 | [diff] [blame] | 216 | thisPathMap[transaction->process_name].emplace(iface_name); |
| 217 | |
John Wang | d0cf942 | 2019-09-17 16:01:34 +0800 | [diff] [blame] | 218 | if (std::strcmp(iface_name, assocDefsInterface) == 0) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 219 | { |
Matt Spinler | e0b0e3a | 2019-04-08 10:39:23 -0500 | [diff] [blame] | 220 | do_associations(system_bus, interface_map, objectServer, |
John Wang | d0cf942 | 2019-09-17 16:01:34 +0800 | [diff] [blame] | 221 | transaction->process_name, path); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 222 | } |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 223 | |
| 224 | pElement = pElement->NextSiblingElement("interface"); |
| 225 | } |
| 226 | |
Matt Spinler | 11401e2 | 2019-04-08 13:13:25 -0500 | [diff] [blame] | 227 | // 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 Tanous | 50232cd | 2018-11-12 11:34:43 -0800 | [diff] [blame] | 232 | pElement = pRoot->FirstChildElement("node"); |
| 233 | while (pElement != nullptr) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 234 | { |
Ed Tanous | 50232cd | 2018-11-12 11:34:43 -0800 | [diff] [blame] | 235 | const char* child_path = pElement->Attribute("name"); |
| 236 | if (child_path != nullptr) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 237 | { |
Ed Tanous | 50232cd | 2018-11-12 11:34:43 -0800 | [diff] [blame] | 238 | std::string parent_path(path); |
| 239 | if (parent_path == "/") |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 240 | { |
Ed Tanous | 50232cd | 2018-11-12 11:34:43 -0800 | [diff] [blame] | 241 | parent_path.clear(); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 242 | } |
Ed Tanous | 50232cd | 2018-11-12 11:34:43 -0800 | [diff] [blame] | 243 | |
| 244 | do_introspect(system_bus, transaction, interface_map, |
| 245 | objectServer, parent_path + "/" + child_path); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 246 | } |
Ed Tanous | 50232cd | 2018-11-12 11:34:43 -0800 | [diff] [blame] | 247 | pElement = pElement->NextSiblingElement("node"); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 248 | } |
| 249 | }, |
| 250 | transaction->process_name, path, "org.freedesktop.DBus.Introspectable", |
| 251 | "Introspect"); |
| 252 | } |
| 253 | |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 254 | void start_new_introspect( |
Ed Tanous | 21c6059 | 2020-08-17 23:43:46 -0700 | [diff] [blame] | 255 | sdbusplus::asio::connection* system_bus, boost::asio::io_context& io, |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 256 | interface_map_type& interface_map, const std::string& process_name, |
Matt Spinler | 11401e2 | 2019-04-08 13:13:25 -0500 | [diff] [blame] | 257 | AssociationMaps& assocMaps, |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 258 | #ifdef DEBUG |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 259 | std::shared_ptr<std::chrono::time_point<std::chrono::steady_clock>> |
| 260 | global_start_time, |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 261 | #endif |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 262 | sdbusplus::asio::object_server& objectServer) |
| 263 | { |
Andrew Geissler | 82815da | 2019-02-04 12:19:41 -0600 | [diff] [blame] | 264 | if (needToIntrospect(process_name, service_whitelist, service_blacklist)) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 265 | { |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 266 | std::shared_ptr<InProgressIntrospect> transaction = |
Matt Spinler | 11401e2 | 2019-04-08 13:13:25 -0500 | [diff] [blame] | 267 | std::make_shared<InProgressIntrospect>(system_bus, io, process_name, |
| 268 | assocMaps |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 269 | #ifdef DEBUG |
| 270 | , |
| 271 | global_start_time |
| 272 | #endif |
| 273 | ); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 274 | |
| 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 |
| 281 | template <class InputIt1, class InputIt2> |
| 282 | bool 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 | |
| 301 | void doListNames( |
Ed Tanous | 21c6059 | 2020-08-17 23:43:46 -0700 | [diff] [blame] | 302 | boost::asio::io_context& io, interface_map_type& interface_map, |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 303 | sdbusplus::asio::connection* system_bus, |
| 304 | boost::container::flat_map<std::string, std::string>& name_owners, |
Matt Spinler | 11401e2 | 2019-04-08 13:13:25 -0500 | [diff] [blame] | 305 | AssociationMaps& assocMaps, sdbusplus::asio::object_server& objectServer) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 306 | { |
| 307 | system_bus->async_method_call( |
Matt Spinler | 11401e2 | 2019-04-08 13:13:25 -0500 | [diff] [blame] | 308 | [&io, &interface_map, &name_owners, &objectServer, system_bus, |
| 309 | &assocMaps](const boost::system::error_code ec, |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 310 | 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 Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 317 | // Try to make startup consistent |
| 318 | std::sort(process_names.begin(), process_names.end()); |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 319 | #ifdef DEBUG |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 320 | 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 Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 324 | #endif |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 325 | for (const std::string& process_name : process_names) |
| 326 | { |
Andrew Geissler | 82815da | 2019-02-04 12:19:41 -0600 | [diff] [blame] | 327 | if (needToIntrospect(process_name, service_whitelist, |
| 328 | service_blacklist)) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 329 | { |
| 330 | start_new_introspect(system_bus, io, interface_map, |
Matt Spinler | 11401e2 | 2019-04-08 13:13:25 -0500 | [diff] [blame] | 331 | process_name, assocMaps, |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 332 | #ifdef DEBUG |
| 333 | global_start_time, |
| 334 | #endif |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 335 | 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 Spinler | dd94586 | 2018-09-07 12:41:05 -0500 | [diff] [blame] | 344 | void 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 Spinler | 47c0975 | 2018-11-29 14:54:13 -0600 | [diff] [blame] | 362 | void addObjectMapResult( |
| 363 | std::vector<interface_map_type::value_type>& objectMap, |
Matt Spinler | 9f0958e | 2018-09-11 08:26:10 -0500 | [diff] [blame] | 364 | 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 Spinler | 47c0975 | 2018-11-29 14:54:13 -0600 | [diff] [blame] | 369 | // the results of GetSubTree and GetAncestors. |
Matt Spinler | 9f0958e | 2018-09-11 08:26:10 -0500 | [diff] [blame] | 370 | // 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 Spinler | 47c0975 | 2018-11-29 14:54:13 -0600 | [diff] [blame] | 374 | objectMap.begin(), objectMap.end(), |
Matt Spinler | 9f0958e | 2018-09-11 08:26:10 -0500 | [diff] [blame] | 375 | [&objectPath](const auto& i) { return objectPath == i.first; }); |
| 376 | |
Matt Spinler | 47c0975 | 2018-11-29 14:54:13 -0600 | [diff] [blame] | 377 | if (entry != objectMap.end()) |
Matt Spinler | 9f0958e | 2018-09-11 08:26:10 -0500 | [diff] [blame] | 378 | { |
| 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 Spinler | 47c0975 | 2018-11-29 14:54:13 -0600 | [diff] [blame] | 386 | objectMap.push_back(object); |
Matt Spinler | 9f0958e | 2018-09-11 08:26:10 -0500 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | |
Matt Spinler | a82779f | 2019-01-09 12:39:42 -0600 | [diff] [blame] | 390 | // 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 |
| 395 | void 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 Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 452 | int main(int argc, char** argv) |
| 453 | { |
Matt Spinler | dd94586 | 2018-09-07 12:41:05 -0500 | [diff] [blame] | 454 | auto options = ArgumentParser(argc, argv); |
Ed Tanous | 21c6059 | 2020-08-17 23:43:46 -0700 | [diff] [blame] | 455 | boost::asio::io_context io; |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 456 | std::shared_ptr<sdbusplus::asio::connection> system_bus = |
| 457 | std::make_shared<sdbusplus::asio::connection>(io); |
| 458 | |
Matt Spinler | dd94586 | 2018-09-07 12:41:05 -0500 | [diff] [blame] | 459 | splitArgs(options["service-namespaces"], service_whitelist); |
Matt Spinler | dd94586 | 2018-09-07 12:41:05 -0500 | [diff] [blame] | 460 | splitArgs(options["service-blacklists"], service_blacklist); |
| 461 | |
Ed Tanous | d4dd96a | 2018-11-12 11:37:44 -0800 | [diff] [blame] | 462 | // 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 Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 467 | 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 Bishop | 2d41d6a | 2021-08-03 08:14:45 -0400 | [diff] [blame] | 471 | signals.async_wait( |
| 472 | [&io](const boost::system::error_code&, int) { io.stop(); }); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 473 | |
| 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 Geissler | 2067926 | 2019-02-11 20:20:40 -0600 | [diff] [blame] | 480 | std::string name; // well-known |
| 481 | std::string old_owner; // unique-name |
| 482 | std::string new_owner; // unique-name |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 483 | |
| 484 | message.read(name, old_owner, new_owner); |
| 485 | |
| 486 | if (!old_owner.empty()) |
| 487 | { |
Andrew Geissler | 2067926 | 2019-02-11 20:20:40 -0600 | [diff] [blame] | 488 | processNameChangeDelete(name_owners, name, old_owner, |
Matt Spinler | e2359fb | 2019-04-05 14:11:33 -0500 | [diff] [blame] | 489 | interface_map, associationMaps, server); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | if (!new_owner.empty()) |
| 493 | { |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 494 | #ifdef DEBUG |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 495 | auto transaction = std::make_shared< |
| 496 | std::chrono::time_point<std::chrono::steady_clock>>( |
| 497 | std::chrono::steady_clock::now()); |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 498 | #endif |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 499 | // New daemon added |
Andrew Geissler | 82815da | 2019-02-04 12:19:41 -0600 | [diff] [blame] | 500 | if (needToIntrospect(name, service_whitelist, |
| 501 | service_blacklist)) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 502 | { |
| 503 | name_owners[new_owner] = name; |
| 504 | start_new_introspect(system_bus.get(), io, interface_map, |
Matt Spinler | 11401e2 | 2019-04-08 13:13:25 -0500 | [diff] [blame] | 505 | name, associationMaps, |
Matt Spinler | aecabe8 | 2018-09-19 13:25:42 -0500 | [diff] [blame] | 506 | #ifdef DEBUG |
| 507 | transaction, |
| 508 | #endif |
| 509 | server); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 510 | } |
| 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 Geissler | 7046189 | 2019-02-27 09:57:37 -0600 | [diff] [blame] | 522 | InterfacesAdded interfaces_added; |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 523 | message.read(obj_path, interfaces_added); |
| 524 | std::string well_known; |
Andrew Geissler | 3b025e6 | 2019-02-01 10:33:54 -0600 | [diff] [blame] | 525 | if (!getWellKnown(name_owners, message.get_sender(), well_known)) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 526 | { |
| 527 | return; // only introspect well-known |
| 528 | } |
Andrew Geissler | 82815da | 2019-02-04 12:19:41 -0600 | [diff] [blame] | 529 | if (needToIntrospect(well_known, service_whitelist, |
| 530 | service_blacklist)) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 531 | { |
Andrew Geissler | 7046189 | 2019-02-27 09:57:37 -0600 | [diff] [blame] | 532 | processInterfaceAdded(interface_map, obj_path, interfaces_added, |
Matt Spinler | e2359fb | 2019-04-05 14:11:33 -0500 | [diff] [blame] | 533 | well_known, associationMaps, server); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 534 | } |
| 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 Geissler | 3b025e6 | 2019-02-01 10:33:54 -0600 | [diff] [blame] | 555 | if (!getWellKnown(name_owners, message.get_sender(), sender)) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 556 | { |
| 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 Wang | d0cf942 | 2019-09-17 16:01:34 +0800 | [diff] [blame] | 567 | if (interface == assocDefsInterface) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 568 | { |
Andrew Geissler | 67e5a42 | 2019-02-04 13:00:59 -0600 | [diff] [blame] | 569 | removeAssociation(obj_path.str, sender, server, |
Matt Spinler | e2359fb | 2019-04-05 14:11:33 -0500 | [diff] [blame] | 570 | associationMaps); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | interface_set->second.erase(interface); |
Matt Spinler | 9c3d285 | 2019-04-08 15:57:19 -0500 | [diff] [blame] | 574 | |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 575 | if (interface_set->second.empty()) |
| 576 | { |
Matt Spinler | 9c3d285 | 2019-04-08 15:57:19 -0500 | [diff] [blame] | 577 | // If this was the last interface on this connection, |
| 578 | // erase the connection |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 579 | connection_map->second.erase(interface_set); |
Matt Spinler | 9c3d285 | 2019-04-08 15:57:19 -0500 | [diff] [blame] | 580 | |
| 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 Spinler | 5eddf44 | 2019-04-10 10:08:56 -0500 | [diff] [blame] | 588 | MAPPER_BUSNAME)) |
Matt Spinler | 9c3d285 | 2019-04-08 15:57:19 -0500 | [diff] [blame] | 589 | { |
| 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 Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 595 | } |
| 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 Spinler | a82779f | 2019-01-09 12:39:42 -0600 | [diff] [blame] | 603 | |
| 604 | removeUnneededParents(obj_path.str, sender, interface_map); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 605 | }; |
| 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 Spinler | e0b0e3a | 2019-04-08 10:39:23 -0500 | [diff] [blame] | 613 | associationChangedHandler = [&server, &name_owners, &interface_map]( |
Matt Spinler | 8f876a5 | 2019-04-15 13:22:50 -0500 | [diff] [blame] | 614 | sdbusplus::message::message& message) { |
| 615 | std::string objectName; |
Patrick Williams | 2bb2d6b | 2020-05-13 17:59:02 -0500 | [diff] [blame] | 616 | boost::container::flat_map<std::string, |
| 617 | std::variant<std::vector<Association>>> |
Matt Spinler | 8f876a5 | 2019-04-15 13:22:50 -0500 | [diff] [blame] | 618 | values; |
| 619 | message.read(objectName, values); |
John Wang | d0cf942 | 2019-09-17 16:01:34 +0800 | [diff] [blame] | 620 | auto prop = values.find(assocDefsProperty); |
Matt Spinler | 8f876a5 | 2019-04-15 13:22:50 -0500 | [diff] [blame] | 621 | if (prop != values.end()) |
| 622 | { |
| 623 | std::vector<Association> associations = |
Patrick Williams | b05bc12 | 2020-05-13 12:21:00 -0500 | [diff] [blame] | 624 | std::get<std::vector<Association>>(prop->second); |
Matt Spinler | 8f876a5 | 2019-04-15 13:22:50 -0500 | [diff] [blame] | 625 | |
| 626 | std::string well_known; |
| 627 | if (!getWellKnown(name_owners, message.get_sender(), |
| 628 | well_known)) |
| 629 | { |
| 630 | return; |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 631 | } |
Matt Spinler | 8f876a5 | 2019-04-15 13:22:50 -0500 | [diff] [blame] | 632 | associationChanged(server, associations, message.get_path(), |
Matt Spinler | e0b0e3a | 2019-04-08 10:39:23 -0500 | [diff] [blame] | 633 | well_known, interface_map, associationMaps); |
Matt Spinler | 8f876a5 | 2019-04-15 13:22:50 -0500 | [diff] [blame] | 634 | } |
| 635 | }; |
| 636 | sdbusplus::bus::match::match assocChangedMatch( |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 637 | 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 Spinler | 8f876a5 | 2019-04-15 13:22:50 -0500 | [diff] [blame] | 641 | sdbusplus::bus::match::rules::argN(0, assocDefsInterface), |
| 642 | associationChangedHandler); |
| 643 | |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 644 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface = |
Matt Spinler | 5eddf44 | 2019-04-10 10:08:56 -0500 | [diff] [blame] | 645 | server.add_interface(MAPPER_PATH, MAPPER_INTERFACE); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 646 | |
| 647 | iface->register_method( |
Matt Spinler | 6a39e8c | 2018-11-13 11:11:36 -0600 | [diff] [blame] | 648 | "GetAncestors", [&interface_map](std::string& req_path, |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 649 | std::vector<std::string>& interfaces) { |
| 650 | // Interfaces need to be sorted for intersect to function |
| 651 | std::sort(interfaces.begin(), interfaces.end()); |
| 652 | |
James Feist | d732287 | 2019-01-28 11:25:00 -0800 | [diff] [blame] | 653 | if (boost::ends_with(req_path, "/")) |
Matt Spinler | 6a39e8c | 2018-11-13 11:11:36 -0600 | [diff] [blame] | 654 | { |
James Feist | d732287 | 2019-01-28 11:25:00 -0800 | [diff] [blame] | 655 | req_path.pop_back(); |
| 656 | } |
| 657 | if (req_path.size() && |
| 658 | interface_map.find(req_path) == interface_map.end()) |
| 659 | { |
| 660 | throw NotFoundException(); |
Matt Spinler | 6a39e8c | 2018-11-13 11:11:36 -0600 | [diff] [blame] | 661 | } |
| 662 | |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 663 | std::vector<interface_map_type::value_type> ret; |
| 664 | for (auto& object_path : interface_map) |
| 665 | { |
| 666 | auto& this_path = object_path.first; |
Matt Spinler | ea6516c | 2018-09-25 16:23:13 -0500 | [diff] [blame] | 667 | if (boost::starts_with(req_path, this_path) && |
| 668 | (req_path != this_path)) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 669 | { |
| 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 Spinler | 47c0975 | 2018-11-29 14:54:13 -0600 | [diff] [blame] | 683 | addObjectMapResult(ret, this_path, |
| 684 | interface_map); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 685 | } |
| 686 | } |
| 687 | } |
| 688 | } |
| 689 | } |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 690 | |
| 691 | return ret; |
| 692 | }); |
| 693 | |
| 694 | iface->register_method( |
| 695 | "GetObject", [&interface_map](const std::string& path, |
| 696 | std::vector<std::string>& interfaces) { |
Matt Spinler | 7a38d41 | 2018-09-18 16:13:25 -0500 | [diff] [blame] | 697 | boost::container::flat_map<std::string, |
| 698 | boost::container::flat_set<std::string>> |
| 699 | results; |
| 700 | |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 701 | // 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 Spinler | 7a38d41 | 2018-09-18 16:13:25 -0500 | [diff] [blame] | 718 | results.emplace(interface_map.first, interface_map.second); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 719 | } |
| 720 | } |
Matt Spinler | 7a38d41 | 2018-09-18 16:13:25 -0500 | [diff] [blame] | 721 | |
| 722 | if (results.empty()) |
| 723 | { |
| 724 | throw NotFoundException(); |
| 725 | } |
| 726 | |
| 727 | return results; |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 728 | }); |
| 729 | |
| 730 | iface->register_method( |
Matt Spinler | 153494e | 2018-11-07 16:35:40 -0600 | [diff] [blame] | 731 | "GetSubTree", [&interface_map](std::string& req_path, int32_t depth, |
| 732 | std::vector<std::string>& interfaces) { |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 733 | 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 Feist | d732287 | 2019-01-28 11:25:00 -0800 | [diff] [blame] | 741 | if (boost::ends_with(req_path, "/")) |
Matt Spinler | 153494e | 2018-11-07 16:35:40 -0600 | [diff] [blame] | 742 | { |
James Feist | d732287 | 2019-01-28 11:25:00 -0800 | [diff] [blame] | 743 | req_path.pop_back(); |
| 744 | } |
| 745 | if (req_path.size() && |
| 746 | interface_map.find(req_path) == interface_map.end()) |
| 747 | { |
| 748 | throw NotFoundException(); |
Matt Spinler | 153494e | 2018-11-07 16:35:40 -0600 | [diff] [blame] | 749 | } |
| 750 | |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 751 | for (auto& object_path : interface_map) |
| 752 | { |
| 753 | auto& this_path = object_path.first; |
Matt Spinler | 153494e | 2018-11-07 16:35:40 -0600 | [diff] [blame] | 754 | |
| 755 | if (this_path == req_path) |
| 756 | { |
| 757 | continue; |
| 758 | } |
| 759 | |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 760 | 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 Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 768 | for (auto& interface_map : object_path.second) |
| 769 | { |
| 770 | if (intersect(interfaces.begin(), interfaces.end(), |
| 771 | interface_map.second.begin(), |
Matt Spinler | 9f0958e | 2018-09-11 08:26:10 -0500 | [diff] [blame] | 772 | interface_map.second.end()) || |
| 773 | interfaces.empty()) |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 774 | { |
Matt Spinler | 47c0975 | 2018-11-29 14:54:13 -0600 | [diff] [blame] | 775 | addObjectMapResult(ret, this_path, |
| 776 | interface_map); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 777 | } |
| 778 | } |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 779 | } |
| 780 | } |
| 781 | } |
Matt Spinler | 6a39e8c | 2018-11-13 11:11:36 -0600 | [diff] [blame] | 782 | |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 783 | return ret; |
| 784 | }); |
| 785 | |
| 786 | iface->register_method( |
| 787 | "GetSubTreePaths", |
Matt Spinler | 153494e | 2018-11-07 16:35:40 -0600 | [diff] [blame] | 788 | [&interface_map](std::string& req_path, int32_t depth, |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 789 | 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 Spinler | 153494e | 2018-11-07 16:35:40 -0600 | [diff] [blame] | 797 | |
James Feist | d732287 | 2019-01-28 11:25:00 -0800 | [diff] [blame] | 798 | if (boost::ends_with(req_path, "/")) |
Matt Spinler | 153494e | 2018-11-07 16:35:40 -0600 | [diff] [blame] | 799 | { |
James Feist | d732287 | 2019-01-28 11:25:00 -0800 | [diff] [blame] | 800 | req_path.pop_back(); |
| 801 | } |
| 802 | if (req_path.size() && |
| 803 | interface_map.find(req_path) == interface_map.end()) |
| 804 | { |
| 805 | throw NotFoundException(); |
Matt Spinler | 153494e | 2018-11-07 16:35:40 -0600 | [diff] [blame] | 806 | } |
| 807 | |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 808 | for (auto& object_path : interface_map) |
| 809 | { |
| 810 | auto& this_path = object_path.first; |
Matt Spinler | 153494e | 2018-11-07 16:35:40 -0600 | [diff] [blame] | 811 | |
| 812 | if (this_path == req_path) |
| 813 | { |
| 814 | continue; |
| 815 | } |
| 816 | |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 817 | 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 Spinler | 6a39e8c | 2018-11-13 11:11:36 -0600 | [diff] [blame] | 844 | |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 845 | return ret; |
| 846 | }); |
| 847 | |
| 848 | iface->initialize(); |
| 849 | |
| 850 | io.post([&]() { |
Matt Spinler | 11401e2 | 2019-04-08 13:13:25 -0500 | [diff] [blame] | 851 | doListNames(io, interface_map, system_bus.get(), name_owners, |
| 852 | associationMaps, server); |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 853 | }); |
| 854 | |
Vishwanatha Subbanna | 64354ef | 2020-08-21 03:35:26 -0500 | [diff] [blame] | 855 | system_bus->request_name(MAPPER_BUSNAME); |
| 856 | |
Ed Tanous | 6052063 | 2018-06-11 17:46:52 -0700 | [diff] [blame] | 857 | io.run(); |
| 858 | } |