fansensor: fix fan sensor service crash
fs::canonical can throw exception if the path does not exist,
which can happen for i2c fan type. Check for path exists before the
call.
Tested:
fan sensor service successfully detect i2c fan type and does not crash.
Fixes: 9a472e8 ("detect fan type by matching against 'compatible'")
Signed-off-by: Zhikui Ren <zhikui.ren@intel.com>
Change-Id: I395e4d47b16fc58eba777e88be304bf28993bec3
diff --git a/src/FanMain.cpp b/src/FanMain.cpp
index 32022c1..85de747 100644
--- a/src/FanMain.cpp
+++ b/src/FanMain.cpp
@@ -74,8 +74,12 @@
FanTypes getFanType(const fs::path& parentPath)
{
fs::path linkPath = parentPath / "of_node";
- std::string canonical = fs::canonical(linkPath);
+ if (!fs::exists(linkPath))
+ {
+ return FanTypes::i2c;
+ }
+ std::string canonical = fs::canonical(linkPath);
std::string compatiblePath = canonical + "/compatible";
std::ifstream compatibleStream(compatiblePath);