Skip to content

Commit dac0874

Browse files
committed
Correctly test directory on Linux 3.13
Previously the fopen write mode caused the call to spuriously fail. Also plug file descriptor leak and more carefully check return values.
1 parent 5f2da79 commit dac0874

2 files changed

Lines changed: 12 additions & 8 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ This section reports those models where mbpfan was tested successfully. It does
4646
- MacBook Pro 6,2 15" (Intel i7 - Linux 3.5.0)
4747
- MacBook Pro 6,2 15" (Intel i7 - Linux 3.2.0)
4848
- MacBook Pro 2,2 15" (Intel Core 2 Duo - Linux 3.4.4)
49+
- MacBook Air 6,1 13" (Intel i7 - Linux 3.13)
4950
- MacBook Air 5,2 13" (Intel i5 - Linux 3.16)
5051
- MacBook Air 1,1 13" (Intel Core Duo - Linux 4.4, Linux 4.8)
5152
- MacBook Air 7,2 13" (Intel Core Duo - Linux 4.10)

src/mbpfan.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,12 @@ bool is_legacy_sensors_path()
7777

7878

7979
// thanks http://stackoverflow.com/questions/18192998/plain-c-opening-a-directory-with-fopen
80-
fopen("/sys/devices/platform/coretemp.0/hwmon", "wb");
81-
82-
if (errno == EISDIR) {
83-
return 0;
84-
} else {
85-
return 1;
80+
FILE *file = fopen("/sys/devices/platform/coretemp.0/hwmon", "rb");
81+
int isdir = file == NULL && errno == EISDIR;
82+
if (file != NULL) {
83+
fclose(file);
8684
}
85+
return !isdir;
8786

8887
//
8988
// str_kernel_version = strtok(NULL, ".");
@@ -143,9 +142,13 @@ t_sensors *retrieve_sensors()
143142
sprintf(hwmon_path, "%s%d", path_begin, counter);
144143

145144
// thanks http://stackoverflow.com/questions/18192998/plain-c-opening-a-directory-with-fopen
146-
fopen(hwmon_path, "wb");
145+
FILE *file = fopen(hwmon_path, "rb");
146+
int isdir = file == NULL && errno == EISDIR;
147+
if (file != NULL) {
148+
fclose(file);
149+
}
147150

148-
if (errno == EISDIR) {
151+
if (isdir) {
149152

150153
path_begin = (char*) malloc(sizeof( char ) * (strlen(hwmon_path) + strlen("/temp") + 1));
151154
strcpy(path_begin, hwmon_path);

0 commit comments

Comments
 (0)