mbpfan-josie

Correctly test sensor path for Linux prior to 3.15

Previously the fopen write mode caused the call to spuriously report
newer sensors.  Now we check the newer sensors path explicitly.  Also
plug file descriptor leak and more carefully check return values.

885a084f5cdfca443a9665787135de394a2cc833
Andrew Gaul <andrew@gaul.org> · 2017-07-12T17:56 · browse files at this commit

parents: ca52e9c

diff --git a/README.md b/README.md
index 9a26648..fb179a6 100644
--- a/README.md
+++ b/README.md
@@ -62,6 +62,7 @@ This section reports those models where mbpfan was tested successfully. It does
 - MacBook Pro 6,2 15"  (Intel i7 - Linux 3.5.0)
 - MacBook Pro 6,2 15"  (Intel i7 - Linux 3.2.0)
 - MacBook Pro 2,2 15"  (Intel Core 2 Duo - Linux 3.4.4)
+- MacBook Air 6,1 13"  (Intel i7 - Linux 3.13)
 - MacBook Air 5,2 13"  (Intel i5 - Linux 3.16)
 - MacBook Air 1,1 13"  (Intel Core Duo - Linux 4.4, Linux 4.8)
 - MacBook Air 7,2 13"  (Intel Core Duo - Linux 4.10)
diff --git a/src/mbpfan.c b/src/mbpfan.c
index acbdc08..67958bc 100644
--- a/src/mbpfan.c
+++ b/src/mbpfan.c
@@ -100,31 +100,25 @@ bool is_legacy_sensors_path()
         exit(EXIT_FAILURE);
     }
 
-    
-    // thanks http://stackoverflow.com/questions/18192998/plain-c-opening-a-directory-with-fopen
-    fopen("/sys/devices/platform/coretemp.0/hwmon", "wb");
+    const char *path_begin = "/sys/devices/platform/coretemp.0/hwmon/hwmon";
+    int counter;
 
-    if (errno == EISDIR) {
-        return 0;
-    } else {
-        return 1;
-    }
+    for (counter = 0; counter < 10; counter++) {
+        char hwmon_path[strlen(path_begin)+2];
+        sprintf(hwmon_path, "%s%d", path_begin, counter);
 
-    // 
-    // str_kernel_version = strtok(NULL, ".");
-    // int kernel_version = atoi(str_kernel_version);
-
-    // if(verbose) {
-    //     printf("Detected kernel version: %s\n", kernel.release);
-    //     printf("Detected kernel minor revision: %s\n", str_kernel_version);
+        FILE *file = fopen(hwmon_path, "rb");
+        int isdir = file == NULL && errno == EISDIR;
+        if (file != NULL) {
+            fclose(file);
+        }
 
-    //     if(daemonize) {
-    //         syslog(LOG_INFO, "Kernel version: %s", kernel.release);
-    //         syslog(LOG_INFO, "Detected kernel minor revision: %s", str_kernel_version);
-    //     }
-    // }
+        if (isdir) {
+            return 0;
+        }
+    }
 
-    // return (atoi(kernel.release) == 3 && kernel_version < 15);
+    return 1;
 }
 
 
@@ -167,10 +161,13 @@ t_sensors *retrieve_sensors()
 
             sprintf(hwmon_path, "%s%d", path_begin, counter);
 
-            // thanks http://stackoverflow.com/questions/18192998/plain-c-opening-a-directory-with-fopen
-            fopen(hwmon_path, "wb");
+            FILE *file = fopen(hwmon_path, "rb");
+            int isdir = file == NULL && errno == EISDIR;
+            if (file != NULL) {
+                fclose(file);
+            }
 
-            if (errno == EISDIR) {
+            if (isdir) {
 
                 free(path_begin);
                 path_begin = smprintf("%s/temp", hwmon_path);