Andrew Geissler | 3b025e6 | 2019-02-01 10:33:54 -0600 | [diff] [blame] | 1 | #include "processing.hpp" |
| 2 | |
| 3 | #include <boost/algorithm/string/predicate.hpp> |
Brad Bishop | 2352088 | 2022-05-26 21:39:53 -0400 | [diff] [blame] | 4 | |
Andrew Geissler | 7046189 | 2019-02-27 09:57:37 -0600 | [diff] [blame] | 5 | #include <iostream> |
Andrew Geissler | 3b025e6 | 2019-02-01 10:33:54 -0600 | [diff] [blame] | 6 | |
| 7 | bool getWellKnown( |
| 8 | const boost::container::flat_map<std::string, std::string>& owners, |
| 9 | const std::string& request, std::string& wellKnown) |
| 10 | { |
| 11 | // If it's already a well known name, just return |
| 12 | if (!boost::starts_with(request, ":")) |
| 13 | { |
| 14 | wellKnown = request; |
| 15 | return true; |
| 16 | } |
| 17 | |
| 18 | auto it = owners.find(request); |
| 19 | if (it == owners.end()) |
| 20 | { |
| 21 | return false; |
| 22 | } |
| 23 | wellKnown = it->second; |
| 24 | return true; |
| 25 | } |
Andrew Geissler | 82815da | 2019-02-04 12:19:41 -0600 | [diff] [blame] | 26 | |
| 27 | bool needToIntrospect(const std::string& processName, |
Brad Bishop | d554232 | 2022-06-02 19:56:23 -0400 | [diff] [blame] | 28 | const AllowDenyList& allowList) |
Andrew Geissler | 82815da | 2019-02-04 12:19:41 -0600 | [diff] [blame] | 29 | { |
Brad Bishop | f944a45 | 2022-05-05 15:06:46 -0400 | [diff] [blame] | 30 | auto inAllowList = |
| 31 | std::find_if(allowList.begin(), allowList.end(), |
Andrew Geissler | 82815da | 2019-02-04 12:19:41 -0600 | [diff] [blame] | 32 | [&processName](const auto& prefix) { |
| 33 | return boost::starts_with(processName, prefix); |
Brad Bishop | f944a45 | 2022-05-05 15:06:46 -0400 | [diff] [blame] | 34 | }) != allowList.end(); |
Andrew Geissler | 82815da | 2019-02-04 12:19:41 -0600 | [diff] [blame] | 35 | |
Brad Bishop | d554232 | 2022-06-02 19:56:23 -0400 | [diff] [blame] | 36 | return inAllowList; |
Andrew Geissler | 82815da | 2019-02-04 12:19:41 -0600 | [diff] [blame] | 37 | } |
Andrew Geissler | 2067926 | 2019-02-11 20:20:40 -0600 | [diff] [blame] | 38 | |
| 39 | void processNameChangeDelete( |
| 40 | boost::container::flat_map<std::string, std::string>& nameOwners, |
| 41 | const std::string& wellKnown, const std::string& oldOwner, |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 42 | InterfaceMapType& interfaceMap, AssociationMaps& assocMaps, |
Andrew Geissler | 2067926 | 2019-02-11 20:20:40 -0600 | [diff] [blame] | 43 | sdbusplus::asio::object_server& server) |
| 44 | { |
| 45 | if (boost::starts_with(oldOwner, ":")) |
| 46 | { |
| 47 | auto it = nameOwners.find(oldOwner); |
| 48 | if (it != nameOwners.end()) |
| 49 | { |
| 50 | nameOwners.erase(it); |
| 51 | } |
| 52 | } |
| 53 | // Connection removed |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 54 | InterfaceMapType::iterator pathIt = interfaceMap.begin(); |
Andrew Geissler | 2067926 | 2019-02-11 20:20:40 -0600 | [diff] [blame] | 55 | while (pathIt != interfaceMap.end()) |
| 56 | { |
| 57 | // If an associations interface is being removed, |
| 58 | // also need to remove the corresponding associations |
| 59 | // objects and properties. |
| 60 | auto ifaces = pathIt->second.find(wellKnown); |
| 61 | if (ifaces != pathIt->second.end()) |
| 62 | { |
John Wang | d0cf942 | 2019-09-17 16:01:34 +0800 | [diff] [blame] | 63 | auto assoc = std::find(ifaces->second.begin(), ifaces->second.end(), |
| 64 | assocDefsInterface); |
Andrew Geissler | 2067926 | 2019-02-11 20:20:40 -0600 | [diff] [blame] | 65 | if (assoc != ifaces->second.end()) |
| 66 | { |
Matt Spinler | e2359fb | 2019-04-05 14:11:33 -0500 | [diff] [blame] | 67 | removeAssociation(pathIt->first, wellKnown, server, assocMaps); |
Andrew Geissler | 2067926 | 2019-02-11 20:20:40 -0600 | [diff] [blame] | 68 | } |
Matt Spinler | 9c3d285 | 2019-04-08 15:57:19 -0500 | [diff] [blame] | 69 | |
| 70 | // Instead of checking if every single path is the endpoint of an |
| 71 | // association that needs to be moved to pending, only check when |
| 72 | // we own this path as well, which would be because of an |
| 73 | // association. |
| 74 | if ((pathIt->second.size() == 2) && |
Brad Bishop | a02cd54 | 2021-10-12 19:12:42 -0400 | [diff] [blame] | 75 | (pathIt->second.find("xyz.openbmc_project.ObjectMapper") != |
| 76 | pathIt->second.end())) |
Matt Spinler | 9c3d285 | 2019-04-08 15:57:19 -0500 | [diff] [blame] | 77 | { |
| 78 | // Remove the 2 association D-Bus paths and move the |
| 79 | // association to pending. |
| 80 | moveAssociationToPending(pathIt->first, assocMaps, server); |
| 81 | } |
Andrew Geissler | 2067926 | 2019-02-11 20:20:40 -0600 | [diff] [blame] | 82 | } |
| 83 | pathIt->second.erase(wellKnown); |
| 84 | if (pathIt->second.empty()) |
| 85 | { |
| 86 | // If the last connection to the object is gone, |
| 87 | // delete the top level object |
| 88 | pathIt = interfaceMap.erase(pathIt); |
| 89 | continue; |
| 90 | } |
| 91 | pathIt++; |
| 92 | } |
| 93 | } |
Andrew Geissler | 7046189 | 2019-02-27 09:57:37 -0600 | [diff] [blame] | 94 | |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 95 | void processInterfaceAdded(InterfaceMapType& interfaceMap, |
Andrew Geissler | 7046189 | 2019-02-27 09:57:37 -0600 | [diff] [blame] | 96 | const sdbusplus::message::object_path& objPath, |
| 97 | const InterfacesAdded& intfAdded, |
| 98 | const std::string& wellKnown, |
Matt Spinler | e2359fb | 2019-04-05 14:11:33 -0500 | [diff] [blame] | 99 | AssociationMaps& assocMaps, |
Andrew Geissler | 7046189 | 2019-02-27 09:57:37 -0600 | [diff] [blame] | 100 | sdbusplus::asio::object_server& server) |
| 101 | { |
| 102 | auto& ifaceList = interfaceMap[objPath.str]; |
| 103 | |
| 104 | for (const auto& interfacePair : intfAdded) |
| 105 | { |
| 106 | ifaceList[wellKnown].emplace(interfacePair.first); |
| 107 | |
John Wang | d0cf942 | 2019-09-17 16:01:34 +0800 | [diff] [blame] | 108 | if (interfacePair.first == assocDefsInterface) |
Andrew Geissler | 7046189 | 2019-02-27 09:57:37 -0600 | [diff] [blame] | 109 | { |
Patrick Williams | 2bb2d6b | 2020-05-13 17:59:02 -0500 | [diff] [blame] | 110 | const std::variant<std::vector<Association>>* variantAssociations = |
| 111 | nullptr; |
Andrew Geissler | 7046189 | 2019-02-27 09:57:37 -0600 | [diff] [blame] | 112 | for (const auto& interface : interfacePair.second) |
| 113 | { |
John Wang | d0cf942 | 2019-09-17 16:01:34 +0800 | [diff] [blame] | 114 | if (interface.first == assocDefsProperty) |
Andrew Geissler | 7046189 | 2019-02-27 09:57:37 -0600 | [diff] [blame] | 115 | { |
| 116 | variantAssociations = &(interface.second); |
| 117 | } |
| 118 | } |
| 119 | if (variantAssociations == nullptr) |
| 120 | { |
| 121 | std::cerr << "Illegal association found on " << wellKnown |
| 122 | << "\n"; |
| 123 | continue; |
| 124 | } |
| 125 | std::vector<Association> associations = |
Patrick Williams | b05bc12 | 2020-05-13 12:21:00 -0500 | [diff] [blame] | 126 | std::get<std::vector<Association>>(*variantAssociations); |
Andrew Geissler | 7046189 | 2019-02-27 09:57:37 -0600 | [diff] [blame] | 127 | associationChanged(server, associations, objPath.str, wellKnown, |
Matt Spinler | e0b0e3a | 2019-04-08 10:39:23 -0500 | [diff] [blame] | 128 | interfaceMap, assocMaps); |
Andrew Geissler | 7046189 | 2019-02-27 09:57:37 -0600 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
| 132 | // To handle the case where an object path is being created |
| 133 | // with 2 or more new path segments, check if the parent paths |
| 134 | // of this path are already in the interface map, and add them |
| 135 | // if they aren't with just the default freedesktop interfaces. |
| 136 | // This would be done via introspection if they would have |
| 137 | // already existed at startup. While we could also introspect |
| 138 | // them now to do the work, we know there aren't any other |
| 139 | // interfaces or we would have gotten signals for them as well, |
| 140 | // so take a shortcut to speed things up. |
| 141 | // |
| 142 | // This is all needed so that mapper operations can be done |
| 143 | // on the new parent paths. |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 144 | using iface_map_iterator = InterfaceMapType::iterator; |
Ed Tanous | 964681c | 2022-07-08 12:47:24 -0700 | [diff] [blame^] | 145 | using name_map_iterator = InterfaceMapType::mapped_type::iterator; |
Andrew Geissler | 7046189 | 2019-02-27 09:57:37 -0600 | [diff] [blame] | 146 | |
Ed Tanous | 964681c | 2022-07-08 12:47:24 -0700 | [diff] [blame^] | 147 | static const InterfaceNames defaultIfaces{ |
Andrew Geissler | 7046189 | 2019-02-27 09:57:37 -0600 | [diff] [blame] | 148 | "org.freedesktop.DBus.Introspectable", "org.freedesktop.DBus.Peer", |
| 149 | "org.freedesktop.DBus.Properties"}; |
| 150 | |
| 151 | std::string parent = objPath.str; |
| 152 | auto pos = parent.find_last_of('/'); |
| 153 | |
| 154 | while (pos != std::string::npos) |
| 155 | { |
| 156 | parent = parent.substr(0, pos); |
| 157 | |
| 158 | std::pair<iface_map_iterator, bool> parentEntry = |
Ed Tanous | 964681c | 2022-07-08 12:47:24 -0700 | [diff] [blame^] | 159 | interfaceMap.try_emplace(parent); |
Andrew Geissler | 7046189 | 2019-02-27 09:57:37 -0600 | [diff] [blame] | 160 | |
| 161 | std::pair<name_map_iterator, bool> ifaceEntry = |
Ed Tanous | 964681c | 2022-07-08 12:47:24 -0700 | [diff] [blame^] | 162 | parentEntry.first->second.try_emplace(wellKnown, defaultIfaces); |
Andrew Geissler | 7046189 | 2019-02-27 09:57:37 -0600 | [diff] [blame] | 163 | |
| 164 | if (!ifaceEntry.second) |
| 165 | { |
| 166 | // Entry was already there for this name so done. |
| 167 | break; |
| 168 | } |
| 169 | |
| 170 | pos = parent.find_last_of('/'); |
| 171 | } |
Matt Spinler | 11401e2 | 2019-04-08 13:13:25 -0500 | [diff] [blame] | 172 | |
| 173 | // The new interface might have an association pending |
| 174 | checkIfPendingAssociation(objPath.str, interfaceMap, assocMaps, server); |
Andrew Geissler | 7046189 | 2019-02-27 09:57:37 -0600 | [diff] [blame] | 175 | } |