Skip to content

Commit 885a084

Browse files
committed
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.
1 parent ca52e9c commit 885a084

2 files changed

Lines changed: 22 additions & 24 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ This section reports those models where mbpfan was tested successfully. It does
6262
- MacBook Pro 6,2 15" (Intel i7 - Linux 3.5.0)
6363
- MacBook Pro 6,2 15" (Intel i7 - Linux 3.2.0)
6464
- MacBook Pro 2,2 15" (Intel Core 2 Duo - Linux 3.4.4)
65+
- MacBook Air 6,1 13" (Intel i7 - Linux 3.13)
6566
- MacBook Air 5,2 13" (Intel i5 - Linux 3.16)
6667
- MacBook Air 1,1 13" (Intel Core Duo - Linux 4.4, Linux 4.8)
6768
- MacBook Air 7,2 13" (Intel Core Duo - Linux 4.10)

src/mbpfan.c

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -100,31 +100,25 @@ bool is_legacy_sensors_path()
100100
exit(EXIT_FAILURE);
101101
}
102102

103-
104-
// thanks http://stackoverflow.com/questions/18192998/plain-c-opening-a-directory-with-fopen
105-
fopen("/sys/devices/platform/coretemp.0/hwmon", "wb");
103+
const char *path_begin = "/sys/devices/platform/coretemp.0/hwmon/hwmon";
104+
int counter;
106105

107-
if (errno == EISDIR) {
108-
return 0;
109-
} else {
110-
return 1;
111-
}
106+
for (counter = 0; counter < 10; counter++) {
107+
char hwmon_path[strlen(path_begin)+2];
108+
sprintf(hwmon_path, "%s%d", path_begin, counter);
112109

113-
//
114-
// str_kernel_version = strtok(NULL, ".");
115-
// int kernel_version = atoi(str_kernel_version);
116-
117-
// if(verbose) {
118-
// printf("Detected kernel version: %s\n", kernel.release);
119-
// printf("Detected kernel minor revision: %s\n", str_kernel_version);
110+
FILE *file = fopen(hwmon_path, "rb");
111+
int isdir = file == NULL && errno == EISDIR;
112+
if (file != NULL) {
113+
fclose(file);
114+
}
120115

121-
// if(daemonize) {
122-
// syslog(LOG_INFO, "Kernel version: %s", kernel.release);
123-
// syslog(LOG_INFO, "Detected kernel minor revision: %s", str_kernel_version);
124-
// }
125-
// }
116+
if (isdir) {
117+
return 0;
118+
}
119+
}
126120

127-
// return (atoi(kernel.release) == 3 && kernel_version < 15);
121+
return 1;
128122
}
129123

130124

@@ -167,10 +161,13 @@ t_sensors *retrieve_sensors()
167161

168162
sprintf(hwmon_path, "%s%d", path_begin, counter);
169163

170-
// thanks http://stackoverflow.com/questions/18192998/plain-c-opening-a-directory-with-fopen
171-
fopen(hwmon_path, "wb");
164+
FILE *file = fopen(hwmon_path, "rb");
165+
int isdir = file == NULL && errno == EISDIR;
166+
if (file != NULL) {
167+
fclose(file);
168+
}
172169

173-
if (errno == EISDIR) {
170+
if (isdir) {
174171

175172
free(path_begin);
176173
path_begin = smprintf("%s/temp", hwmon_path);

0 commit comments

Comments
 (0)