Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2016 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 | */ |
Brad Bishop | 8b574a7 | 2017-08-25 16:17:19 -0400 | [diff] [blame^] | 16 | #include <cerrno> |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 17 | #include <cstdlib> |
Brad Bishop | 08379a3 | 2017-03-06 21:28:46 -0500 | [diff] [blame] | 18 | #include <experimental/filesystem> |
Brad Bishop | 68c43b2 | 2017-08-28 16:24:00 -0400 | [diff] [blame] | 19 | #include <fstream> |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 20 | #include <memory> |
Matthew Barth | 048ac87 | 2017-03-09 14:36:08 -0600 | [diff] [blame] | 21 | #include <phosphor-logging/elog.hpp> |
| 22 | #include <phosphor-logging/elog-errors.hpp> |
| 23 | #include <xyz/openbmc_project/Control/Device/error.hpp> |
Matthew Barth | 4e1f30f | 2017-03-21 16:13:27 -0500 | [diff] [blame] | 24 | #include <xyz/openbmc_project/Sensor/Device/error.hpp> |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 25 | #include "sysfs.hpp" |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 26 | |
Matthew Barth | 048ac87 | 2017-03-09 14:36:08 -0600 | [diff] [blame] | 27 | using namespace phosphor::logging; |
Brad Bishop | f4bf63a | 2017-08-28 15:39:19 -0400 | [diff] [blame] | 28 | using namespace std::string_literals; |
Brandon Wyman | 8af8a20 | 2017-05-31 18:26:30 -0500 | [diff] [blame] | 29 | namespace fs = std::experimental::filesystem; |
Matthew Barth | 048ac87 | 2017-03-09 14:36:08 -0600 | [diff] [blame] | 30 | |
Patrick Venture | 1e6324f | 2017-06-01 14:07:05 -0700 | [diff] [blame] | 31 | namespace sysfs { |
| 32 | |
Brad Bishop | f4bf63a | 2017-08-28 15:39:19 -0400 | [diff] [blame] | 33 | static const auto emptyString = ""s; |
Brandon Wyman | 8af8a20 | 2017-05-31 18:26:30 -0500 | [diff] [blame] | 34 | static constexpr auto ofRoot = "/sys/firmware/devicetree/base"; |
| 35 | |
Brad Bishop | f4bf63a | 2017-08-28 15:39:19 -0400 | [diff] [blame] | 36 | std::string findPhandleMatch( |
| 37 | const std::string& iochanneldir, |
| 38 | const std::string& phandledir) |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 39 | { |
Brad Bishop | f4bf63a | 2017-08-28 15:39:19 -0400 | [diff] [blame] | 40 | // TODO: At the moment this method only supports device trees |
| 41 | // with iio-hwmon nodes with a single sensor. Typically |
| 42 | // device trees are defined with all the iio sensors in a |
| 43 | // single iio-hwmon node so it would be nice to add support |
| 44 | // for lists of phandles (with variable sized entries) via |
| 45 | // libfdt or something like that, so that users are not |
| 46 | // forced into implementing unusual looking device trees |
| 47 | // with multiple iio-hwmon nodes - one for each sensor. |
| 48 | |
| 49 | fs::path ioChannelsPath{iochanneldir}; |
| 50 | ioChannelsPath /= "io-channels"; |
| 51 | |
| 52 | if (!fs::exists(ioChannelsPath)) |
| 53 | { |
| 54 | return emptyString; |
| 55 | } |
| 56 | |
| 57 | uint32_t ioChannelsValue; |
| 58 | std::ifstream ioChannelsFile(ioChannelsPath); |
| 59 | |
| 60 | ioChannelsFile.read( |
| 61 | reinterpret_cast<char*>(&ioChannelsValue), |
| 62 | sizeof(ioChannelsValue)); |
| 63 | |
Brandon Wyman | 8af8a20 | 2017-05-31 18:26:30 -0500 | [diff] [blame] | 64 | for (const auto& ofInst : fs::recursive_directory_iterator(phandledir)) |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 65 | { |
Brandon Wyman | 8af8a20 | 2017-05-31 18:26:30 -0500 | [diff] [blame] | 66 | auto path = ofInst.path(); |
Brad Bishop | f4bf63a | 2017-08-28 15:39:19 -0400 | [diff] [blame] | 67 | if ("phandle" != path.filename()) |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 68 | { |
Brad Bishop | f4bf63a | 2017-08-28 15:39:19 -0400 | [diff] [blame] | 69 | continue; |
| 70 | } |
| 71 | std::ifstream pHandleFile(path); |
| 72 | uint32_t pHandleValue; |
Brandon Wyman | 4eb9858 | 2017-05-24 14:24:00 -0500 | [diff] [blame] | 73 | |
Brad Bishop | f4bf63a | 2017-08-28 15:39:19 -0400 | [diff] [blame] | 74 | pHandleFile.read( |
| 75 | reinterpret_cast<char*>(&pHandleValue), |
| 76 | sizeof(pHandleValue)); |
Brandon Wyman | 4eb9858 | 2017-05-24 14:24:00 -0500 | [diff] [blame] | 77 | |
Brad Bishop | f4bf63a | 2017-08-28 15:39:19 -0400 | [diff] [blame] | 78 | if (ioChannelsValue == pHandleValue) |
| 79 | { |
| 80 | return path; |
Brandon Wyman | 8af8a20 | 2017-05-31 18:26:30 -0500 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
Brad Bishop | f4bf63a | 2017-08-28 15:39:19 -0400 | [diff] [blame] | 84 | return emptyString; |
Brandon Wyman | 8af8a20 | 2017-05-31 18:26:30 -0500 | [diff] [blame] | 85 | } |
| 86 | |
Brad Bishop | 431d26a | 2017-08-25 09:47:58 -0400 | [diff] [blame] | 87 | std::string findCalloutPath(const std::string& instancePath) |
Brandon Wyman | 8af8a20 | 2017-05-31 18:26:30 -0500 | [diff] [blame] | 88 | { |
Brad Bishop | 431d26a | 2017-08-25 09:47:58 -0400 | [diff] [blame] | 89 | // Follow the hwmon instance (/sys/class/hwmon/hwmon<N>) |
| 90 | // /sys/devices symlink. |
| 91 | fs::path devPath{instancePath}; |
| 92 | devPath /= "device"; |
Brandon Wyman | 8af8a20 | 2017-05-31 18:26:30 -0500 | [diff] [blame] | 93 | |
Brad Bishop | 431d26a | 2017-08-25 09:47:58 -0400 | [diff] [blame] | 94 | try |
Brandon Wyman | 8af8a20 | 2017-05-31 18:26:30 -0500 | [diff] [blame] | 95 | { |
Brad Bishop | 431d26a | 2017-08-25 09:47:58 -0400 | [diff] [blame] | 96 | devPath = fs::canonical(devPath); |
| 97 | } |
| 98 | catch (const std::system_error& e) |
| 99 | { |
| 100 | return emptyString; |
| 101 | } |
| 102 | |
| 103 | // See if the device is backed by the iio-hwmon driver. |
| 104 | fs::path p{devPath}; |
| 105 | p /= "driver"; |
| 106 | p = fs::canonical(p); |
| 107 | |
| 108 | if (p.filename() != "iio_hwmon") |
| 109 | { |
| 110 | // Not backed by iio-hwmon. The device pointed to |
| 111 | // is the callout device. |
| 112 | return devPath; |
| 113 | } |
| 114 | |
| 115 | // Find the DT path to the iio-hwmon platform device. |
| 116 | fs::path ofDevPath{devPath}; |
| 117 | ofDevPath /= "of_node"; |
| 118 | |
| 119 | try |
| 120 | { |
| 121 | ofDevPath = fs::canonical(ofDevPath); |
| 122 | } |
| 123 | catch (const std::system_error& e) |
| 124 | { |
| 125 | return emptyString; |
| 126 | } |
| 127 | |
| 128 | // Search /sys/bus/iio/devices for the phandle in io-channels. |
| 129 | // If a match is found, use the corresponding /sys/devices |
| 130 | // iio device as the callout device. |
| 131 | static constexpr auto iioDevices = "/sys/bus/iio/devices"; |
| 132 | for (const auto& iioDev: fs::recursive_directory_iterator(iioDevices)) |
| 133 | { |
| 134 | p = iioDev.path(); |
| 135 | p /= "of_node"; |
| 136 | |
| 137 | try |
| 138 | { |
| 139 | p = fs::canonical(p); |
| 140 | } |
| 141 | catch (const std::system_error& e) |
| 142 | { |
| 143 | continue; |
| 144 | } |
| 145 | |
| 146 | auto match = findPhandleMatch(ofDevPath, p); |
| 147 | auto n = match.rfind('/'); |
Brandon Wyman | 8af8a20 | 2017-05-31 18:26:30 -0500 | [diff] [blame] | 148 | if (n != std::string::npos) |
| 149 | { |
Brad Bishop | 431d26a | 2017-08-25 09:47:58 -0400 | [diff] [blame] | 150 | // This is the iio device referred to by io-channels. |
| 151 | // Remove iio:device<N>. |
| 152 | try |
| 153 | { |
| 154 | return fs::canonical(iioDev).parent_path(); |
| 155 | } |
| 156 | catch (const std::system_error& e) |
| 157 | { |
| 158 | return emptyString; |
| 159 | } |
Brandon Wyman | 8af8a20 | 2017-05-31 18:26:30 -0500 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
Brad Bishop | 431d26a | 2017-08-25 09:47:58 -0400 | [diff] [blame] | 163 | return emptyString; |
Brandon Wyman | 8af8a20 | 2017-05-31 18:26:30 -0500 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | std::string findHwmon(const std::string& ofNode) |
| 167 | { |
| 168 | static constexpr auto hwmonRoot = "/sys/class/hwmon"; |
| 169 | |
| 170 | fs::path fullOfPath{ofRoot}; |
| 171 | fullOfPath /= ofNode; |
| 172 | |
| 173 | for (const auto& hwmonInst : fs::directory_iterator(hwmonRoot)) |
| 174 | { |
| 175 | auto path = hwmonInst.path(); |
| 176 | path /= "of_node"; |
Brad Bishop | 4e24ebd | 2017-08-28 16:19:16 -0400 | [diff] [blame] | 177 | |
| 178 | try |
Brandon Wyman | 8af8a20 | 2017-05-31 18:26:30 -0500 | [diff] [blame] | 179 | { |
Brad Bishop | 4e24ebd | 2017-08-28 16:19:16 -0400 | [diff] [blame] | 180 | path = fs::canonical(path); |
| 181 | } |
| 182 | catch (const std::system_error& e) |
| 183 | { |
| 184 | // realpath may encounter ENOENT (Hwmon |
| 185 | // instances have a nasty habit of |
| 186 | // going away without warning). |
| 187 | continue; |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 188 | } |
| 189 | |
Brad Bishop | 4e24ebd | 2017-08-28 16:19:16 -0400 | [diff] [blame] | 190 | if (path == fullOfPath) |
| 191 | { |
| 192 | return hwmonInst.path(); |
| 193 | } |
| 194 | |
| 195 | // Try to find HWMON instance via phandle values. |
| 196 | // Used for IIO device drivers. |
| 197 | auto matchpath = findPhandleMatch(path, fullOfPath); |
| 198 | if (!matchpath.empty()) |
| 199 | { |
| 200 | return hwmonInst.path(); |
| 201 | } |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 202 | } |
| 203 | |
Brad Bishop | 4e24ebd | 2017-08-28 16:19:16 -0400 | [diff] [blame] | 204 | return emptyString; |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 205 | } |
| 206 | |
Brad Bishop | 4db6442 | 2017-02-16 11:33:32 -0500 | [diff] [blame] | 207 | int readSysfsWithCallout(const std::string& root, |
| 208 | const std::string& instance, |
| 209 | const std::string& type, |
| 210 | const std::string& id, |
Matt Spinler | 3b8e36e | 2017-07-28 10:44:45 -0500 | [diff] [blame] | 211 | const std::string& sensor, |
| 212 | bool throwDeviceBusy) |
Brad Bishop | 4db6442 | 2017-02-16 11:33:32 -0500 | [diff] [blame] | 213 | { |
Brad Bishop | 5ec68ab | 2017-03-27 13:41:02 -0400 | [diff] [blame] | 214 | namespace fs = std::experimental::filesystem; |
| 215 | |
Brad Bishop | 4db6442 | 2017-02-16 11:33:32 -0500 | [diff] [blame] | 216 | int value = 0; |
Brad Bishop | 5ec68ab | 2017-03-27 13:41:02 -0400 | [diff] [blame] | 217 | std::ifstream ifs; |
| 218 | fs::path instancePath{root}; |
| 219 | instancePath /= instance; |
Brad Bishop | 4db6442 | 2017-02-16 11:33:32 -0500 | [diff] [blame] | 220 | std::string fullPath = make_sysfs_path(instancePath, |
| 221 | type, id, sensor); |
Brad Bishop | 4db6442 | 2017-02-16 11:33:32 -0500 | [diff] [blame] | 222 | |
| 223 | ifs.exceptions(std::ifstream::failbit |
| 224 | | std::ifstream::badbit |
| 225 | | std::ifstream::eofbit); |
| 226 | try |
| 227 | { |
| 228 | ifs.open(fullPath); |
| 229 | ifs >> value; |
| 230 | } |
| 231 | catch (const std::exception& e) |
| 232 | { |
| 233 | // Too many GCC bugs (53984, 66145) to do |
| 234 | // this the right way... |
Brad Bishop | 4db6442 | 2017-02-16 11:33:32 -0500 | [diff] [blame] | 235 | |
| 236 | // errno should still reflect the error from the failing open |
| 237 | // or read system calls that got us here. |
| 238 | auto rc = errno; |
Andrew Geissler | ac8b7c6 | 2017-06-13 16:15:30 -0500 | [diff] [blame] | 239 | |
Matt Spinler | 3b8e36e | 2017-07-28 10:44:45 -0500 | [diff] [blame] | 240 | if ((rc == EAGAIN) && throwDeviceBusy) |
| 241 | { |
| 242 | throw DeviceBusyException(fullPath); |
| 243 | } |
| 244 | |
Andrew Geissler | ac8b7c6 | 2017-06-13 16:15:30 -0500 | [diff] [blame] | 245 | // If the directory disappeared then this application should gracefully |
| 246 | // exit. There are race conditions between the unloading of a hwmon |
| 247 | // driver and the stopping of this service by systemd. To prevent |
| 248 | // this application from falsely failing in these scenarios, it will |
| 249 | // simply exit if the directory or file can not be found. It is up |
| 250 | // to the user(s) of this provided hwmon object to log the appropriate |
| 251 | // errors if the object disappears when it should not. |
| 252 | if (rc == ENOENT) |
| 253 | { |
| 254 | exit(0); |
| 255 | } |
Brad Bishop | 5ec68ab | 2017-03-27 13:41:02 -0400 | [diff] [blame] | 256 | instancePath /= "device"; |
Brad Bishop | 431d26a | 2017-08-25 09:47:58 -0400 | [diff] [blame] | 257 | auto callOutPath = findCalloutPath(instancePath); |
Matthew Barth | 4e1f30f | 2017-03-21 16:13:27 -0500 | [diff] [blame] | 258 | using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::Error; |
Patrick Venture | 1e6324f | 2017-06-01 14:07:05 -0700 | [diff] [blame] | 259 | |
| 260 | // this throws a ReadFailure. |
| 261 | elog<ReadFailure>( |
Marri Devender Rao | 05711eb | 2017-04-15 06:47:11 -0500 | [diff] [blame] | 262 | xyz::openbmc_project::Sensor::Device:: |
| 263 | ReadFailure::CALLOUT_ERRNO(rc), |
| 264 | xyz::openbmc_project::Sensor::Device:: |
Brad Bishop | 431d26a | 2017-08-25 09:47:58 -0400 | [diff] [blame] | 265 | ReadFailure::CALLOUT_DEVICE_PATH(callOutPath.c_str())); |
Brad Bishop | 4db6442 | 2017-02-16 11:33:32 -0500 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | return value; |
| 269 | } |
| 270 | |
Matthew Barth | 048ac87 | 2017-03-09 14:36:08 -0600 | [diff] [blame] | 271 | uint64_t writeSysfsWithCallout(const uint64_t& value, |
| 272 | const std::string& root, |
| 273 | const std::string& instance, |
| 274 | const std::string& type, |
| 275 | const std::string& id, |
| 276 | const std::string& sensor) |
| 277 | { |
| 278 | namespace fs = std::experimental::filesystem; |
| 279 | |
| 280 | std::string valueStr = std::to_string(value); |
| 281 | std::ofstream ofs; |
| 282 | fs::path instancePath{root}; |
| 283 | instancePath /= instance; |
| 284 | std::string fullPath = make_sysfs_path(instancePath, |
| 285 | type, id, sensor); |
| 286 | |
| 287 | ofs.exceptions(std::ofstream::failbit |
| 288 | | std::ofstream::badbit |
| 289 | | std::ofstream::eofbit); |
| 290 | try |
| 291 | { |
| 292 | ofs.open(fullPath); |
| 293 | ofs << valueStr; |
| 294 | } |
| 295 | catch (const std::exception& e) |
| 296 | { |
| 297 | // errno should still reflect the error from the failing open |
| 298 | // or write system calls that got us here. |
| 299 | auto rc = errno; |
| 300 | instancePath /= "device"; |
Brad Bishop | 431d26a | 2017-08-25 09:47:58 -0400 | [diff] [blame] | 301 | auto callOutPath = findCalloutPath(instancePath); |
Matthew Barth | 048ac87 | 2017-03-09 14:36:08 -0600 | [diff] [blame] | 302 | using namespace sdbusplus::xyz::openbmc_project::Control::Device::Error; |
Marri Devender Rao | 05711eb | 2017-04-15 06:47:11 -0500 | [diff] [blame] | 303 | report<WriteFailure>( |
| 304 | xyz::openbmc_project::Control::Device:: |
| 305 | WriteFailure::CALLOUT_ERRNO(rc), |
| 306 | xyz::openbmc_project::Control::Device:: |
Brad Bishop | 431d26a | 2017-08-25 09:47:58 -0400 | [diff] [blame] | 307 | WriteFailure::CALLOUT_DEVICE_PATH(callOutPath.c_str())); |
Marri Devender Rao | 05711eb | 2017-04-15 06:47:11 -0500 | [diff] [blame] | 308 | |
Matthew Barth | 048ac87 | 2017-03-09 14:36:08 -0600 | [diff] [blame] | 309 | exit(EXIT_FAILURE); |
| 310 | } |
| 311 | |
| 312 | return value; |
| 313 | } |
| 314 | |
Brad Bishop | 8b574a7 | 2017-08-25 16:17:19 -0400 | [diff] [blame^] | 315 | namespace hwmonio |
| 316 | { |
| 317 | |
| 318 | HwmonIO::HwmonIO(const std::string& path) : p(path) |
| 319 | { |
| 320 | |
| 321 | } |
| 322 | |
| 323 | uint32_t HwmonIO::read( |
| 324 | const std::string& type, |
| 325 | const std::string& id, |
| 326 | const std::string& sensor) const |
| 327 | { |
| 328 | uint32_t val; |
| 329 | std::ifstream ifs; |
| 330 | auto fullPath = sysfs::make_sysfs_path( |
| 331 | p, type, id, sensor); |
| 332 | |
| 333 | ifs.exceptions( |
| 334 | std::ifstream::failbit | |
| 335 | std::ifstream::badbit | |
| 336 | std::ifstream::eofbit); |
| 337 | try |
| 338 | { |
| 339 | ifs.open(fullPath); |
| 340 | ifs >> val; |
| 341 | } |
| 342 | catch (const std::exception& e) |
| 343 | { |
| 344 | auto rc = errno; |
| 345 | |
| 346 | if (rc == ENOENT) |
| 347 | { |
| 348 | // If the directory disappeared then this application should |
| 349 | // gracefully exit. There are race conditions between the |
| 350 | // unloading of a hwmon driver and the stopping of this service |
| 351 | // by systemd. To prevent this application from falsely failing |
| 352 | // in these scenarios, it will simply exit if the directory or |
| 353 | // file can not be found. It is up to the user(s) of this |
| 354 | // provided hwmon object to log the appropriate errors if the |
| 355 | // object disappears when it should not. |
| 356 | exit(0); |
| 357 | } |
| 358 | |
| 359 | if (rc) |
| 360 | { |
| 361 | // Work around GCC bugs 53984 and 66145 for callers by |
| 362 | // explicitly raising system_error here. |
| 363 | throw std::system_error(rc, std::generic_category()); |
| 364 | } |
| 365 | |
| 366 | throw; |
| 367 | } |
| 368 | return val; |
| 369 | } |
| 370 | |
| 371 | void HwmonIO::write( |
| 372 | uint32_t val, |
| 373 | const std::string& type, |
| 374 | const std::string& id, |
| 375 | const std::string& sensor) const |
| 376 | { |
| 377 | std::ofstream ofs; |
| 378 | auto fullPath = sysfs::make_sysfs_path( |
| 379 | p, type, id, sensor); |
| 380 | |
| 381 | ofs.exceptions( |
| 382 | std::ofstream::failbit | |
| 383 | std::ofstream::badbit | |
| 384 | std::ofstream::eofbit); |
| 385 | |
| 386 | // See comments in the read method for an explanation of the odd exception |
| 387 | // handling behavior here. |
| 388 | |
| 389 | try |
| 390 | { |
| 391 | ofs.open(fullPath); |
| 392 | ofs << val; |
| 393 | } |
| 394 | catch (const std::exception& e) |
| 395 | { |
| 396 | auto rc = errno; |
| 397 | |
| 398 | if (rc == ENOENT) |
| 399 | { |
| 400 | exit(0); |
| 401 | } |
| 402 | |
| 403 | if (rc) |
| 404 | { |
| 405 | throw std::system_error(rc, std::generic_category()); |
| 406 | } |
| 407 | |
| 408 | throw; |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | std::string HwmonIO::path() const |
| 413 | { |
| 414 | return p; |
| 415 | } |
| 416 | |
| 417 | } // namespace hwmonio |
Patrick Venture | 1e6324f | 2017-06-01 14:07:05 -0700 | [diff] [blame] | 418 | } |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 419 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |