Skip to content

Commit 7e019f8

Browse files
committed
Correctly test sensor path for Linux 3.13
Previously the fopen write mode caused the call to spuriously report newer sensors. Now we check the newer sensors path. Also plug file descriptor leak and more carefully check return values.
1 parent 237eae7 commit 7e019f8

2 files changed

Lines changed: 21 additions & 34 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: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -87,43 +87,25 @@ static char *smprintf(const char *fmt, ...)
8787

8888
bool is_legacy_sensors_path()
8989
{
90-
struct utsname kernel;
91-
uname(&kernel);
90+
const char *path_begin = "/sys/devices/platform/coretemp.0/hwmon/hwmon";
91+
int counter;
9292

93-
char *str_kernel_version;
94-
str_kernel_version = strtok(kernel.release, ".");
93+
for (counter = 0; counter < 10; counter++) {
94+
char hwmon_path[strlen(path_begin)+2];
95+
sprintf(hwmon_path, "%s%d", path_begin, counter);
9596

96-
if (atoi(str_kernel_version) < 3){
97-
syslog(LOG_INFO, "mbpfan detected a pre-3.x.x linux kernel. Detected version: %s. Exiting.\n", kernel.release);
98-
printf("mbpfan detected a pre-3.x.x linux kernel. Detected version: %s. Exiting.\n", kernel.release);
99-
exit(EXIT_FAILURE);
100-
}
101-
102-
103-
// thanks http://stackoverflow.com/questions/18192998/plain-c-opening-a-directory-with-fopen
104-
fopen("/sys/devices/platform/coretemp.0/hwmon", "wb");
97+
FILE *file = fopen(hwmon_path, "rb");
98+
int isdir = file == NULL && errno == EISDIR;
99+
if (file != NULL) {
100+
fclose(file);
101+
}
105102

106-
if (errno == EISDIR) {
107-
return 0;
108-
} else {
109-
return 1;
103+
if (isdir) {
104+
return 0;
105+
}
110106
}
111107

112-
//
113-
// str_kernel_version = strtok(NULL, ".");
114-
// int kernel_version = atoi(str_kernel_version);
115-
116-
// if(verbose) {
117-
// printf("Detected kernel version: %s\n", kernel.release);
118-
// printf("Detected kernel minor revision: %s\n", str_kernel_version);
119-
120-
// if(daemonize) {
121-
// syslog(LOG_INFO, "Kernel version: %s", kernel.release);
122-
// syslog(LOG_INFO, "Detected kernel minor revision: %s", str_kernel_version);
123-
// }
124-
// }
125-
126-
// return (atoi(kernel.release) == 3 && kernel_version < 15);
108+
return 1;
127109
}
128110

129111

@@ -167,9 +149,13 @@ t_sensors *retrieve_sensors()
167149
sprintf(hwmon_path, "%s%d", path_begin, counter);
168150

169151
// thanks http://stackoverflow.com/questions/18192998/plain-c-opening-a-directory-with-fopen
170-
fopen(hwmon_path, "wb");
152+
FILE *file = fopen(hwmon_path, "rb");
153+
int isdir = file == NULL && errno == EISDIR;
154+
if (file != NULL) {
155+
fclose(file);
156+
}
171157

172-
if (errno == EISDIR) {
158+
if (isdir) {
173159

174160
path_begin = smprintf("%s/temp", hwmon_path);
175161

0 commit comments

Comments
 (0)