diff --git a/src/daemon.c b/src/daemon.c
index 0ec8ead..0a5dbb4 100644
--- a/src/daemon.c
+++ b/src/daemon.c
@@ -82,6 +82,9 @@ static void cleanup_and_exit(int exit_code)
struct s_fans *next_fan;
while (fans != NULL) {
next_fan = fans->next;
+ if (fans->file != NULL) {
+ fclose(fans->file);
+ }
free(fans->fan_output_path);
free(fans->fan_manual_path);
free(fans);
@@ -91,6 +94,9 @@ static void cleanup_and_exit(int exit_code)
struct s_sensors *next_sensor;
while (sensors != NULL) {
next_sensor = sensors->next;
+ if (sensors->file != NULL) {
+ fclose(sensors->file);
+ }
free(sensors->path);
free(sensors);
sensors = next_sensor;
diff --git a/src/global.h b/src/global.h
index 9b64acf..19db7c1 100644
--- a/src/global.h
+++ b/src/global.h
@@ -8,13 +8,15 @@ extern const char* PROGRAM_NAME;
extern const char* PROGRAM_PID;
struct s_sensors {
+ FILE* file;
char* path;
unsigned int temperature;
struct s_sensors *next;
};
struct s_fans {
- char* path;
+ FILE* file;
+ char* path; // TODO: unused
char* fan_output_path;
char* fan_manual_path;
struct s_fans *next;
diff --git a/src/mbpfan.c b/src/mbpfan.c
index a9cd563..b090087 100644
--- a/src/mbpfan.c
+++ b/src/mbpfan.c
@@ -206,7 +206,7 @@ t_sensors *retrieve_sensors()
tmp->next->next = NULL;
}
- fclose(file);
+ s->file = file;
sensors_found++;
}
@@ -273,7 +273,7 @@ t_fans *retrieve_fans()
strncat( path_manual, path_man_end, strlen(path_begin) );
- FILE *file = fopen(path_output, "r");
+ FILE *file = fopen(path_output, "w");
if(file != NULL) {
fan = (t_fans *) malloc( sizeof( t_fans ) );
@@ -297,7 +297,7 @@ t_fans *retrieve_fans()
tmp->next->next = NULL;
}
- fclose(file);
+ fan->file = file;
fans_found++;
}
@@ -362,11 +362,11 @@ t_sensors *refresh_sensors(t_sensors *sensors)
t_sensors *tmp = sensors;
while(tmp != NULL) {
- FILE *file = fopen(tmp->path, "r");
-
- if(file != NULL) {
- fscanf(file, "%d", &tmp->temperature);
- fclose(file);
+ if(tmp->file != NULL) {
+ char buf[16];
+ int len = pread(fileno(tmp->file), buf, sizeof(buf), /*offset=*/ 0);
+ buf[len] = '\0';
+ sscanf(buf, "%d", &tmp->temperature);
}
tmp = tmp->next;
@@ -380,14 +380,12 @@ t_sensors *refresh_sensors(t_sensors *sensors)
void set_fan_speed(t_fans* fans, int speed)
{
t_fans *tmp = fans;
- FILE *file;
while(tmp != NULL) {
- file = fopen(tmp->fan_output_path, "rw+");
-
- if(file != NULL) {
- fprintf(file, "%d", speed);
- fclose(file);
+ if(tmp->file != NULL) {
+ char buf[16];
+ int len = snprintf(buf, sizeof(buf), "%d", speed);
+ pwrite(fileno(tmp->file), buf, len, /*offset=*/ 0);
}
tmp = tmp->next;
Do not repeatedly reopen fan and sensor devices
This avoids unnecessary system calls and saves battery.
399aad782a2daf478ab3801e1fc12f8f23ec1ab6
Andrew Gaul <andrew@gaul.org> · 2017-07-13T15:28 ·
browse files at this commit
parents:
5f2da79