Reformat source code with clang-tidy

Run with:

clang-format -i src/*

Closes #233.
This commit is contained in:
Andrew Gaul 2021-04-12 22:05:15 +09:00
parent 16f28f900c
commit 623f44d0a5
12 changed files with 253 additions and 271 deletions

View file

@ -16,7 +16,6 @@
* *
*/ */
#include <sys/prctl.h> #include <sys/prctl.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -43,7 +42,7 @@ int write_pid(int pid)
FILE *file = NULL; FILE *file = NULL;
file = fopen(PROGRAM_PID, "w"); file = fopen(PROGRAM_PID, "w");
if(file != NULL) { if (file != NULL) {
fprintf(file, "%d", pid); fprintf(file, "%d", pid);
fclose(file); fclose(file);
return 1; return 1;
@ -59,14 +58,13 @@ int read_pid()
int pid = -1; int pid = -1;
file = fopen(PROGRAM_PID, "r"); file = fopen(PROGRAM_PID, "r");
if(file != NULL) { if (file != NULL) {
fscanf(file, "%d", &pid); fscanf(file, "%d", &pid);
fclose(file); fclose(file);
if (kill(pid, 0) == -1 && errno == ESRCH) if (kill(pid, 0) == -1 && errno == ESRCH) { /* a process with such a pid does not exist, remove the pid file */
{ /* a process with such a pid does not exist, remove the pid file */ if (remove(PROGRAM_PID) == 0) {
if (remove(PROGRAM_PID) == 0) { return -1;
return -1; }
}
} }
return pid; return pid;
} }
@ -81,40 +79,40 @@ int delete_pid()
static void cleanup_and_exit(int exit_code) static void cleanup_and_exit(int exit_code)
{ {
delete_pid(); delete_pid();
set_fans_auto(fans); set_fans_auto(fans);
struct s_fans *next_fan; struct s_fans *next_fan;
while (fans != NULL) { while (fans != NULL) {
next_fan = fans->next; next_fan = fans->next;
if (fans->file != NULL) { if (fans->file != NULL) {
fclose(fans->file); fclose(fans->file);
} }
free(fans->label); free(fans->label);
free(fans->fan_output_path); free(fans->fan_output_path);
free(fans->fan_manual_path); free(fans->fan_manual_path);
free(fans); free(fans);
fans = next_fan; fans = next_fan;
} }
struct s_sensors *next_sensor; struct s_sensors *next_sensor;
while (sensors != NULL) { while (sensors != NULL) {
next_sensor = sensors->next; next_sensor = sensors->next;
if (sensors->file != NULL) { if (sensors->file != NULL) {
fclose(sensors->file); fclose(sensors->file);
} }
free(sensors->path); free(sensors->path);
free(sensors); free(sensors);
sensors = next_sensor; sensors = next_sensor;
} }
exit(exit_code); exit(exit_code);
} }
void signal_handler(int signal) void signal_handler(int signal)
{ {
switch(signal) { switch (signal) {
case SIGHUP: case SIGHUP:
syslog(LOG_WARNING, "Received SIGHUP signal."); syslog(LOG_WARNING, "Received SIGHUP signal.");
retrieve_settings(NULL, fans); retrieve_settings(NULL, fans);
@ -151,7 +149,7 @@ void go_daemon(void (*fan_control)())
signal(SIGINT, signal_handler); signal(SIGINT, signal_handler);
// Setup syslog logging - see SETLOGMASK(3) // Setup syslog logging - see SETLOGMASK(3)
if(verbose) { if (verbose) {
setlogmask(LOG_UPTO(LOG_DEBUG)); setlogmask(LOG_UPTO(LOG_DEBUG));
openlog(PROGRAM_NAME, LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER); openlog(PROGRAM_NAME, LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER);
@ -198,15 +196,12 @@ void go_daemon(void (*fan_control)())
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
/* Close out the standard file descriptors */ /* Close out the standard file descriptors */
close(STDIN_FILENO); close(STDIN_FILENO);
close(STDOUT_FILENO); close(STDOUT_FILENO);
close(STDERR_FILENO); close(STDERR_FILENO);
} }
int current_pid = getpid(); int current_pid = getpid();
if (read_pid() == -1) { if (read_pid() == -1) {
@ -229,10 +224,9 @@ void go_daemon(void (*fan_control)())
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
fan_control(); fan_control();
if(daemonize) { if (daemonize) {
syslog(LOG_INFO, "%s daemon exiting", PROGRAM_NAME); syslog(LOG_INFO, "%s daemon exiting", PROGRAM_NAME);
} }
} }

View file

@ -50,5 +50,4 @@ void signal_handler(int signal);
*/ */
void go_daemon(void (*function)()); void go_daemon(void (*function)());
#endif #endif

View file

@ -9,18 +9,18 @@ extern int daemonize;
extern int verbose; extern int verbose;
struct s_sensors { struct s_sensors {
FILE* file; FILE *file;
char* path; char *path;
unsigned int temperature; unsigned int temperature;
struct s_sensors *next; struct s_sensors *next;
}; };
struct s_fans { struct s_fans {
FILE* file; FILE *file;
char* path; // TODO: unused char *path; // TODO: unused
char* label; char *label;
char* fan_output_path; char *fan_output_path;
char* fan_manual_path; char *fan_manual_path;
int step_up; int step_up;
int step_down; int step_down;
int fan_id; int fan_id;
@ -33,7 +33,7 @@ struct s_fans {
typedef struct s_sensors t_sensors; typedef struct s_sensors t_sensors;
typedef struct s_fans t_fans; typedef struct s_fans t_fans;
extern t_sensors* sensors; extern t_sensors *sensors;
extern t_fans* fans; extern t_fans *fans;
#endif #endif

View file

@ -31,7 +31,7 @@
void print_usage(int argc, char *argv[]) void print_usage(int argc, char *argv[])
{ {
if (argc >=1) { if (argc >= 1) {
printf("Usage: %s OPTION(S) \n", argv[0]); printf("Usage: %s OPTION(S) \n", argv[0]);
printf("Options:\n"); printf("Options:\n");
printf("\t-h Show this help screen\n"); printf("\t-h Show this help screen\n");
@ -46,8 +46,8 @@ int main(int argc, char *argv[])
int c; int c;
while( (c = getopt(argc, argv, "hfv|help")) != -1) { while ((c = getopt(argc, argv, "hfv|help")) != -1) {
switch(c) { switch (c) {
case 'h': case 'h':
print_usage(argc, argv); print_usage(argc, argv);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);

View file

@ -47,8 +47,8 @@
#include "util.h" #include "util.h"
/* lazy min/max... */ /* lazy min/max... */
#define min(a,b) ((a) < (b) ? (a) : (b)) #define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a,b) ((a) > (b) ? (a) : (b)) #define max(a, b) ((a) > (b) ? (a) : (b))
#define CORETEMP_PATH "/sys/devices/platform/coretemp.0" #define CORETEMP_PATH "/sys/devices/platform/coretemp.0"
#define APPLESMC_PATH "/sys/devices/platform/applesmc.768" #define APPLESMC_PATH "/sys/devices/platform/applesmc.768"
@ -57,9 +57,9 @@
* low_temp - temperature below which fan speed will be at minimum * low_temp - temperature below which fan speed will be at minimum
* high_temp - fan will increase speed when higher than this temperature * high_temp - fan will increase speed when higher than this temperature
* max_temp - fan will run at full speed above this temperature */ * max_temp - fan will run at full speed above this temperature */
int low_temp = 63; // try ranges 55-63 int low_temp = 63; // try ranges 55-63
int high_temp = 66; // try ranges 58-66 int high_temp = 66; // try ranges 58-66
int max_temp = 86; // do not set it > 90 int max_temp = 86; // do not set it > 90
// maximum number of processors etc supported // maximum number of processors etc supported
#define NUM_PROCESSORS 6 #define NUM_PROCESSORS 6
@ -72,8 +72,8 @@ int max_temp = 86; // do not set it > 90
int polling_interval = 1; int polling_interval = 1;
t_sensors* sensors = NULL; t_sensors *sensors = NULL;
t_fans* fans = NULL; t_fans *fans = NULL;
char *smprintf(const char *fmt, ...) char *smprintf(const char *fmt, ...)
{ {
@ -105,7 +105,7 @@ bool is_modern_sensors_path()
char *str_kernel_version; char *str_kernel_version;
str_kernel_version = strtok(kernel.release, "."); str_kernel_version = strtok(kernel.release, ".");
if (atoi(str_kernel_version) < 3){ if (atoi(str_kernel_version) < 3) {
mbp_log(LOG_ERR, "mbpfan detected a pre-3.x.x linux kernel. Detected version: %s. Exiting.\n", kernel.release); mbp_log(LOG_ERR, "mbpfan detected a pre-3.x.x linux kernel. Detected version: %s. Exiting.\n", kernel.release);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -127,7 +127,6 @@ bool is_modern_sensors_path()
return 0; return 0;
} }
t_sensors *retrieve_sensors() t_sensors *retrieve_sensors()
{ {
t_sensors *sensors_head = NULL; t_sensors *sensors_head = NULL;
@ -140,7 +139,7 @@ t_sensors *retrieve_sensors()
int sensors_found = 0; int sensors_found = 0;
if (!is_modern_sensors_path()) { if (!is_modern_sensors_path()) {
if(verbose) { if (verbose) {
mbp_log(LOG_INFO, "Using legacy path for kernel < 3.15.0"); mbp_log(LOG_INFO, "Using legacy path for kernel < 3.15.0");
} }
@ -148,82 +147,82 @@ t_sensors *retrieve_sensors()
} else { } else {
if(verbose) { if (verbose) {
mbp_log(LOG_INFO, "Using new sensor path for kernel >= 3.15.0 or some CentOS versions with kernel 3.10.0 "); mbp_log(LOG_INFO, "Using new sensor path for kernel >= 3.15.0 or some CentOS versions with kernel 3.10.0 ");
} }
// loop over up to 6 processors // loop over up to 6 processors
int processor; int processor;
for (processor = 0; processor < NUM_PROCESSORS; processor++) { for (processor = 0; processor < NUM_PROCESSORS; processor++) {
if (path_begin != NULL) { if (path_begin != NULL) {
free(path_begin); free(path_begin);
} }
path_begin = smprintf("/sys/devices/platform/coretemp.%d/hwmon/hwmon", processor); path_begin = smprintf("/sys/devices/platform/coretemp.%d/hwmon/hwmon", processor);
int counter; int counter;
for (counter = 0; counter < NUM_HWMONS; counter++) { for (counter = 0; counter < NUM_HWMONS; counter++) {
char *hwmon_path = smprintf("%s%d", path_begin, counter); char *hwmon_path = smprintf("%s%d", path_begin, counter);
int res = access(hwmon_path, R_OK); int res = access(hwmon_path, R_OK);
if (res == 0) { if (res == 0) {
free(path_begin); free(path_begin);
path_begin = smprintf("%s/temp", hwmon_path); path_begin = smprintf("%s/temp", hwmon_path);
if(verbose) { if (verbose) {
mbp_log(LOG_INFO, "Found hwmon path at %s", path_begin); mbp_log(LOG_INFO, "Found hwmon path at %s", path_begin);
} }
free(hwmon_path); free(hwmon_path);
break; break;
} }
free(hwmon_path); free(hwmon_path);
} }
int core = 0; int core = 0;
for(core = 0; core<NUM_TEMP_INPUTS; core++) { for (core = 0; core < NUM_TEMP_INPUTS; core++) {
path = smprintf("%s%d%s", path_begin, core, path_end); path = smprintf("%s%d%s", path_begin, core, path_end);
FILE *file = fopen(path, "r"); FILE *file = fopen(path, "r");
if(file != NULL) { if (file != NULL) {
s = (t_sensors *) malloc( sizeof( t_sensors ) ); s = (t_sensors *)malloc(sizeof(t_sensors));
s->path = strdup(path); s->path = strdup(path);
fscanf(file, "%d", &s->temperature); fscanf(file, "%d", &s->temperature);
if (sensors_head == NULL) { if (sensors_head == NULL) {
sensors_head = s; sensors_head = s;
sensors_head->next = NULL; sensors_head->next = NULL;
} else { } else {
t_sensors *tmp = sensors_head; t_sensors *tmp = sensors_head;
while (tmp->next != NULL) { while (tmp->next != NULL) {
tmp = tmp->next; tmp = tmp->next;
} }
tmp->next = s; tmp->next = s;
tmp->next->next = NULL; tmp->next->next = NULL;
} }
s->file = file; s->file = file;
sensors_found++; sensors_found++;
} }
free(path); free(path);
path = NULL; path = NULL;
} }
} }
} }
if(verbose) { if (verbose) {
mbp_log(LOG_INFO, "Found %d sensors", sensors_found); mbp_log(LOG_INFO, "Found %d sensors", sensors_found);
} }
if (sensors_found == 0){ if (sensors_found == 0) {
mbp_log(LOG_CRIT, "mbpfan could not detect any temp sensor. Please contact the developer."); mbp_log(LOG_CRIT, "mbpfan could not detect any temp sensor. Please contact the developer.");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -284,33 +283,33 @@ t_fans *retrieve_fans()
int counter = 0; int counter = 0;
int fans_found = 0; int fans_found = 0;
for(counter = 0; counter<NUM_FANS; counter++) { for (counter = 0; counter < NUM_FANS; counter++) {
path_output = smprintf("%s%d%s", path_begin, counter, path_output_end); path_output = smprintf("%s%d%s", path_begin, counter, path_output_end);
path_label = smprintf("%s%d%s", path_begin, counter, path_label_end); path_label = smprintf("%s%d%s", path_begin, counter, path_label_end);
path_manual = smprintf("%s%d%s", path_begin, counter, path_man_end); path_manual = smprintf("%s%d%s", path_begin, counter, path_man_end);
path_fan_min = smprintf("%s%d%s",path_begin, counter, path_min_speed); path_fan_min = smprintf("%s%d%s", path_begin, counter, path_min_speed);
path_fan_max = smprintf("%s%d%s",path_begin, counter, path_max_speed); path_fan_max = smprintf("%s%d%s", path_begin, counter, path_max_speed);
FILE *file = fopen(path_output, "w"); FILE *file = fopen(path_output, "w");
if(file != NULL) { if (file != NULL) {
fan = (t_fans *) malloc( sizeof( t_fans ) ); fan = (t_fans *)malloc(sizeof(t_fans));
fan->fan_output_path = strdup(path_output); fan->fan_output_path = strdup(path_output);
fan->fan_manual_path = strdup(path_manual); fan->fan_manual_path = strdup(path_manual);
fan->fan_id = counter; fan->fan_id = counter;
int fan_speed = read_value(path_fan_min); int fan_speed = read_value(path_fan_min);
if(fan_speed == -1 || fan_speed < MIN_FAN_SPEED_DEFAULT) if (fan_speed == -1 || fan_speed < MIN_FAN_SPEED_DEFAULT)
fan->fan_min_speed = MIN_FAN_SPEED_DEFAULT; fan->fan_min_speed = MIN_FAN_SPEED_DEFAULT;
else else
fan->fan_min_speed = fan_speed; fan->fan_min_speed = fan_speed;
fan_speed = read_value(path_fan_max); fan_speed = read_value(path_fan_max);
if(fan_speed == -1 || fan_speed > MAX_FAN_SPEED_DEFAULT) if (fan_speed == -1 || fan_speed > MAX_FAN_SPEED_DEFAULT)
fan->fan_max_speed = MAX_FAN_SPEED_DEFAULT; fan->fan_max_speed = MAX_FAN_SPEED_DEFAULT;
else else
fan->fan_max_speed = fan_speed; fan->fan_max_speed = fan_speed;
size_t max_label_len = 64; size_t max_label_len = 64;
fan->label = malloc(max_label_len); fan->label = malloc(max_label_len);
@ -337,23 +336,23 @@ t_fans *retrieve_fans()
fan->file = file; fan->file = file;
fans_found++; fans_found++;
} }
free(path_fan_min); free(path_fan_min);
path_fan_min = NULL; path_fan_min = NULL;
free(path_label); free(path_label);
path_label = NULL; path_label = NULL;
free(path_fan_max); free(path_fan_max);
path_fan_max = NULL; path_fan_max = NULL;
free(path_output); free(path_output);
path_output = NULL; path_output = NULL;
free(path_manual); free(path_manual);
path_manual = NULL; path_manual = NULL;
} }
if(verbose) { if (verbose) {
mbp_log(LOG_INFO, "Found %d fans", fans_found); mbp_log(LOG_INFO, "Found %d fans", fans_found);
} }
if (fans_found == 0){ if (fans_found == 0) {
mbp_log(LOG_CRIT, "mbpfan could not detect any fan. Please contact the developer."); mbp_log(LOG_CRIT, "mbpfan could not detect any fan. Please contact the developer.");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -366,10 +365,10 @@ static void set_fans_mode(t_fans *fans, int mode)
t_fans *tmp = fans; t_fans *tmp = fans;
FILE *file; FILE *file;
while(tmp != NULL) { while (tmp != NULL) {
file = fopen(tmp->fan_manual_path, "rw+"); file = fopen(tmp->fan_manual_path, "rw+");
if(file != NULL) { if (file != NULL) {
fprintf(file, "%d", mode); fprintf(file, "%d", mode);
fclose(file); fclose(file);
} }
@ -394,10 +393,10 @@ t_sensors *refresh_sensors(t_sensors *sensors)
{ {
t_sensors *tmp = sensors; t_sensors *tmp = sensors;
while(tmp != NULL) { while (tmp != NULL) {
if(tmp->file != NULL) { if (tmp->file != NULL) {
char buf[16]; char buf[16];
int len = pread(fileno(tmp->file), buf, sizeof(buf), /*offset=*/ 0); int len = pread(fileno(tmp->file), buf, sizeof(buf), /*offset=*/0);
buf[len] = '\0'; buf[len] = '\0';
tmp->temperature = strtod(buf, NULL); tmp->temperature = strtod(buf, NULL);
} }
@ -409,36 +408,36 @@ t_sensors *refresh_sensors(t_sensors *sensors)
} }
/* Controls the speed of a fan */ /* Controls the speed of a fan */
void set_fan_speed(t_fans* fan, int speed) void set_fan_speed(t_fans *fan, int speed)
{ {
if(fan != NULL && fan->file != NULL && fan->old_speed != speed) { if (fan != NULL && fan->file != NULL && fan->old_speed != speed) {
char buf[16]; char buf[16];
int len = snprintf(buf, sizeof(buf), "%d", speed); int len = snprintf(buf, sizeof(buf), "%d", speed);
int res = pwrite(fileno(fan->file), buf, len, /*offset=*/ 0); int res = pwrite(fileno(fan->file), buf, len, /*offset=*/0);
if (res == -1) { if (res == -1) {
perror("Could not set fan speed"); perror("Could not set fan speed");
} }
fan->old_speed = speed; fan->old_speed = speed;
} }
} }
void set_fan_minimum_speed(t_fans* fans) void set_fan_minimum_speed(t_fans *fans)
{ {
t_fans *tmp = fans; t_fans *tmp = fans;
while(tmp != NULL) { while (tmp != NULL) {
set_fan_speed(tmp,tmp->fan_min_speed); set_fan_speed(tmp, tmp->fan_min_speed);
tmp = tmp->next; tmp = tmp->next;
} }
} }
unsigned short get_temp(t_sensors* sensors) unsigned short get_temp(t_sensors *sensors)
{ {
sensors = refresh_sensors(sensors); sensors = refresh_sensors(sensors);
unsigned int temp = 0; unsigned int temp = 0;
t_sensors* tmp = sensors; t_sensors *tmp = sensors;
while(tmp != NULL) { while (tmp != NULL) {
temp = max(temp, tmp->temperature); temp = max(temp, tmp->temperature);
tmp = tmp->next; tmp = tmp->next;
} }
@ -446,7 +445,7 @@ unsigned short get_temp(t_sensors* sensors)
return temp / 1000; return temp / 1000;
} }
void retrieve_settings(const char* settings_path, t_fans* fans) void retrieve_settings(const char *settings_path, t_fans *fans)
{ {
Settings *settings = NULL; Settings *settings = NULL;
int result = 0; int result = 0;
@ -459,10 +458,9 @@ void retrieve_settings(const char* settings_path, t_fans* fans)
f = fopen(settings_path, "r"); f = fopen(settings_path, "r");
} }
if (f == NULL) { if (f == NULL) {
/* Could not open configfile */ /* Could not open configfile */
if(verbose) { if (verbose) {
mbp_log(LOG_INFO, "Couldn't open configfile, using defaults"); mbp_log(LOG_INFO, "Couldn't open configfile, using defaults");
} }
@ -472,34 +470,34 @@ void retrieve_settings(const char* settings_path, t_fans* fans)
if (settings == NULL) { if (settings == NULL) {
/* Could not read configfile */ /* Could not read configfile */
if(verbose) { if (verbose) {
mbp_log(LOG_WARNING, "Couldn't read configfile"); mbp_log(LOG_WARNING, "Couldn't read configfile");
} }
} else { } else {
t_fans *fan = fans; t_fans *fan = fans;
while(fan != NULL) { while (fan != NULL) {
char* config_key; char *config_key;
config_key = smprintf("min_fan%d_speed", fan->fan_id); config_key = smprintf("min_fan%d_speed", fan->fan_id);
/* Read configfile values */ /* Read configfile values */
result = settings_get_int(settings, "general", config_key); result = settings_get_int(settings, "general", config_key);
if (result != 0) { if (result != 0) {
fan->fan_min_speed = result; fan->fan_min_speed = result;
} }
free(config_key); free(config_key);
config_key = smprintf("max_fan%d_speed", fan->fan_id); config_key = smprintf("max_fan%d_speed", fan->fan_id);
result = settings_get_int(settings, "general", config_key); result = settings_get_int(settings, "general", config_key);
if (result != 0) { if (result != 0) {
fan->fan_max_speed = result; fan->fan_max_speed = result;
} }
free(config_key); free(config_key);
fan = fan->next; fan = fan->next;
} }
result = settings_get_int(settings, "general", "low_temp"); result = settings_get_int(settings, "general", "low_temp");
if (result != 0) { if (result != 0) {
@ -530,14 +528,14 @@ void retrieve_settings(const char* settings_path, t_fans* fans)
} }
} }
void check_requirements(const char* program_path) void check_requirements(const char *program_path)
{ {
/** /**
* Check for root * Check for root
*/ */
uid_t uid=getuid(), euid=geteuid(); uid_t uid = getuid(), euid = geteuid();
if (uid != 0 || euid != 0) { if (uid != 0 || euid != 0) {
mbp_log(LOG_ERR, "%s needs root privileges. Please run %s as root. Exiting.", program_path, program_path); mbp_log(LOG_ERR, "%s needs root privileges. Please run %s as root. Exiting.", program_path, program_path);
@ -545,9 +543,9 @@ void check_requirements(const char* program_path)
} }
/** /**
* Check for coretemp and applesmc modules * Check for coretemp and applesmc modules
*/ */
DIR* dir = opendir(CORETEMP_PATH); DIR *dir = opendir(CORETEMP_PATH);
if (ENOENT == errno) { if (ENOENT == errno) {
mbp_log(LOG_ERR, "%s needs coretemp support. Please either load it or build it into the kernel. Exiting.", program_path); mbp_log(LOG_ERR, "%s needs coretemp support. Please either load it or build it into the kernel. Exiting.", program_path);
@ -556,7 +554,6 @@ void check_requirements(const char* program_path)
closedir(dir); closedir(dir);
dir = opendir(APPLESMC_PATH); dir = opendir(APPLESMC_PATH);
if (ENOENT == errno) { if (ENOENT == errno) {
@ -565,8 +562,6 @@ void check_requirements(const char* program_path)
} }
closedir(dir); closedir(dir);
} }
int get_max_mhz(void) int get_max_mhz(void)
@ -578,9 +573,7 @@ int get_max_mhz(void)
} }
struct dirent *ent; struct dirent *ent;
while ((ent = readdir(dir)) != NULL) { while ((ent = readdir(dir)) != NULL) {
if (strncmp(ent->d_name, "cpu", 3) != 0 || if (strncmp(ent->d_name, "cpu", 3) != 0 || strcmp(ent->d_name, "cpufreq") == 0 || strcmp(ent->d_name, "cpuidle") == 0) {
strcmp(ent->d_name, "cpufreq") == 0 ||
strcmp(ent->d_name, "cpuidle") == 0) {
continue; continue;
} }
char *path = smprintf("/sys/devices/system/cpu/%s/cpufreq/scaling_cur_freq", ent->d_name); char *path = smprintf("/sys/devices/system/cpu/%s/cpufreq/scaling_cur_freq", ent->d_name);
@ -601,14 +594,14 @@ void mbpfan()
retrieve_settings(NULL, fans); retrieve_settings(NULL, fans);
t_fans* fan = fans; t_fans *fan = fans;
while(fan != NULL) { while (fan != NULL) {
if (fan->fan_min_speed > fan->fan_max_speed) { if (fan->fan_min_speed > fan->fan_max_speed) {
mbp_log(LOG_ERR, "Invalid fan speeds: %d %d", fan->fan_min_speed, fan->fan_max_speed); mbp_log(LOG_ERR, "Invalid fan speeds: %d %d", fan->fan_min_speed, fan->fan_max_speed);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
fan = fan->next; fan = fan->next;
} }
if (low_temp > high_temp || high_temp > max_temp) { if (low_temp > high_temp || high_temp > max_temp) {
@ -622,60 +615,58 @@ void mbpfan()
set_fan_minimum_speed(fans); set_fan_minimum_speed(fans);
fan = fans; fan = fans;
while(fan != NULL) { while (fan != NULL) {
fan->step_up = (float)( fan->fan_max_speed - fan->fan_min_speed ) / fan->step_up = (float)(fan->fan_max_speed - fan->fan_min_speed) / (float)((max_temp - high_temp) * (max_temp - high_temp + 1) / 2.0);
(float)( ( max_temp - high_temp ) * ( max_temp - high_temp + 1 ) / 2.0 );
fan->step_down = (float)( fan->fan_max_speed - fan->fan_min_speed ) / fan->step_down = (float)(fan->fan_max_speed - fan->fan_min_speed) / (float)((max_temp - low_temp) * (max_temp - low_temp + 1) / 2.0);
(float)( ( max_temp - low_temp ) * ( max_temp - low_temp + 1 ) / 2.0 ); fan = fan->next;
fan = fan->next;
} }
recalibrate: recalibrate:
if(verbose) { if (verbose) {
mbp_log(LOG_INFO, "Sleeping for 2 seconds to get first temp delta"); mbp_log(LOG_INFO, "Sleeping for 2 seconds to get first temp delta");
} }
sleep(2); sleep(2);
while(1) { while (1) {
old_temp = new_temp; old_temp = new_temp;
new_temp = get_temp(sensors); new_temp = get_temp(sensors);
fan = fans; fan = fans;
while(fan != NULL) { while (fan != NULL) {
fan_speed = fan->old_speed; fan_speed = fan->old_speed;
if(new_temp >= max_temp && fan->old_speed != fan->fan_max_speed) { if (new_temp >= max_temp && fan->old_speed != fan->fan_max_speed) {
fan_speed = fan->fan_max_speed; fan_speed = fan->fan_max_speed;
} }
if(new_temp <= low_temp && fan_speed != fan->fan_min_speed) { if (new_temp <= low_temp && fan_speed != fan->fan_min_speed) {
fan_speed = fan->fan_min_speed; fan_speed = fan->fan_min_speed;
} }
temp_change = new_temp - old_temp; temp_change = new_temp - old_temp;
if(temp_change > 0 && new_temp > high_temp && new_temp < max_temp) { if (temp_change > 0 && new_temp > high_temp && new_temp < max_temp) {
steps = ( new_temp - high_temp ) * ( new_temp - high_temp + 1 ) / 2; steps = (new_temp - high_temp) * (new_temp - high_temp + 1) / 2;
fan_speed = max( fan_speed, ceil(fan->fan_min_speed + steps * fan->step_up) ); fan_speed = max(fan_speed, ceil(fan->fan_min_speed + steps * fan->step_up));
} }
if(temp_change < 0 && new_temp > low_temp && new_temp < max_temp) { if (temp_change < 0 && new_temp > low_temp && new_temp < max_temp) {
steps = ( max_temp - new_temp ) * ( max_temp - new_temp + 1 ) / 2; steps = (max_temp - new_temp) * (max_temp - new_temp + 1) / 2;
fan_speed = min( fan_speed, floor(fan->fan_max_speed - steps * fan->step_down) ); fan_speed = min(fan_speed, floor(fan->fan_max_speed - steps * fan->step_down));
} }
if(verbose) { if (verbose) {
mbp_log(LOG_INFO, "Old Temp: %d New Temp: %d Fan: %s Speed: %d Max MHz: %d", old_temp, new_temp, fan->label, fan_speed, get_max_mhz()); mbp_log(LOG_INFO, "Old Temp: %d New Temp: %d Fan: %s Speed: %d Max MHz: %d", old_temp, new_temp, fan->label, fan_speed, get_max_mhz());
} }
set_fan_speed(fan, fan_speed); set_fan_speed(fan, fan_speed);
fan = fan->next; fan = fan->next;
} }
if(verbose) { if (verbose) {
mbp_log(LOG_INFO, "Sleeping for %d seconds", polling_interval); mbp_log(LOG_INFO, "Sleeping for %d seconds", polling_interval);
} }
@ -689,7 +680,7 @@ recalibrate:
nanosleep(&ts, NULL); nanosleep(&ts, NULL);
time_t after_sleep = time(NULL); time_t after_sleep = time(NULL);
if(after_sleep - before_sleep > 2 * polling_interval) { if (after_sleep - before_sleep > 2 * polling_interval) {
mbp_log(LOG_INFO, "Clock skew detected - slept for %ld seconds but expected %d", after_sleep - before_sleep, polling_interval); mbp_log(LOG_INFO, "Clock skew detected - slept for %ld seconds but expected %d", after_sleep - before_sleep, polling_interval);
set_fans_man(fans); set_fans_man(fans);
goto recalibrate; goto recalibrate;

View file

@ -40,7 +40,7 @@ typedef struct s_sensors t_sensors;
struct s_fans; struct s_fans;
typedef struct s_fans t_fans; typedef struct s_fans t_fans;
char *smprintf(const char *fmt, ...) __attribute__((format (printf, 1, 2))); char *smprintf(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
/** /**
* Return true if the kernel is < 3.15.0 * Return true if the kernel is < 3.15.0
@ -52,7 +52,7 @@ bool is_legacy_sensors_path();
* /etc/mbpfan.conf * /etc/mbpfan.conf
* If it fails, the default hardcoded settings are used * If it fails, the default hardcoded settings are used
*/ */
void retrieve_settings(const char* settings_path, t_fans *fans); void retrieve_settings(const char *settings_path, t_fans *fans);
/** /**
* Detect the sensors in /sys/devices/platform/coretemp.0/temp * Detect the sensors in /sys/devices/platform/coretemp.0/temp
@ -71,7 +71,7 @@ t_sensors *refresh_sensors(t_sensors *sensors);
* Detect the fans in /sys/devices/platform/applesmc.768/ * Detect the fans in /sys/devices/platform/applesmc.768/
* Associate each fan to a sensor * Associate each fan to a sensor
*/ */
t_fans* retrieve_fans(); t_fans *retrieve_fans();
/** /**
* Given a list of sensors with associated fans * Given a list of sensors with associated fans
@ -89,22 +89,22 @@ void set_fans_auto(t_fans *fans);
* Given a sensors with associated fans * Given a sensors with associated fans
* Change their speed * Change their speed
*/ */
void set_fan_speed(t_fans* fan, int speed); void set_fan_speed(t_fans *fan, int speed);
/** /**
* Given a list of fans set their minumum fan speed * Given a list of fans set their minumum fan speed
*/ */
void set_fan_minimum_speed(t_fans* fans); void set_fan_minimum_speed(t_fans *fans);
/** /**
* Return maximum CPU temp in degrees * Return maximum CPU temp in degrees
*/ */
unsigned short get_temp(t_sensors* sensors); unsigned short get_temp(t_sensors *sensors);
/** /**
* Check if user has proper access and that required * Check if user has proper access and that required
* kernel modules are available * kernel modules are available
*/ */
void check_requirements(const char* program_path); void check_requirements(const char *program_path);
/** /**
* Main Program * Main Program

View file

@ -36,17 +36,17 @@
*/ */
#include "settings.h" #include "settings.h"
#define MAX_SECTIONCHARS 256 #define MAX_SECTIONCHARS 256
#define MAX_KEYCHARS 256 #define MAX_KEYCHARS 256
#define MAX_VALUECHARS 256 #define MAX_VALUECHARS 256
#define MAX_LINECHARS (MAX_KEYCHARS + MAX_VALUECHARS + 10) #define MAX_LINECHARS (MAX_KEYCHARS + MAX_VALUECHARS + 10)
#define COMMENT_CHAR '#' #define COMMENT_CHAR '#'
#define SECTION_START_CHAR '[' #define SECTION_START_CHAR '['
#define SECTION_END_CHAR ']' #define SECTION_END_CHAR ']'
#define KEY_VALUE_SEPARATOR_CHAR '=' #define KEY_VALUE_SEPARATOR_CHAR '='
#define DEFAULT_STRMAP_CAPACITY 256 #define DEFAULT_STRMAP_CAPACITY 256
typedef struct Section Section; typedef struct Section Section;
typedef struct ParseState ParseState; typedef struct ParseState ParseState;
@ -83,20 +83,20 @@ static int is_comment_str(const char *str);
static int is_section_str(const char *str); static int is_section_str(const char *str);
static int is_key_value_str(const char *str); static int is_key_value_str(const char *str);
static int is_key_without_value_str(const char *str); static int is_key_without_value_str(const char *str);
static const char * get_token(char *str, char delim, char **last); static const char *get_token(char *str, char delim, char **last);
static int get_section_from_str(const char *str, char *out_buf, unsigned int out_buf_n); static int get_section_from_str(const char *str, char *out_buf, unsigned int out_buf_n);
static int get_key_value_from_str(const char *str, char *out_buf1, unsigned int out_buf1_n, char *out_buf2, unsigned int out_buf2_n); static int get_key_value_from_str(const char *str, char *out_buf1, unsigned int out_buf1_n, char *out_buf2, unsigned int out_buf2_n);
static int get_key_without_value_from_str(const char *str, char *out_buf, unsigned int out_buf_n); static int get_key_without_value_from_str(const char *str, char *out_buf, unsigned int out_buf_n);
static int get_converted_value(const Settings *settings, const char *section, const char *key, ConvertMode mode, void *out); static int get_converted_value(const Settings *settings, const char *section, const char *key, ConvertMode mode, void *out);
static int get_converted_tuple(const Settings *settings, const char *section, const char *key, char delim, ConvertMode mode, void *out, unsigned int n_out); static int get_converted_tuple(const Settings *settings, const char *section, const char *key, char delim, ConvertMode mode, void *out, unsigned int n_out);
static Section * get_section(Section *sections, unsigned int n, const char *name); static Section *get_section(Section *sections, unsigned int n, const char *name);
static void enum_map(const char *key, const char *value, const void *obj); static void enum_map(const char *key, const char *value, const void *obj);
Settings * settings_new() Settings *settings_new()
{ {
Settings *settings; Settings *settings;
settings = (Settings*)malloc(sizeof(Settings)); settings = (Settings *)malloc(sizeof(Settings));
if (settings == NULL) { if (settings == NULL) {
return NULL; return NULL;
@ -135,7 +135,7 @@ void settings_delete(Settings *settings)
free(settings); free(settings);
} }
Settings * settings_open(FILE *stream) Settings *settings_open(FILE *stream)
{ {
Settings *settings; Settings *settings;
char buf[MAX_LINECHARS]; char buf[MAX_LINECHARS];
@ -285,7 +285,7 @@ int settings_set(Settings *settings, const char *section, const char *key, const
if (s == NULL) { if (s == NULL) {
/* The section is not created---create it */ /* The section is not created---create it */
s = (Section*)realloc(settings->sections, (settings->section_count + 1) * sizeof(Section)); s = (Section *)realloc(settings->sections, (settings->section_count + 1) * sizeof(Section));
if (s == NULL) { if (s == NULL) {
return 0; return 0;
@ -301,7 +301,7 @@ int settings_set(Settings *settings, const char *section, const char *key, const
return 0; return 0;
} }
s->name = (char*)malloc((strlen(section) + 1) * sizeof(char)); s->name = (char *)malloc((strlen(section) + 1) * sizeof(char));
if (s->name == NULL) { if (s->name == NULL) {
sm_delete(s->map); sm_delete(s->map);
@ -723,7 +723,7 @@ static int get_key_without_value_from_str(const char *str, char *out_buf, unsign
* printf("token: %s", token); * printf("token: %s", token);
* } * }
*/ */
static const char * get_token(char *str, char delim, char **last) static const char *get_token(char *str, char delim, char **last)
{ {
char *s0; char *s0;
@ -845,7 +845,7 @@ static int get_converted_tuple(const Settings *settings, const char *section, co
/* Returns a pointer to the section or null if the named section does not /* Returns a pointer to the section or null if the named section does not
* exist. * exist.
*/ */
static Section * get_section(Section *sections, unsigned int n, const char *name) static Section *get_section(Section *sections, unsigned int n, const char *name)
{ {
unsigned int i; unsigned int i;
Section *section; Section *section;
@ -896,7 +896,7 @@ static void enum_map(const char *key, const char *value, const void *obj)
/* /*
GNU LESSER GENERAL PUBLIC LICENSE GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007 Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/> Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>

View file

@ -39,8 +39,7 @@
#include "strmap.h" #include "strmap.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C" {
{
#endif #endif
typedef struct Settings Settings; typedef struct Settings Settings;
@ -62,7 +61,7 @@ typedef struct Settings Settings;
* *
* Return value: None. * Return value: None.
*/ */
typedef void(*settings_section_enum_func)(const char *key, const char *value, const void *obj); typedef void (*settings_section_enum_func)(const char *key, const char *value, const void *obj);
/* /*
* Creates a settings object. * Creates a settings object.
@ -70,7 +69,7 @@ typedef void(*settings_section_enum_func)(const char *key, const char *value, co
* Return value: A pointer to a settings object, * Return value: A pointer to a settings object,
* or null if a new settings object could not be allocated. * or null if a new settings object could not be allocated.
*/ */
Settings * settings_new(); Settings *settings_new();
/* /*
* Releases all memory held by a settings object. * Releases all memory held by a settings object.
@ -98,7 +97,7 @@ void settings_delete(Settings *settings);
* Return value: A pointer to a settings object, * Return value: A pointer to a settings object,
* or null if an error occurred. * or null if an error occurred.
*/ */
Settings * settings_open(FILE *stream); Settings *settings_open(FILE *stream);
/* /*
* Saves the current settings object in textual form to the given stream. * Saves the current settings object in textual form to the given stream.
@ -338,7 +337,7 @@ int settings_section_enum(const Settings *settings, const char *section, setting
/* /*
GNU LESSER GENERAL PUBLIC LICENSE GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007 Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/> Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>

View file

@ -54,21 +54,21 @@ struct StrMap {
Bucket *buckets; Bucket *buckets;
}; };
static Pair * get_pair(Bucket *bucket, const char *key); static Pair *get_pair(Bucket *bucket, const char *key);
static unsigned long hash(const char *str); static unsigned long hash(const char *str);
StrMap * sm_new(unsigned int capacity) StrMap *sm_new(unsigned int capacity)
{ {
StrMap *map; StrMap *map;
map = (StrMap*)malloc(sizeof(StrMap)); map = (StrMap *)malloc(sizeof(StrMap));
if (map == NULL) { if (map == NULL) {
return NULL; return NULL;
} }
map->count = capacity; map->count = capacity;
map->buckets = (Bucket*)malloc(map->count * sizeof(Bucket)); map->buckets = (Bucket *)malloc(map->count * sizeof(Bucket));
if (map->buckets == NULL) { if (map->buckets == NULL) {
free(map); free(map);
@ -98,7 +98,7 @@ void sm_delete(StrMap *map)
pair = bucket->pairs; pair = bucket->pairs;
j = 0; j = 0;
while(j < m) { while (j < m) {
free(pair->key); free(pair->key);
free(pair->value); free(pair->value);
pair++; pair++;
@ -210,7 +210,7 @@ int sm_put(StrMap *map, const char *key, const char *value)
/* If the new value is larger than the old value, re-allocate /* If the new value is larger than the old value, re-allocate
* space for the new larger value. * space for the new larger value.
*/ */
tmp_value = (char*)realloc(pair->value, (value_len + 1) * sizeof(char)); tmp_value = (char *)realloc(pair->value, (value_len + 1) * sizeof(char));
if (tmp_value == NULL) { if (tmp_value == NULL) {
return 0; return 0;
@ -225,13 +225,13 @@ int sm_put(StrMap *map, const char *key, const char *value)
} }
/* Allocate space for a new key and value */ /* Allocate space for a new key and value */
new_key = (char*)malloc((key_len + 1) * sizeof(char)); new_key = (char *)malloc((key_len + 1) * sizeof(char));
if (new_key == NULL) { if (new_key == NULL) {
return 0; return 0;
} }
new_value = (char*)malloc((value_len + 1) * sizeof(char)); new_value = (char *)malloc((value_len + 1) * sizeof(char));
if (new_value == NULL) { if (new_value == NULL) {
free(new_key); free(new_key);
@ -243,7 +243,7 @@ int sm_put(StrMap *map, const char *key, const char *value)
/* The bucket is empty, lazily allocate space for a single /* The bucket is empty, lazily allocate space for a single
* key-value pair. * key-value pair.
*/ */
bucket->pairs = (Pair*)malloc(sizeof(Pair)); bucket->pairs = (Pair *)malloc(sizeof(Pair));
if (bucket->pairs == NULL) { if (bucket->pairs == NULL) {
free(new_key); free(new_key);
@ -257,7 +257,7 @@ int sm_put(StrMap *map, const char *key, const char *value)
/* The bucket wasn't empty but no pair existed that matches the provided /* The bucket wasn't empty but no pair existed that matches the provided
* key, so create a new key-value pair. * key, so create a new key-value pair.
*/ */
tmp_pairs = (Pair*)realloc(bucket->pairs, (bucket->count + 1) * sizeof(Pair)); tmp_pairs = (Pair *)realloc(bucket->pairs, (bucket->count + 1) * sizeof(Pair));
if (tmp_pairs == NULL) { if (tmp_pairs == NULL) {
free(new_key); free(new_key);
@ -353,7 +353,7 @@ int sm_enum(const StrMap *map, sm_enum_func enum_func, const void *obj)
* Returns a pair from the bucket that matches the provided key, * Returns a pair from the bucket that matches the provided key,
* or null if no such pair exist. * or null if no such pair exist.
*/ */
static Pair * get_pair(Bucket *bucket, const char *key) static Pair *get_pair(Bucket *bucket, const char *key)
{ {
unsigned int i, n; unsigned int i, n;
Pair *pair; Pair *pair;
@ -398,7 +398,7 @@ static unsigned long hash(const char *str)
/* /*
GNU LESSER GENERAL PUBLIC LICENSE GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007 Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/> Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>

View file

@ -37,8 +37,7 @@
#define _STRMAP_H_ #define _STRMAP_H_
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C" {
{
#endif #endif
#include <stdlib.h> #include <stdlib.h>
@ -63,7 +62,7 @@ typedef struct StrMap StrMap;
* *
* Return value: None. * Return value: None.
*/ */
typedef void(*sm_enum_func)(const char *key, const char *value, const void *obj); typedef void (*sm_enum_func)(const char *key, const char *value, const void *obj);
/* /*
* Creates a string map. * Creates a string map.
@ -76,7 +75,7 @@ typedef void(*sm_enum_func)(const char *key, const char *value, const void *obj)
* Return value: A pointer to a string map object, * Return value: A pointer to a string map object,
* or null if a new string map could not be allocated. * or null if a new string map could not be allocated.
*/ */
StrMap * sm_new(unsigned int capacity); StrMap *sm_new(unsigned int capacity);
/* /*
* Releases all memory held by a string map object. * Releases all memory held by a string map object.
@ -186,7 +185,7 @@ int sm_enum(const StrMap *map, sm_enum_func enum_func, const void *obj);
/* /*
GNU LESSER GENERAL PUBLIC LICENSE GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007 Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/> Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>

View file

@ -16,6 +16,6 @@ void mbp_log(int level, const char *fmt, ...)
va_start(args, fmt); va_start(args, fmt);
vprintf(fmt, args); vprintf(fmt, args);
puts(""); // trailing newline puts(""); // trailing newline
va_end(args); va_end(args);
} }

View file

@ -1,6 +1,6 @@
#ifndef _UTIL_H_ #ifndef _UTIL_H_
#define _UTIL_H_ #define _UTIL_H_
void mbp_log(int level, const char *fmt, ...) __attribute__ ((format (printf, 2, 3))); void mbp_log(int level, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
#endif #endif