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