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 | */ |
| 16 | #include <cstdlib> |
Brad Bishop | 08379a3 | 2017-03-06 21:28:46 -0500 | [diff] [blame] | 17 | #include <experimental/filesystem> |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 18 | #include <memory> |
Brad Bishop | 4db6442 | 2017-02-16 11:33:32 -0500 | [diff] [blame] | 19 | #include <phosphor-logging/log.hpp> |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 20 | #include "sysfs.hpp" |
| 21 | #include "util.hpp" |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 22 | |
| 23 | std::string findHwmon(const std::string& ofNode) |
| 24 | { |
Brad Bishop | 08379a3 | 2017-03-06 21:28:46 -0500 | [diff] [blame] | 25 | namespace fs = std::experimental::filesystem; |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 26 | static constexpr auto hwmonRoot = "/sys/class/hwmon"; |
Brad Bishop | 08379a3 | 2017-03-06 21:28:46 -0500 | [diff] [blame] | 27 | static constexpr auto ofRoot = "/sys/firmware/devicetree/base"; |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 28 | |
Brad Bishop | 08379a3 | 2017-03-06 21:28:46 -0500 | [diff] [blame] | 29 | fs::path fullOfPath{ofRoot}; |
| 30 | fullOfPath /= ofNode; |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 31 | |
Brad Bishop | 08379a3 | 2017-03-06 21:28:46 -0500 | [diff] [blame] | 32 | for (const auto& hwmonInst : fs::directory_iterator(hwmonRoot)) |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 33 | { |
Brad Bishop | 08379a3 | 2017-03-06 21:28:46 -0500 | [diff] [blame] | 34 | auto path = hwmonInst.path(); |
| 35 | path /= "of_node"; |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 36 | |
Brad Bishop | 08379a3 | 2017-03-06 21:28:46 -0500 | [diff] [blame] | 37 | if (fs::canonical(path) != fullOfPath) |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 38 | { |
| 39 | continue; |
| 40 | } |
| 41 | |
Brad Bishop | 08379a3 | 2017-03-06 21:28:46 -0500 | [diff] [blame] | 42 | return hwmonInst.path(); |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | return std::string(); |
| 46 | } |
| 47 | |
Brad Bishop | 4db6442 | 2017-02-16 11:33:32 -0500 | [diff] [blame] | 48 | int readSysfsWithCallout(const std::string& root, |
| 49 | const std::string& instance, |
| 50 | const std::string& type, |
| 51 | const std::string& id, |
| 52 | const std::string& sensor) |
| 53 | { |
Brad Bishop | 5ec68ab | 2017-03-27 13:41:02 -0400 | [diff] [blame^] | 54 | namespace fs = std::experimental::filesystem; |
| 55 | |
Brad Bishop | 4db6442 | 2017-02-16 11:33:32 -0500 | [diff] [blame] | 56 | int value = 0; |
Brad Bishop | 5ec68ab | 2017-03-27 13:41:02 -0400 | [diff] [blame^] | 57 | std::ifstream ifs; |
| 58 | fs::path instancePath{root}; |
| 59 | instancePath /= instance; |
Brad Bishop | 4db6442 | 2017-02-16 11:33:32 -0500 | [diff] [blame] | 60 | std::string fullPath = make_sysfs_path(instancePath, |
| 61 | type, id, sensor); |
Brad Bishop | 4db6442 | 2017-02-16 11:33:32 -0500 | [diff] [blame] | 62 | |
| 63 | ifs.exceptions(std::ifstream::failbit |
| 64 | | std::ifstream::badbit |
| 65 | | std::ifstream::eofbit); |
| 66 | try |
| 67 | { |
| 68 | ifs.open(fullPath); |
| 69 | ifs >> value; |
| 70 | } |
| 71 | catch (const std::exception& e) |
| 72 | { |
| 73 | // Too many GCC bugs (53984, 66145) to do |
| 74 | // this the right way... |
Brad Bishop | 4db6442 | 2017-02-16 11:33:32 -0500 | [diff] [blame] | 75 | |
| 76 | // errno should still reflect the error from the failing open |
| 77 | // or read system calls that got us here. |
| 78 | auto rc = errno; |
Brad Bishop | 5ec68ab | 2017-03-27 13:41:02 -0400 | [diff] [blame^] | 79 | instancePath /= "device"; |
Brad Bishop | 4db6442 | 2017-02-16 11:33:32 -0500 | [diff] [blame] | 80 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 81 | strerror(rc), |
Brad Bishop | 5ec68ab | 2017-03-27 13:41:02 -0400 | [diff] [blame^] | 82 | phosphor::logging::entry( |
| 83 | "CALLOUT_DEVICE_PATH=%s", |
| 84 | fs::canonical(instancePath).c_str()), |
Brad Bishop | 4db6442 | 2017-02-16 11:33:32 -0500 | [diff] [blame] | 85 | phosphor::logging::entry("CALLOUT_ERRNO=%d", rc)); |
| 86 | exit(EXIT_FAILURE); |
| 87 | } |
| 88 | |
| 89 | return value; |
| 90 | } |
| 91 | |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 92 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |