Minor update of the configuration file parsing, and some cosmetics to make the code...
authorFrancois Fleuret <francois@fleuret.org>
Sat, 31 Oct 2015 13:49:06 +0000 (14:49 +0100)
committerFrancois Fleuret <francois@fleuret.org>
Sat, 31 Oct 2015 13:49:06 +0000 (14:49 +0100)
Makefile
breezed.c
breezed_Lenovo_T450s.conf

index 54a818e..2a4b9e4 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -22,12 +22,12 @@ BINARY_PATH = $(DESTDIR)/usr/bin
 MAN_PATH = $(DESTDIR)/usr/share/man/man1
 PM_SLEEPD = $(DESTDIR)/usr/lib/pm-utils/sleep.d
 
-CFLAGS = -Wall -ansi -pedantic $(OPTIMIZE_FLAG)
+CFLAGS = -Wall -D_GNU_SOURCE -ansi -pedantic $(OPTIMIZE_FLAG)
 
 all: breezed
 
 breezed: breezed.c
-       $(CC) -o $@ $^ $(LDFLAGS)
+       $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
 
 install: breezed $(DESTDIR)/etc/breezed.conf
        install -m 755 breezed $(BINARY_PATH)
index dd360c1..1dc7357 100644 (file)
--- a/breezed.c
+++ b/breezed.c
 #include <errno.h>
 #include <unistd.h>
 #include <string.h>
+#include <string.h>
 
 const int major_version_number = 1;
-const int minor_version_number = 4;
+const int minor_version_number = 5;
 
-const int buffer_size = 1024;
+#define BUFFER_SIZE 1024
 
 const char *default_configuration_file = "/etc/breezed.conf";
 
@@ -111,7 +112,7 @@ char *next_word(char *buffer, char *r, int buffer_size) {
 }
 
 void set_fan_level(int fan_fd, int f, int max_fan_level) {
-  char buffer[buffer_size];
+  char buffer[BUFFER_SIZE];
   if(f < 0 || f > max_fan_level) f = max_fan_level;
   sprintf(buffer, "level %s\n", speed_names[f]);
   if(write(fan_fd, buffer, strlen(buffer)) < 0) {
@@ -122,33 +123,36 @@ void set_fan_level(int fan_fd, int f, int max_fan_level) {
 }
 
 void define_thermal_files(char *definition) {
-  char token[buffer_size];
+  char token[BUFFER_SIZE];
+  char *s;
+  int k;
 
   if(file_thermal) {
     fprintf(stderr, "Thermal files already defined.\n");
     exit(EXIT_FAILURE);
   }
 
-  char *s;
   s = definition;
   while(s) {
-    s = next_word(token, s, buffer_size);
+    s = next_word(token, s, BUFFER_SIZE);
     nb_file_thermal++;
   }
 
   file_thermal = safe_malloc(nb_file_thermal * sizeof(char *));
   file_thermal_fd = safe_malloc(nb_file_thermal * sizeof(int));
   s = definition;
-  int k = 0;
+  k = 0;
   while(s) {
-    s = next_word(token, s, buffer_size);
+    s = next_word(token, s, BUFFER_SIZE);
     file_thermal[k] = strdup(token);
     k++;
   }
 }
 
 void define_temperature_thresholds(char *definition) {
-  char token[buffer_size];
+  char token[BUFFER_SIZE];
+  char *s, *u;
+  int k;
 
   if(temperature_thresholds) {
     fprintf(stderr, "Temperature thresholds already defined.\n");
@@ -157,11 +161,9 @@ void define_temperature_thresholds(char *definition) {
 
   nb_temperature_thresholds = 0;
 
-  char *s, *u;
-
   s = definition;
   while(s) {
-    s = next_word(token, s, buffer_size);
+    s = next_word(token, s, BUFFER_SIZE);
     nb_temperature_thresholds++;
   }
 
@@ -172,9 +174,9 @@ void define_temperature_thresholds(char *definition) {
     safe_malloc(nb_temperature_thresholds * sizeof(char *));
 
   s = definition;
-  int k = 0;
+  k = 0;
   while(s) {
-    s = next_word(token, s, buffer_size);
+    s = next_word(token, s, BUFFER_SIZE);
     u = token;
     while(*u && *u != ':') { u++; }
     if(*u) {
@@ -184,7 +186,7 @@ void define_temperature_thresholds(char *definition) {
       speed_names[k] = strdup(u);
     } else {
       temperature_thresholds[k] = atoi(token);
-      snprintf(token, buffer_size, "%d", k);
+      snprintf(token, BUFFER_SIZE, "%d", k);
       speed_names[k] = strdup(token);
     }
 
@@ -209,10 +211,10 @@ void define_temperature_thresholds(char *definition) {
 }
 
 void evaluate_one_configuration_line(char *line, int line_number) {
-  char token[buffer_size];
+  char token[BUFFER_SIZE];
   char *s;
 
-  s = next_word(token, line, buffer_size);
+  s = next_word(token, line, BUFFER_SIZE);
 
   if(strcmp(token, "thermal_files") == 0) {
     if(s == 0) {
@@ -259,7 +261,7 @@ void evaluate_one_configuration_line(char *line, int line_number) {
 /******************************************************************/
 
 int main(int argc, char **argv) {
-  char buffer[buffer_size];
+  char buffer[BUFFER_SIZE];
   int i, t;
 
   configuration_file = strdup(default_configuration_file);
@@ -275,7 +277,7 @@ int main(int argc, char **argv) {
     else if(strcmp(argv[i], "--version") == 0 || strcmp(argv[i], "-v") == 0) {
       printf("Breezed v%d.%d. Written by Francois Fleuret (francois@fleuret.org).\n",
              major_version_number, minor_version_number);
-      exit(0);
+      exit(EXIT_SUCCESS);
     }
 
     else if(strcmp(argv[i], "--no-configuration-file") == 0 ||
@@ -342,47 +344,48 @@ int main(int argc, char **argv) {
 
     else if(strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) {
 
-      printf("%s [--version|-v] [--help|-h] [--debug|-d]\\\n\
-  [--configuration-file|-cf <file>]\\\n\
-  [--no-configuration-file|-ncf]\\\n\
+      printf("%s [--version|-v] [--help|-h] [--debug|-d] \\\n\
+  [--configuration-file|-cf <file>] \\\n\
+  [--no-configuration-file|-ncf] \\\n\
   [--thermal-files|-tf <thermalfile1,thermalfile2,...>] \\\n\
   [--fanfile|-ff <fan file>] \\\n\
   [--temperature-thresholds|-tt <t1,t2,t3,t4,...>]\n\
-\n\
-  --help|-h :                    shows this help\n\
-  --version|-v :                 shows the version number\n\
-  --debug|-d :                   prints out additional information\n\
+\n",
+             argv[0]);
+
+      printf("  --help|-h                      shows this help\n\
+  --version|-v                   shows the version number\n\
+  --debug|-d                     prints out additional information\n\
   --configuration-file|-cf       sets the configuration file\n\
   --no-configuration-file|-ncf   do not load a configuration file\n\
-  --thermal-files|-tf :          sets where to look for temperatures\n\
-  --fanfile|-ff :                sets where to control the fan level\n\
-  --temperature-thresholds|-tt : sets the temperature thresholds\n\
-\n\
-This daemon polls the temperatures every 5s, takes the max and sets\n\
+  --thermal-files|-tf            sets where to look for temperatures\n\
+  --fanfile|-ff                  sets where to control the fan level\n\
+  --temperature-thresholds|-tt   sets the temperature thresholds\n\n");
+
+      printf("This daemon polls the temperatures every 5s, takes the max and sets\n\
 the fan level accordingly. It uses as temperatures all the numbers it\n\
 finds in the provided \"thermal files\". Hence you can use as well\n\
-/proc/acpi/thermal_zone/THM*/temperature or /proc/acpi/ibm/thermal.\n\
-\n\
-The fan speed is set by echoing into the provided fan file.\n\
+/proc/acpi/thermal_zone/THM*/temperature or /proc/acpi/ibm/thermal.\n\n");
+
+      printf("The fan speed is set by echoing into the provided fan file.\n\
 \n\
 To reduce oscillations, it will not reduce the fan speed less than 30s\n\
 after the last previous change and the thresholds actually used to\n\
 reduce the fan speed are two degrees lower than the provided\n\
-thresholds, which are used to increase the fan speed.\n\
+thresholds, which are used to increase the fan speed.\n\n");
+
+      printf("This daemon should be started through the adequate shell script in\n\
 \n\
-This daemon should be started through the adequate shell script in\n\
-/etc/init.d.\n\
+  /etc/init.d.\n\
 \n\
 Options can be set either in the configuration file, or on the \n\
 command line. Options can not be set twice.\n\
 \n\
-Version %d.%d, November 2009.\n\
-\n\
+Version %d.%d, November 2015\n\
 Written by Francois Fleuret (francois@fleuret.org).\n",
-             argv[0],
              major_version_number, minor_version_number);
 
-      exit(0);
+      exit(EXIT_SUCCESS);
     }
 
     else {
@@ -395,9 +398,11 @@ Written by Francois Fleuret (francois@fleuret.org).\n",
   /******************************************************************/
 
   if(configuration_file) {
-    char raw_line[buffer_size];
-    int start, end, eol, k;
+    char raw_line[BUFFER_SIZE];
     FILE *file;
+    int line_number = 0;
+    char *start = raw_line;
+    char *s;
 
     file = fopen(configuration_file, "r");
 
@@ -406,65 +411,29 @@ Written by Francois Fleuret (francois@fleuret.org).\n",
       exit(EXIT_FAILURE);
     }
 
-    start = 0;
-    end = 0;
-
-    char *s;
-
-    int line_number = 0;
-
-    while(end > start || !feof(file)) {
-      eol = start;
-
-      /* Look for the end of a line in what is already in the buffer */
-      while(eol < end && raw_line[eol] != '\n') eol++;
-
-      /* if we did not find the of a line, move what has not been
-         processed and is in the buffer to the beginning of the buffer,
-         fill the buffer with new data from the file, and look for the
-         end of a line */
-      if(eol == end) {
-        for(k = 0; k < end - start; k++) {
-          raw_line[k] = raw_line[k + start];
-        }
-        end -= start;
-        eol -= start;
-        start = 0;
-        end += fread(raw_line + end, sizeof(char), buffer_size - end, file);
-        while(eol < end && raw_line[eol] != '\n') eol++;
-      }
+    while(fgets(start, raw_line + BUFFER_SIZE - start, file)) {
+      line_number++;
 
-      /* The end of the line is the buffer size, which means the line is
-         too long */
+      /* Go to the end of the line */
+      for(s = start; s < raw_line + BUFFER_SIZE && *s; s++);
 
-      if(eol == buffer_size) {
-        raw_line[buffer_size - 1] = '\0';
-        fprintf(stderr, "Selector: Line too long (max is %d characters):\n",
-                buffer_size);
-        fprintf(stderr, raw_line);
-        fprintf(stderr, "\n");
+      if(s == raw_line + BUFFER_SIZE - 1) {
+        fprintf(stderr, "Line too long at %s:%d.\n", configuration_file, line_number);
         exit(EXIT_FAILURE);
       }
 
-      /* If we got a line, we replace the carriage return by a \0 to
-         finish the string */
-
-      raw_line[eol] = '\0';
+      /* Trim the CR */
+      if(s > start && *(s-1) == '\n') { s--; *s = '\0'; }
 
-      /* here we process the line */
-
-      line_number++;
-
-      if(debug) {
-        printf("%s:%d \"%s\"\n",
-               configuration_file, line_number, raw_line + start);
+      /* Allows line continuation */
+      if(s == start || *(s-1) != '\\') {
+        /* printf("%d: \"%s\"\n", line_number, raw_line); */
+        evaluate_one_configuration_line(raw_line, line_number);
+        start = raw_line;
+      } else {
+        start = s-1;
       }
-
-      evaluate_one_configuration_line(raw_line + start, line_number);
-
-      start = eol + 1;
     }
-
   }
 
   /******************************************************************/
@@ -521,7 +490,12 @@ Written by Francois Fleuret (francois@fleuret.org).\n",
 
   while(1) {
 
-    int temperature = -1;
+    int new_level;
+    int new_level_up;
+    int size, temperature;
+    char *s;
+
+    temperature = -1;
 
     if(debug) {
       printf("Temperature:");
@@ -529,15 +503,15 @@ Written by Francois Fleuret (francois@fleuret.org).\n",
 
     for(i = 0; i < nb_file_thermal; i++) {
       lseek(file_thermal_fd[i], 0, SEEK_SET);
-      int size = read(file_thermal_fd[i], buffer, buffer_size);
+      size = read(file_thermal_fd[i], buffer, BUFFER_SIZE);
 
-      if(size > 0 && size < buffer_size) {
+      if(size > 0 && size < BUFFER_SIZE) {
         buffer[size] = '\0';
 
-        char *s = buffer;
+        s = buffer;
 
         while(*s) {
-          while(*s && *s != '-' && (*s < '0') || (*s > '9')) s++;
+          while(*s && (*s != '-') && ((*s < '0') || (*s > '9'))) s++;
 
           if(*s) {
             int t = 0, sign = 1;
@@ -572,7 +546,7 @@ Written by Francois Fleuret (francois@fleuret.org).\n",
         }
       } else {
         if(size == 0) {
-          fprintf(stderr, "Nothing to read in %d.\n", file_thermal[i]);
+          fprintf(stderr, "Nothing to read in %s.\n", file_thermal[i]);
         } else if(size < 0) {
           fprintf(stderr, "Error while reading %s (%s).\n",
                   file_thermal[i], strerror(errno));
@@ -591,9 +565,8 @@ Written by Francois Fleuret (francois@fleuret.org).\n",
 
     nb_rounds_since_last_change++;
 
-    int new_level = last_level;
-
-    int new_level_up = nb_temperature_thresholds - 1;
+    new_level = last_level;
+    new_level_up = nb_temperature_thresholds - 1;
     while(new_level_up > 0 &&
           temperature < temperature_thresholds[new_level_up]) {
       new_level_up--;
index ac3844d..315389f 100644 (file)
@@ -4,7 +4,12 @@
 
 # This file is distributed without ANY WARRANTY of any kind.
 
-thermal_files /sys/class/thermal/thermal_zone0/temp,/sys/class/thermal/thermal_zone1/temp
+#thermal_files /sys/class/thermal/thermal_zone0/temp,/sys/class/thermal/thermal_zone1/temp
+
+thermal_files /sys/class/hwmon/hwmon0/temp1_input,\
+              /sys/class/hwmon/hwmon2/temp1_input,\
+              /sys/class/hwmon/hwmon2/temp2_input,\
+              /sys/class/hwmon/hwmon2/temp3_input
 
 fan_file /proc/acpi/ibm/fan