X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?p=breezed.git;a=blobdiff_plain;f=breezed.c;h=6c4895b09ac637afea90d89c41a132628b49216b;hp=5d57861f3c04960c865621ddac4f4f547f3a4fa9;hb=269a286192181e8357e5b8ccbb126bdb1e1af6de;hpb=31a2f609ec040429706d3a603d438683f205eb82 diff --git a/breezed.c b/breezed.c index 5d57861..6c4895b 100644 --- a/breezed.c +++ b/breezed.c @@ -3,7 +3,7 @@ breezed is a fan speed control daemon for Linux computers. - Copyright (c) 2008, 2009 Francois Fleuret + Copyright (c) 2008, 2009, 2010 Francois Fleuret Written by Francois Fleuret This file is part of breezed. @@ -30,7 +30,7 @@ #include const int major_version_number = 1; -const int minor_version_number = 3; +const int minor_version_number = 4; const int buffer_size = 1024; @@ -56,6 +56,7 @@ int file_fan_fd; int nb_temperature_thresholds; int *temperature_thresholds = 0; +char **speed_names = 0; int nb_file_thermal = 0; char **file_thermal = 0; @@ -63,6 +64,19 @@ int *file_thermal_fd = 0; char *configuration_file; +/********************************************************************/ + +/* malloc with error checking. */ + +void *safe_malloc(size_t n) { + void *p = malloc(n); + if (!p && n != 0) { + fprintf(stderr, "Can not allocate memory: %s\n", strerror(errno)); + exit(EXIT_FAILURE); + } + return p; +} + /******************************************************************/ char *next_word(char *buffer, char *r, int buffer_size) { @@ -99,7 +113,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]; if(f < 0 || f > max_fan_level) f = max_fan_level; - sprintf(buffer, "level %d\n", f); + sprintf(buffer, "level %s\n", speed_names[f]); if(write(fan_fd, buffer, strlen(buffer)) < 0) { fprintf(stderr, "Error in setting the fan level (%s).\n", strerror(errno)); @@ -122,8 +136,8 @@ void define_thermal_files(char *definition) { nb_file_thermal++; } - file_thermal = (char **) malloc(nb_file_thermal * sizeof(char *)); - file_thermal_fd = (int *) malloc(nb_file_thermal * sizeof(int)); + file_thermal = safe_malloc(nb_file_thermal * sizeof(char *)); + file_thermal_fd = safe_malloc(nb_file_thermal * sizeof(int)); s = definition; int k = 0; while(s) { @@ -141,9 +155,10 @@ void define_temperature_thresholds(char *definition) { exit(EXIT_FAILURE); } - nb_temperature_thresholds = 1; + nb_temperature_thresholds = 0; + + char *s, *u; - char *s; s = definition; while(s) { s = next_word(token, s, buffer_size); @@ -151,22 +166,46 @@ void define_temperature_thresholds(char *definition) { } temperature_thresholds = - (int *) malloc(nb_temperature_thresholds * sizeof(int)); + safe_malloc(nb_temperature_thresholds * sizeof(int)); - temperature_thresholds[0] = -1; + speed_names = + safe_malloc(nb_temperature_thresholds * sizeof(char *)); s = definition; - int k = 1; + int k = 0; while(s) { s = next_word(token, s, buffer_size); - temperature_thresholds[k] = atoi(token); + u = token; + while(*u && *u != ':') { u++; } + if(*u) { + *u = '\0'; + temperature_thresholds[k] = atoi(token); + u++; + speed_names[k] = strdup(u); + } else { + temperature_thresholds[k] = atoi(token); + snprintf(token, buffer_size, "%d", k); + speed_names[k] = strdup(token); + } + if(k > 0 && temperature_thresholds[k] < temperature_thresholds[k-1]) { fprintf(stderr, "The temperature thresholds have to be increasing.\n"); - exit(0); + exit(EXIT_FAILURE); } k++; } + + if(nb_temperature_thresholds <= 0) { + fprintf(stderr, "There has to be at least one temperature.\n"); + exit(EXIT_FAILURE); + } + + if(temperature_thresholds[0] > 0) { + fprintf(stderr, "The lowest temperature has to be 0.\n"); + exit(EXIT_FAILURE); + } + } void evaluate_one_configuration_line(char *line, int line_number) { @@ -472,8 +511,9 @@ Written by Francois Fleuret (francois@fleuret.org).\n", printf("file_fan %s\n", file_fan); for(t = 0; t < nb_temperature_thresholds; t++) { - printf("temperature_thresholds[%d] %d", - t, temperature_thresholds[t]); + printf("temperature_thresholds[%d] = %d speed_names[%d] = \"%s\"\n", + t, temperature_thresholds[t], + t, speed_names[t]); } } @@ -584,8 +624,10 @@ Written by Francois Fleuret (francois@fleuret.org).\n", nb_rounds_since_last_change = 0; last_level = new_level; if(debug) { - printf("Temperature is %dC setting the fan level to %d.", - temperature, new_level); + printf("Temperature is %dC setting the fan level to %d (%s).\n", + temperature, + new_level, + speed_names[new_level]); } }