Skip to content

Commit 4a3ea93

Browse files
committed
Correctly detect legacy or modern sensors
Tighten check by looking for an actual sensor instead of a directory. Fixes #162. Fixes regression from 885a084.
1 parent d717ffb commit 4a3ea93

1 file changed

Lines changed: 13 additions & 22 deletions

File tree

src/mbpfan.c

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ static char *smprintf(const char *fmt, ...)
8686
return buf;
8787
}
8888

89-
bool is_legacy_sensors_path()
89+
bool is_modern_sensors_path()
9090
{
9191
struct utsname kernel;
9292
uname(&kernel);
@@ -100,25 +100,21 @@ bool is_legacy_sensors_path()
100100
exit(EXIT_FAILURE);
101101
}
102102

103-
const char *path_begin = "/sys/devices/platform/coretemp.0/hwmon/hwmon";
104103
int counter;
105104

106105
for (counter = 0; counter < 10; counter++) {
107-
char hwmon_path[strlen(path_begin)+2];
108-
sprintf(hwmon_path, "%s%d", path_begin, counter);
109-
110-
FILE *file = fopen(hwmon_path, "rb");
111-
int isdir = file == NULL && errno == EISDIR;
112-
if (file != NULL) {
113-
fclose(file);
114-
}
115-
116-
if (isdir) {
117-
return 0;
106+
int temp;
107+
for (temp = 1; temp < 10; ++temp) {
108+
char *path = smprintf("/sys/devices/platform/coretemp.0/hwmon/hwmon%d/temp%d_input", counter, temp);
109+
int res = access(path, R_OK);
110+
free(path);
111+
if (res == 0) {
112+
return 1;
113+
}
118114
}
119115
}
120116

121-
return 1;
117+
return 0;
122118
}
123119

124120

@@ -131,7 +127,7 @@ t_sensors *retrieve_sensors()
131127
char *path = NULL;
132128
char *path_begin = NULL;
133129

134-
if (is_legacy_sensors_path()) {
130+
if (!is_modern_sensors_path()) {
135131
if(verbose) {
136132
printf("Using legacy sensor path for kernel < 3.15.0\n");
137133

@@ -161,13 +157,8 @@ t_sensors *retrieve_sensors()
161157

162158
sprintf(hwmon_path, "%s%d", path_begin, counter);
163159

164-
FILE *file = fopen(hwmon_path, "rb");
165-
int isdir = file == NULL && errno == EISDIR;
166-
if (file != NULL) {
167-
fclose(file);
168-
}
169-
170-
if (isdir) {
160+
int res = access(hwmon_path, R_OK);
161+
if (res == 0) {
171162

172163
free(path_begin);
173164
path_begin = smprintf("%s/temp", hwmon_path);

0 commit comments

Comments
 (0)