Fixes #10
This commit is contained in:
parent
a6cc7a45b6
commit
1078814133
8 changed files with 550 additions and 336 deletions
|
@ -24,6 +24,11 @@ Compile with
|
|||
Manually compile with
|
||||
gcc -o bin/mbpfan src/mbpfan.c -lm
|
||||
|
||||
Run The Tests (Recommended)
|
||||
---------------------------
|
||||
|
||||
It is recommended to run the tests before installing the program
|
||||
sudo ./bin/mbpfan -t
|
||||
|
||||
Install Instructions
|
||||
--------------------
|
||||
|
|
73
src/daemon.c
73
src/daemon.c
|
@ -29,23 +29,29 @@
|
|||
#include "mbpfan.h"
|
||||
#include "global.h"
|
||||
|
||||
int write_pid(int pid){
|
||||
int write_pid(int pid)
|
||||
{
|
||||
FILE *file = NULL;
|
||||
file = fopen(program_pid, "w");
|
||||
if(file != NULL) {
|
||||
if(file != NULL)
|
||||
{
|
||||
fprintf(file, "%d", pid);
|
||||
fclose(file);
|
||||
return 1;
|
||||
}else{
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int read_pid(){
|
||||
int read_pid()
|
||||
{
|
||||
FILE *file = NULL;
|
||||
int pid = -1;
|
||||
file = fopen(program_pid, "r");
|
||||
if(file != NULL) {
|
||||
if(file != NULL)
|
||||
{
|
||||
fscanf(file, "%d", &pid);
|
||||
fclose(file);
|
||||
return pid;
|
||||
|
@ -53,7 +59,8 @@ int read_pid(){
|
|||
return -1;
|
||||
}
|
||||
|
||||
int delete_pid(){
|
||||
int delete_pid()
|
||||
{
|
||||
return remove(program_pid);
|
||||
}
|
||||
|
||||
|
@ -61,7 +68,8 @@ int delete_pid(){
|
|||
void signal_handler(int signal)
|
||||
{
|
||||
|
||||
switch(signal) {
|
||||
switch(signal)
|
||||
{
|
||||
case SIGHUP:
|
||||
//TODO: restart myself
|
||||
syslog(LOG_WARNING, "Received SIGHUP signal.");
|
||||
|
@ -96,10 +104,13 @@ void go_daemon(void (*fan_control)())
|
|||
syslog(LOG_INFO, "%s starting up", program_name);
|
||||
|
||||
// Setup syslog logging - see SETLOGMASK(3)
|
||||
if(verbose) {
|
||||
if(verbose)
|
||||
{
|
||||
setlogmask(LOG_UPTO(LOG_DEBUG));
|
||||
openlog(program_name, LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
setlogmask(LOG_UPTO(LOG_INFO));
|
||||
openlog(program_name, LOG_CONS, LOG_USER);
|
||||
}
|
||||
|
@ -108,13 +119,16 @@ void go_daemon(void (*fan_control)())
|
|||
pid_t pid_slave;
|
||||
pid_t sid_slave;
|
||||
|
||||
if (daemonize) {
|
||||
if (daemonize)
|
||||
{
|
||||
|
||||
pid_slave = fork();
|
||||
if (pid_slave < 0) {
|
||||
if (pid_slave < 0)
|
||||
{
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (pid_slave > 0) {
|
||||
if (pid_slave > 0)
|
||||
{
|
||||
// kill the father
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
@ -123,11 +137,13 @@ void go_daemon(void (*fan_control)())
|
|||
|
||||
// new sid_slave for the child process
|
||||
sid_slave = setsid();
|
||||
if (sid_slave < 0) {
|
||||
if (sid_slave < 0)
|
||||
{
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if ((chdir("/")) < 0) {
|
||||
if ((chdir("/")) < 0)
|
||||
{
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
|
@ -142,26 +158,36 @@ void go_daemon(void (*fan_control)())
|
|||
|
||||
int current_pid = getpid();
|
||||
|
||||
if (read_pid() == -1){
|
||||
if (verbose){
|
||||
if (read_pid() == -1)
|
||||
{
|
||||
if (verbose)
|
||||
{
|
||||
printf("Writing a new .pid file with value %d at: %s", current_pid, program_pid);
|
||||
syslog(LOG_INFO, "Writing a new .pid file with value %d at: %s", current_pid, program_pid);
|
||||
}
|
||||
if (write_pid(current_pid) == 0){
|
||||
if (write_pid(current_pid) == 0)
|
||||
{
|
||||
syslog(LOG_ERR, "Can not create a .pid file at: %s. Aborting", program_pid);
|
||||
if (verbose){
|
||||
if (verbose)
|
||||
{
|
||||
printf("ERROR: Can not create a .pid file at: %s. Aborting", program_pid);
|
||||
}
|
||||
exit(EXIT_FAILURE);
|
||||
}else{
|
||||
if (verbose){
|
||||
}
|
||||
else
|
||||
{
|
||||
if (verbose)
|
||||
{
|
||||
printf("Successfully written a new .pid file with value %d at: %s", current_pid, program_pid);
|
||||
syslog(LOG_INFO, "Successfully written a new .pid file with value %d at: %s", current_pid, program_pid);
|
||||
}
|
||||
}
|
||||
}else{
|
||||
}
|
||||
else
|
||||
{
|
||||
syslog(LOG_ERR, "A previously created .pid file exists at: %s. Aborting", program_pid);
|
||||
if (verbose){
|
||||
if (verbose)
|
||||
{
|
||||
printf("ERROR: a previously created .pid file exists at: %s.\n Aborting\n", program_pid);
|
||||
}
|
||||
exit(EXIT_FAILURE);
|
||||
|
@ -170,7 +196,8 @@ void go_daemon(void (*fan_control)())
|
|||
|
||||
fan_control();
|
||||
|
||||
if(daemonize){
|
||||
if(daemonize)
|
||||
{
|
||||
syslog(LOG_INFO, "%s daemon exiting", program_name);
|
||||
}
|
||||
|
||||
|
|
17
src/main.c
17
src/main.c
|
@ -19,6 +19,7 @@
|
|||
#include "mbpfan.h"
|
||||
#include "daemon.h"
|
||||
#include "global.h"
|
||||
#include "minunit.h"
|
||||
|
||||
int daemonize = 1;
|
||||
int verbose = 0;
|
||||
|
@ -28,22 +29,28 @@ const char *program_pid = "/var/run/mbpfan.pid";
|
|||
|
||||
void print_usage(int argc, char *argv[])
|
||||
{
|
||||
if (argc >=1) {
|
||||
if (argc >=1)
|
||||
{
|
||||
printf("Usage: %s OPTION(S) \n", argv[0]);
|
||||
printf("Options:\n");
|
||||
printf("\t-h Show this help screen\n");
|
||||
printf("\t-f Run in foreground\n");
|
||||
printf("\t-t Run the tests\n");
|
||||
printf("\t-v Be (a lot) verbose\n");
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
|
||||
int c;
|
||||
while( (c = getopt(argc, argv, "hfv|help")) != -1) {
|
||||
switch(c) {
|
||||
while( (c = getopt(argc, argv, "hftv|help")) != -1)
|
||||
{
|
||||
switch(c)
|
||||
{
|
||||
case 'h':
|
||||
print_usage(argc, argv);
|
||||
exit(0);
|
||||
|
@ -51,6 +58,10 @@ int main(int argc, char *argv[])
|
|||
case 'f':
|
||||
daemonize = 0;
|
||||
break;
|
||||
case 't':
|
||||
tests();
|
||||
exit(0);
|
||||
break;
|
||||
case 'v':
|
||||
verbose = 1;
|
||||
break;
|
||||
|
|
88
src/mbpfan.c
88
src/mbpfan.c
|
@ -55,7 +55,8 @@ int max_temp = 86; // do not set it > 90
|
|||
int polling_interval = 7;
|
||||
|
||||
|
||||
struct s_sensors {
|
||||
struct s_sensors
|
||||
{
|
||||
char* path;
|
||||
char* fan_min_path;
|
||||
char* fan_man_path;
|
||||
|
@ -79,7 +80,8 @@ t_sensors *retrieve_sensors()
|
|||
sprintf(number,"%d",0);
|
||||
|
||||
int i = 0;
|
||||
for(i = 0; i<10; i++) {
|
||||
for(i = 0; i<10; i++)
|
||||
{
|
||||
path = (char*) malloc(sizeof( char ) * path_size);
|
||||
|
||||
sprintf(number,"%d",i);
|
||||
|
@ -90,17 +92,22 @@ t_sensors *retrieve_sensors()
|
|||
|
||||
FILE *file = fopen(path, "r");
|
||||
|
||||
if(file != NULL) {
|
||||
if(file != NULL)
|
||||
{
|
||||
s = (t_sensors *) malloc( sizeof( t_sensors ) );
|
||||
s->path = (char *) malloc(sizeof( char ) * path_size);
|
||||
strcpy(s->path, path);
|
||||
fscanf(file, "%d", &s->temperature);
|
||||
if (sensors_head == NULL) {
|
||||
if (sensors_head == NULL)
|
||||
{
|
||||
sensors_head = s;
|
||||
sensors_head->next = NULL;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
t_sensors *tmp = sensors_head;
|
||||
while (tmp->next != NULL) {
|
||||
while (tmp->next != NULL)
|
||||
{
|
||||
tmp = tmp->next;
|
||||
}
|
||||
tmp->next = s;
|
||||
|
@ -135,7 +142,8 @@ void find_fans(t_sensors* sensors)
|
|||
int n_sensors = 0;
|
||||
int n_fans = 0;
|
||||
|
||||
for(n_sensors = 0; n_sensors<10; n_sensors++) {
|
||||
for(n_sensors = 0; n_sensors<10; n_sensors++)
|
||||
{
|
||||
path_min = (char*) malloc(sizeof( char ) * path_min_size);
|
||||
path_min[0] = '\0';
|
||||
path_man = (char*) malloc(sizeof( char ) * path_man_size);
|
||||
|
@ -153,8 +161,10 @@ void find_fans(t_sensors* sensors)
|
|||
|
||||
FILE *file = fopen(path_min, "r");
|
||||
|
||||
if(file != NULL) {
|
||||
if (tmp->path != NULL) {
|
||||
if(file != NULL)
|
||||
{
|
||||
if (tmp->path != NULL)
|
||||
{
|
||||
tmp->fan_min_path = (char *) malloc(sizeof( char ) * path_min_size);
|
||||
tmp->fan_man_path = (char *) malloc(sizeof( char ) * path_man_size);
|
||||
}
|
||||
|
@ -166,9 +176,11 @@ void find_fans(t_sensors* sensors)
|
|||
}
|
||||
}
|
||||
|
||||
if(verbose) {
|
||||
if(verbose)
|
||||
{
|
||||
printf("Found %d: sensors and %d fans\n", n_sensors, n_fans);
|
||||
if(daemonize) {
|
||||
if(daemonize)
|
||||
{
|
||||
syslog(LOG_INFO, "Found %d: sensors and %d fans", n_sensors, n_fans);
|
||||
}
|
||||
}
|
||||
|
@ -184,9 +196,11 @@ void set_fans_man(t_sensors *sensors)
|
|||
|
||||
t_sensors *tmp = sensors;
|
||||
FILE *file;
|
||||
while(tmp != NULL) {
|
||||
while(tmp != NULL)
|
||||
{
|
||||
file = fopen(tmp->fan_man_path, "rw+");
|
||||
if(file != NULL) {
|
||||
if(file != NULL)
|
||||
{
|
||||
fprintf(file, "%d", 0);
|
||||
fclose(file);
|
||||
}
|
||||
|
@ -199,10 +213,12 @@ t_sensors *refresh_sensors(t_sensors *sensors)
|
|||
|
||||
t_sensors *tmp = sensors;
|
||||
|
||||
while(tmp != NULL) {
|
||||
while(tmp != NULL)
|
||||
{
|
||||
FILE *file = fopen(tmp->path, "r");
|
||||
|
||||
if(file != NULL) {
|
||||
if(file != NULL)
|
||||
{
|
||||
fscanf(file, "%d", &tmp->temperature);
|
||||
fclose(file);
|
||||
}
|
||||
|
@ -219,9 +235,11 @@ void set_fan_speed(t_sensors* sensors, int speed)
|
|||
{
|
||||
t_sensors *tmp = sensors;
|
||||
FILE *file;
|
||||
while(tmp != NULL) {
|
||||
while(tmp != NULL)
|
||||
{
|
||||
file = fopen(tmp->fan_min_path, "rw+");
|
||||
if(file != NULL) {
|
||||
if(file != NULL)
|
||||
{
|
||||
fprintf(file, "%d", speed);
|
||||
fclose(file);
|
||||
}
|
||||
|
@ -241,7 +259,8 @@ unsigned short get_temp(t_sensors* sensors)
|
|||
unsigned short temp = 0;
|
||||
|
||||
t_sensors* tmp = sensors;
|
||||
while(tmp != NULL) {
|
||||
while(tmp != NULL)
|
||||
{
|
||||
sum_temp += tmp->temperature;
|
||||
tmp = tmp->next;
|
||||
}
|
||||
|
@ -262,9 +281,11 @@ void mbpfan()
|
|||
fan_speed = 2000;
|
||||
set_fan_speed(sensors, fan_speed);
|
||||
|
||||
if(verbose) {
|
||||
if(verbose)
|
||||
{
|
||||
printf("Sleeping for %d seconds\n", polling_interval);
|
||||
if(daemonize) {
|
||||
if(daemonize)
|
||||
{
|
||||
syslog(LOG_INFO, "Sleeping for %d seconds\n", polling_interval);
|
||||
}
|
||||
}
|
||||
|
@ -276,42 +297,51 @@ void mbpfan()
|
|||
step_down = (float)( max_fan_speed - min_fan_speed ) /
|
||||
(float)( ( max_temp - low_temp ) * ( max_temp - low_temp + 1 ) / 2 );
|
||||
|
||||
while(1) {
|
||||
while(1)
|
||||
{
|
||||
old_temp = new_temp;
|
||||
new_temp = get_temp(sensors);
|
||||
|
||||
if(new_temp >= max_temp && fan_speed != max_fan_speed) {
|
||||
if(new_temp >= max_temp && fan_speed != max_fan_speed)
|
||||
{
|
||||
fan_speed = max_fan_speed;
|
||||
}
|
||||
|
||||
if(new_temp <= low_temp && fan_speed != min_fan_speed) {
|
||||
if(new_temp <= low_temp && fan_speed != min_fan_speed)
|
||||
{
|
||||
fan_speed = min_fan_speed;
|
||||
}
|
||||
|
||||
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;
|
||||
fan_speed = max( fan_speed, ceil(min_fan_speed + steps * 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;
|
||||
fan_speed = min( fan_speed, floor(max_fan_speed - steps * step_down) );
|
||||
}
|
||||
|
||||
if(verbose) {
|
||||
if(verbose)
|
||||
{
|
||||
printf("Old Temp %d: New Temp: %d, Fan Speed: %d\n", old_temp, new_temp, fan_speed);
|
||||
if(daemonize) {
|
||||
if(daemonize)
|
||||
{
|
||||
syslog(LOG_INFO, "Old Temp %d: New Temp: %d, Fan Speed: %d\n", old_temp, new_temp, fan_speed);
|
||||
}
|
||||
}
|
||||
|
||||
set_fan_speed(sensors, fan_speed);
|
||||
|
||||
if(verbose) {
|
||||
if(verbose)
|
||||
{
|
||||
printf("Sleeping for %d seconds\n", polling_interval);
|
||||
if(daemonize) {
|
||||
if(daemonize)
|
||||
{
|
||||
syslog(LOG_INFO, "Sleeping for %d seconds\n", polling_interval);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -61,6 +61,11 @@ void find_fans(t_sensors *sensors);
|
|||
*/
|
||||
void set_fans_man(t_sensors *sensors);
|
||||
|
||||
/**
|
||||
* Return average CPU temp in degrees (ceiling)
|
||||
*/
|
||||
unsigned short get_temp(t_sensors* sensors);
|
||||
|
||||
/**
|
||||
* Main Program
|
||||
*/
|
||||
|
|
120
src/minunit.c
Normal file
120
src/minunit.c
Normal file
|
@ -0,0 +1,120 @@
|
|||
/* file minunit_example.c */
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <limits.h>
|
||||
#include "mbpfan.h"
|
||||
#include "minunit.h"
|
||||
|
||||
int tests_run = 0;
|
||||
|
||||
|
||||
struct s_sensors
|
||||
{
|
||||
char* path;
|
||||
char* fan_min_path;
|
||||
char* fan_man_path;
|
||||
unsigned int temperature;
|
||||
struct s_sensors *next;
|
||||
};
|
||||
typedef s_sensors t_sensors;
|
||||
|
||||
|
||||
static char *test_sensor_paths()
|
||||
{
|
||||
t_sensors* sensors = retrieve_sensors();
|
||||
mu_assert("No sensors found", sensors != NULL);
|
||||
t_sensors* tmp = sensors;
|
||||
while(tmp != NULL)
|
||||
{
|
||||
mu_assert("Sensor does not have a valid path", tmp->path != NULL);
|
||||
if(tmp->path != NULL)
|
||||
mu_assert("Sensor does not have valid temperature", tmp->temperature > 0);
|
||||
tmp = tmp->next;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static char *test_fan_paths()
|
||||
{
|
||||
t_sensors* sensors = retrieve_sensors();
|
||||
mu_assert("No sensors found", sensors != NULL);
|
||||
t_sensors* tmp = sensors;
|
||||
int found_fan_path = 0;
|
||||
while(tmp != NULL)
|
||||
{
|
||||
if(tmp->fan_min_path != NULL)
|
||||
found_fan_path++;
|
||||
tmp = tmp->next;
|
||||
}
|
||||
mu_assert("No fans found", found_fan_path != 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned time_seed()
|
||||
{
|
||||
time_t now = time ( 0 );
|
||||
unsigned char *p = (unsigned char *)&now;
|
||||
unsigned seed = 0;
|
||||
size_t i;
|
||||
for ( i = 0; i < sizeof now; i++ )
|
||||
seed = seed * ( UCHAR_MAX + 2U ) + p[i];
|
||||
return seed;
|
||||
}
|
||||
|
||||
// nothing better than a horrible piece of code to
|
||||
// stress a little bit the CPU
|
||||
int stress(int n)
|
||||
{
|
||||
int f = n;
|
||||
while (f > 0)
|
||||
{
|
||||
while(n > 0)
|
||||
{
|
||||
srand ( time_seed() );
|
||||
n--;
|
||||
}
|
||||
f--;
|
||||
n = f;
|
||||
}
|
||||
}
|
||||
|
||||
static char *test_get_temp()
|
||||
{
|
||||
t_sensors* sensors = retrieve_sensors();
|
||||
mu_assert("No sensors found", sensors != NULL);
|
||||
unsigned short temp_1 = get_temp(sensors);
|
||||
mu_assert("Invalid Global Temperature Found", temp_1 > 1 && temp_1 < 150);
|
||||
stress(2000);
|
||||
unsigned short temp_2 = get_temp(sensors);
|
||||
mu_assert("Invalid Higher temp test (if fan was already spinning high, this is not worrying)", temp_1 < temp_2);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static char *all_tests()
|
||||
{
|
||||
mu_run_test(test_sensor_paths);
|
||||
mu_run_test(test_fan_paths);
|
||||
mu_run_test(test_get_temp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int tests()
|
||||
{
|
||||
printf("Starting the tests..\n");
|
||||
printf("It is normal for them to take a bit to finish.\n");
|
||||
char *result = all_tests();
|
||||
if (result != 0)
|
||||
{
|
||||
printf("%s \n", result);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("ALL TESTS PASSED\n");
|
||||
}
|
||||
printf("Tests run: %d\n", tests_run);
|
||||
|
||||
return result != 0;
|
||||
}
|
16
src/minunit.h
Normal file
16
src/minunit.h
Normal file
|
@ -0,0 +1,16 @@
|
|||
/**
|
||||
* This is the MinUnit testing framework - http://www.jera.com/techinfo/jtns/jtn002.html
|
||||
*/
|
||||
#define mu_assert(message, test) do { if (!(test)) return message; } while (0)
|
||||
#define mu_run_test(test) do { char *message = test(); tests_run++; \
|
||||
if (message) return message; } while (0)
|
||||
|
||||
extern int tests_run;
|
||||
|
||||
static char *test_sensor_paths();
|
||||
static char *test_fan_paths();
|
||||
static char *test_get_temp();
|
||||
|
||||
static char *all_tests();
|
||||
|
||||
int tests();
|
Loading…
Reference in a new issue