Matt Spinler | e0017eb | 2018-03-27 11:17:38 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2018 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Matt Spinler | 52ee71b | 2018-05-23 13:18:55 -0500 | [diff] [blame^] | 16 | #include "callout.hpp" |
Matt Spinler | e0017eb | 2018-03-27 11:17:38 -0500 | [diff] [blame] | 17 | #include "config.h" |
| 18 | #include "manager.hpp" |
Matt Spinler | 4a6ea6a | 2018-03-27 14:25:12 -0500 | [diff] [blame] | 19 | #include "policy_find.hpp" |
Matt Spinler | e0017eb | 2018-03-27 11:17:38 -0500 | [diff] [blame] | 20 | |
| 21 | namespace ibm |
| 22 | { |
| 23 | namespace logging |
| 24 | { |
| 25 | |
Matt Spinler | 52ee71b | 2018-05-23 13:18:55 -0500 | [diff] [blame^] | 26 | namespace fs = std::experimental::filesystem; |
| 27 | |
Matt Spinler | e0017eb | 2018-03-27 11:17:38 -0500 | [diff] [blame] | 28 | Manager::Manager(sdbusplus::bus::bus& bus) : |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 29 | bus(bus), |
| 30 | addMatch(bus, |
| 31 | sdbusplus::bus::match::rules::interfacesAdded() + |
| 32 | sdbusplus::bus::match::rules::path_namespace(LOGGING_PATH), |
| 33 | std::bind(std::mem_fn(&Manager::interfaceAdded), this, |
Matt Spinler | 055da3b | 2018-05-09 15:51:20 -0500 | [diff] [blame] | 34 | std::placeholders::_1)), |
| 35 | removeMatch(bus, |
| 36 | sdbusplus::bus::match::rules::interfacesRemoved() + |
| 37 | sdbusplus::bus::match::rules::path_namespace(LOGGING_PATH), |
| 38 | std::bind(std::mem_fn(&Manager::interfaceRemoved), this, |
| 39 | std::placeholders::_1)) |
Matt Spinler | 743e582 | 2018-03-27 13:44:50 -0500 | [diff] [blame] | 40 | #ifdef USE_POLICY_INTERFACE |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 41 | , |
| 42 | policies(POLICY_JSON_PATH) |
Matt Spinler | 743e582 | 2018-03-27 13:44:50 -0500 | [diff] [blame] | 43 | #endif |
Matt Spinler | e0017eb | 2018-03-27 11:17:38 -0500 | [diff] [blame] | 44 | { |
Matt Spinler | 54bfa7e | 2018-03-27 11:28:59 -0500 | [diff] [blame] | 45 | createAll(); |
| 46 | } |
| 47 | |
| 48 | void Manager::createAll() |
| 49 | { |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 50 | auto objects = getManagedObjects(bus, LOGGING_BUSNAME, LOGGING_PATH); |
Matt Spinler | 54bfa7e | 2018-03-27 11:28:59 -0500 | [diff] [blame] | 51 | |
| 52 | for (const auto& object : objects) |
| 53 | { |
| 54 | const auto& interfaces = object.second; |
| 55 | |
Matt Spinler | e6a5159 | 2018-05-23 12:47:10 -0500 | [diff] [blame] | 56 | auto propertyMap = interfaces.find(LOGGING_IFACE); |
Matt Spinler | 54bfa7e | 2018-03-27 11:28:59 -0500 | [diff] [blame] | 57 | |
| 58 | if (propertyMap != interfaces.end()) |
| 59 | { |
Matt Spinler | e6a5159 | 2018-05-23 12:47:10 -0500 | [diff] [blame] | 60 | createWithRestore(object.first, interfaces); |
Matt Spinler | 54bfa7e | 2018-03-27 11:28:59 -0500 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
Matt Spinler | e6a5159 | 2018-05-23 12:47:10 -0500 | [diff] [blame] | 65 | void Manager::createWithRestore(const std::string& objectPath, |
| 66 | const DbusInterfaceMap& interfaces) |
Matt Spinler | 54bfa7e | 2018-03-27 11:28:59 -0500 | [diff] [blame] | 67 | { |
Matt Spinler | e6a5159 | 2018-05-23 12:47:10 -0500 | [diff] [blame] | 68 | createObject(objectPath, interfaces); |
Matt Spinler | 4a6ea6a | 2018-03-27 14:25:12 -0500 | [diff] [blame] | 69 | |
Matt Spinler | e6a5159 | 2018-05-23 12:47:10 -0500 | [diff] [blame] | 70 | // TODO |
| 71 | // restoreCalloutObjects(objectPath, interfaces); |
| 72 | } |
| 73 | |
| 74 | void Manager::create(const std::string& objectPath, |
| 75 | const DbusInterfaceMap& interfaces) |
| 76 | { |
| 77 | createObject(objectPath, interfaces); |
| 78 | |
Matt Spinler | 52ee71b | 2018-05-23 13:18:55 -0500 | [diff] [blame^] | 79 | createCalloutObjects(objectPath, interfaces); |
Matt Spinler | e6a5159 | 2018-05-23 12:47:10 -0500 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void Manager::createObject(const std::string& objectPath, |
| 83 | const DbusInterfaceMap& interfaces) |
| 84 | { |
Matt Spinler | 4a6ea6a | 2018-03-27 14:25:12 -0500 | [diff] [blame] | 85 | #ifdef USE_POLICY_INTERFACE |
Matt Spinler | e6a5159 | 2018-05-23 12:47:10 -0500 | [diff] [blame] | 86 | auto logInterface = interfaces.find(LOGGING_IFACE); |
| 87 | createPolicyInterface(objectPath, logInterface->second); |
Matt Spinler | 4a6ea6a | 2018-03-27 14:25:12 -0500 | [diff] [blame] | 88 | #endif |
Matt Spinler | e0017eb | 2018-03-27 11:17:38 -0500 | [diff] [blame] | 89 | } |
| 90 | |
Matt Spinler | a139035 | 2018-05-23 10:48:19 -0500 | [diff] [blame] | 91 | void Manager::erase(EntryID id) |
| 92 | { |
Matt Spinler | 52ee71b | 2018-05-23 13:18:55 -0500 | [diff] [blame^] | 93 | fs::remove_all(getSaveDir(id)); |
Matt Spinler | 677143b | 2018-05-23 10:53:27 -0500 | [diff] [blame] | 94 | childEntries.erase(id); |
Matt Spinler | a139035 | 2018-05-23 10:48:19 -0500 | [diff] [blame] | 95 | entries.erase(id); |
| 96 | } |
| 97 | |
Matt Spinler | 491fc6f | 2018-04-23 11:00:52 -0500 | [diff] [blame] | 98 | void Manager::addInterface(const std::string& objectPath, InterfaceType type, |
| 99 | std::experimental::any& object) |
| 100 | { |
| 101 | auto id = getEntryID(objectPath); |
| 102 | auto entry = entries.find(id); |
| 103 | |
| 104 | if (entry == entries.end()) |
| 105 | { |
| 106 | InterfaceMap interfaces; |
| 107 | interfaces.emplace(type, object); |
| 108 | entries.emplace(id, std::move(interfaces)); |
| 109 | } |
| 110 | else |
| 111 | { |
| 112 | entry->second.emplace(type, object); |
| 113 | } |
| 114 | } |
| 115 | |
Matt Spinler | 677143b | 2018-05-23 10:53:27 -0500 | [diff] [blame] | 116 | void Manager::addChildInterface(const std::string& objectPath, |
| 117 | InterfaceType type, |
| 118 | std::experimental::any& object) |
| 119 | { |
| 120 | auto id = getEntryID(objectPath); |
| 121 | auto entry = childEntries.find(id); |
| 122 | |
| 123 | // childEntries is: |
| 124 | // A map of error log entry IDs to: |
| 125 | // a map of interface types to: |
| 126 | // a vector of interface objects |
| 127 | |
| 128 | if (entry == childEntries.end()) |
| 129 | { |
| 130 | ObjectList objects{object}; |
| 131 | InterfaceMapMulti interfaces; |
| 132 | interfaces.emplace(type, std::move(objects)); |
| 133 | childEntries.emplace(id, std::move(interfaces)); |
| 134 | } |
| 135 | else |
| 136 | { |
| 137 | auto i = entry->second.find(type); |
| 138 | if (i == entry->second.end()) |
| 139 | { |
| 140 | ObjectList objects{objects}; |
| 141 | entry->second.emplace(type, objects); |
| 142 | } |
| 143 | else |
| 144 | { |
| 145 | i->second.emplace_back(object); |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
Matt Spinler | 4a6ea6a | 2018-03-27 14:25:12 -0500 | [diff] [blame] | 150 | #ifdef USE_POLICY_INTERFACE |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 151 | void Manager::createPolicyInterface(const std::string& objectPath, |
| 152 | const DbusPropertyMap& properties) |
Matt Spinler | 4a6ea6a | 2018-03-27 14:25:12 -0500 | [diff] [blame] | 153 | { |
| 154 | auto values = policy::find(policies, properties); |
| 155 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 156 | auto object = std::make_shared<PolicyObject>(bus, objectPath.c_str(), true); |
Matt Spinler | 4a6ea6a | 2018-03-27 14:25:12 -0500 | [diff] [blame] | 157 | |
| 158 | object->eventID(std::get<policy::EIDField>(values)); |
| 159 | object->description(std::get<policy::MsgField>(values)); |
| 160 | |
Matt Spinler | 9bea4ea | 2018-05-09 15:53:04 -0500 | [diff] [blame] | 161 | object->emit_object_added(); |
| 162 | |
Matt Spinler | 491fc6f | 2018-04-23 11:00:52 -0500 | [diff] [blame] | 163 | std::experimental::any anyObject = object; |
Matt Spinler | 4a6ea6a | 2018-03-27 14:25:12 -0500 | [diff] [blame] | 164 | |
Matt Spinler | 491fc6f | 2018-04-23 11:00:52 -0500 | [diff] [blame] | 165 | addInterface(objectPath, InterfaceType::POLICY, anyObject); |
Matt Spinler | 4a6ea6a | 2018-03-27 14:25:12 -0500 | [diff] [blame] | 166 | } |
| 167 | #endif |
| 168 | |
Matt Spinler | 52ee71b | 2018-05-23 13:18:55 -0500 | [diff] [blame^] | 169 | void Manager::createCalloutObjects(const std::string& objectPath, |
| 170 | const DbusInterfaceMap& interfaces) |
| 171 | { |
| 172 | // Use the associations property in the org.openbmc.Associations |
| 173 | // interface to find any callouts. Then grab all properties on |
| 174 | // the Asset interface for that object in the inventory to use |
| 175 | // in our callout objects. |
| 176 | |
| 177 | auto associations = interfaces.find(ASSOC_IFACE); |
| 178 | if (associations == interfaces.end()) |
| 179 | { |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | const auto& properties = associations->second; |
| 184 | auto assocProperty = properties.find("associations"); |
| 185 | auto assocValue = assocProperty->second.get<AssociationsPropertyType>(); |
| 186 | |
| 187 | auto id = getEntryID(objectPath); |
| 188 | auto calloutNum = 0; |
| 189 | DbusSubtree subtree; |
| 190 | |
| 191 | for (const auto& association : assocValue) |
| 192 | { |
| 193 | if (std::get<forwardPos>(association) != "callout") |
| 194 | { |
| 195 | continue; |
| 196 | } |
| 197 | |
| 198 | auto callout = std::get<endpointPos>(association); |
| 199 | |
| 200 | if (subtree.empty()) |
| 201 | { |
| 202 | subtree = getSubtree(bus, "/", 0, ASSET_IFACE); |
| 203 | if (subtree.empty()) |
| 204 | { |
| 205 | break; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | auto service = getService(callout, ASSET_IFACE, subtree); |
| 210 | if (service.empty()) |
| 211 | { |
| 212 | continue; |
| 213 | } |
| 214 | |
| 215 | auto properties = getAllProperties(bus, service, callout, ASSET_IFACE); |
| 216 | if (properties.empty()) |
| 217 | { |
| 218 | continue; |
| 219 | } |
| 220 | |
| 221 | auto calloutPath = getCalloutObjectPath(objectPath, calloutNum); |
| 222 | |
| 223 | auto object = |
| 224 | std::make_shared<Callout>(bus, calloutPath, callout, calloutNum, |
| 225 | getLogTimestamp(interfaces), properties); |
| 226 | |
| 227 | auto dir = getCalloutSaveDir(id); |
| 228 | if (!fs::exists(dir)) |
| 229 | { |
| 230 | fs::create_directories(dir); |
| 231 | } |
| 232 | object->serialize(dir); |
| 233 | |
| 234 | std::experimental::any anyObject = object; |
| 235 | addChildInterface(objectPath, InterfaceType::CALLOUT, anyObject); |
| 236 | calloutNum++; |
| 237 | } |
| 238 | } |
| 239 | |
Matt Spinler | e0017eb | 2018-03-27 11:17:38 -0500 | [diff] [blame] | 240 | void Manager::interfaceAdded(sdbusplus::message::message& msg) |
| 241 | { |
| 242 | sdbusplus::message::object_path path; |
| 243 | DbusInterfaceMap interfaces; |
| 244 | |
| 245 | msg.read(path, interfaces); |
| 246 | |
Matt Spinler | 259e727 | 2018-03-29 10:57:17 -0500 | [diff] [blame] | 247 | // Find the Logging.Entry interface with all of its properties |
| 248 | // to pass to create(). |
Matt Spinler | e6a5159 | 2018-05-23 12:47:10 -0500 | [diff] [blame] | 249 | if (interfaces.find(LOGGING_IFACE) != interfaces.end()) |
Matt Spinler | e0017eb | 2018-03-27 11:17:38 -0500 | [diff] [blame] | 250 | { |
Matt Spinler | e6a5159 | 2018-05-23 12:47:10 -0500 | [diff] [blame] | 251 | create(path, interfaces); |
Matt Spinler | e0017eb | 2018-03-27 11:17:38 -0500 | [diff] [blame] | 252 | } |
| 253 | } |
Matt Spinler | 055da3b | 2018-05-09 15:51:20 -0500 | [diff] [blame] | 254 | |
Matt Spinler | 52ee71b | 2018-05-23 13:18:55 -0500 | [diff] [blame^] | 255 | uint64_t Manager::getLogTimestamp(const DbusInterfaceMap& interfaces) |
| 256 | { |
| 257 | auto interface = interfaces.find(LOGGING_IFACE); |
| 258 | if (interface != interfaces.end()) |
| 259 | { |
| 260 | auto property = interface->second.find("Timestamp"); |
| 261 | if (property != interface->second.end()) |
| 262 | { |
| 263 | return property->second.get<uint64_t>(); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | fs::path Manager::getSaveDir(EntryID id) |
| 271 | { |
| 272 | return fs::path{ERRLOG_PERSIST_PATH} / std::to_string(id); |
| 273 | } |
| 274 | |
| 275 | fs::path Manager::getCalloutSaveDir(EntryID id) |
| 276 | { |
| 277 | return getSaveDir(id) / "callouts"; |
| 278 | } |
| 279 | |
| 280 | std::string Manager::getCalloutObjectPath(const std::string& objectPath, |
| 281 | uint32_t calloutNum) |
| 282 | { |
| 283 | return fs::path{objectPath} / "callouts" / std::to_string(calloutNum); |
| 284 | } |
| 285 | |
Matt Spinler | 055da3b | 2018-05-09 15:51:20 -0500 | [diff] [blame] | 286 | void Manager::interfaceRemoved(sdbusplus::message::message& msg) |
| 287 | { |
| 288 | sdbusplus::message::object_path path; |
| 289 | DbusInterfaceList interfaces; |
| 290 | |
| 291 | msg.read(path, interfaces); |
| 292 | |
| 293 | // If the Logging.Entry interface was removed, then remove |
| 294 | // our object |
| 295 | |
| 296 | auto i = std::find(interfaces.begin(), interfaces.end(), LOGGING_IFACE); |
| 297 | |
| 298 | if (i != interfaces.end()) |
| 299 | { |
Matt Spinler | a139035 | 2018-05-23 10:48:19 -0500 | [diff] [blame] | 300 | erase(getEntryID(path)); |
Matt Spinler | 055da3b | 2018-05-09 15:51:20 -0500 | [diff] [blame] | 301 | } |
| 302 | } |
Matt Spinler | e0017eb | 2018-03-27 11:17:38 -0500 | [diff] [blame] | 303 | } |
| 304 | } |