sysfs.cpp (431d26a58b89b88492a0dc5c87d3d9e8de49ade1) sysfs.cpp (8b574a7eecd288f5342779cdf4aa20d640cbe141)
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 */
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 <cerrno>
16#include <cstdlib>
17#include <experimental/filesystem>
18#include <fstream>
19#include <memory>
20#include <phosphor-logging/elog.hpp>
21#include <phosphor-logging/elog-errors.hpp>
22#include <xyz/openbmc_project/Control/Device/error.hpp>
23#include <xyz/openbmc_project/Sensor/Device/error.hpp>

--- 282 unchanged lines hidden (view full) ---

306 WriteFailure::CALLOUT_DEVICE_PATH(callOutPath.c_str()));
307
308 exit(EXIT_FAILURE);
309 }
310
311 return value;
312}
313
17#include <cstdlib>
18#include <experimental/filesystem>
19#include <fstream>
20#include <memory>
21#include <phosphor-logging/elog.hpp>
22#include <phosphor-logging/elog-errors.hpp>
23#include <xyz/openbmc_project/Control/Device/error.hpp>
24#include <xyz/openbmc_project/Sensor/Device/error.hpp>

--- 282 unchanged lines hidden (view full) ---

307 WriteFailure::CALLOUT_DEVICE_PATH(callOutPath.c_str()));
308
309 exit(EXIT_FAILURE);
310 }
311
312 return value;
313}
314
315namespace hwmonio
316{
317
318HwmonIO::HwmonIO(const std::string& path) : p(path)
319{
320
314}
321}
322
323uint32_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
371void 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
412std::string HwmonIO::path() const
413{
414 return p;
415}
416
417} // namespace hwmonio
418}
315// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
419// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4