Commit a2dacfde authored by Daniel Graziotin's avatar Daniel Graziotin

Fixes #4. Adds some tests for #28

parent 33a37109
[general]
min_fan_speed = 6200 # default is 2000
max_fan_speed = 6200 # default is 6200
low_temp = 63 # try ranges 55-63, default is 63
high_temp = 66 # try ranges 58-66, default is 66
max_temp = 86 # do not set it > 90, default is 86
polling_interval = 1 # default is 7
......@@ -29,6 +29,7 @@
#include <unistd.h>
#include "mbpfan.h"
#include "global.h"
#include "daemon.h"
int write_pid(int pid)
{
......@@ -65,16 +66,13 @@ int delete_pid()
return remove(PROGRAM_PID);
}
void signal_handler(int signal)
{
switch(signal) {
case SIGHUP:
//TODO: restart myself
syslog(LOG_WARNING, "Received SIGHUP signal.");
delete_pid();
exit(0);
retrieve_settings(NULL);
break;
case SIGTERM:
syslog(LOG_WARNING, "Received SIGTERM signal.");
......@@ -105,6 +103,7 @@ void go_daemon(void (*fan_control)())
signal(SIGHUP, signal_handler);
signal(SIGTERM, signal_handler);
signal(SIGINT, signal_handler);
signal(SIGSTOP, signal_handler);
syslog(LOG_INFO, "%s starting up", PROGRAM_NAME);
......@@ -131,6 +130,7 @@ void go_daemon(void (*fan_control)())
}
if (pid_slave > 0) {
signal(SIGCHLD, SIG_IGN);
// kill the father
exit(EXIT_SUCCESS);
}
......
......@@ -45,10 +45,10 @@ int delete_pid();
*/
void signal_handler(int signal);
/**
* Daemonizes
*/
int go_daemon(void (*function)());
void go_daemon(void (*function)());
#endif
\ No newline at end of file
......@@ -23,4 +23,5 @@ struct s_fans {
typedef struct s_sensors t_sensors;
typedef struct s_fans t_fans;
#endif
\ No newline at end of file
......@@ -68,11 +68,10 @@ void check_requirements()
}
/**
* Check for coretemp and applesmc modules
* Credits: -http://stackoverflow.com/questions/12978794
*/
* Check for coretemp and applesmc modules
* Credits: -http://stackoverflow.com/questions/12978794
*/
FILE *fd = popen("lsmod | grep coretemp", "r");
char buf[16];
if (!(fread (buf, 1, sizeof (buf), fd) > 0)) {
......@@ -83,6 +82,7 @@ void check_requirements()
printf("%s needs coretemp module.\nPlease either load it or build it into the kernel. Exiting.\n", PROGRAM_NAME);
exit(0);
}
}
fd = popen("lsmod | grep applesmc", "r");
......@@ -92,7 +92,7 @@ void check_requirements()
if (ENOENT == errno) {
syslog(LOG_INFO, "%s needs applesmc support. Please either load it or build it into the kernel. Exiting.", PROGRAM_NAME);
printf("%s needs applesmc module.\nPlease either load it or build it into the kernel. Exiting.\n", PROGRAM_NAME);
printf("%s needs applesmc module.\nPlease either load it or build it into the kernel. Exiting.\n", PROGRAM_NAME);
exit(0);
}
......
......@@ -296,12 +296,19 @@ unsigned short get_temp(t_sensors* sensors)
return temp;
}
void retrieve_settings()
void retrieve_settings(const char* settings_path)
{
Settings *settings = NULL;
int result = 0;
FILE *f = NULL;
f = fopen("/etc/mbpfan.conf", "r");
if (settings_path == NULL) {
f = fopen("/etc/mbpfan.conf", "r");
} else {
f = fopen(settings_path, "r");
}
if (f == NULL) {
/* Could not open configfile */
......@@ -378,7 +385,7 @@ void mbpfan()
int temp_change;
int step_up, step_down;
retrieve_settings();
retrieve_settings(NULL);
t_sensors* sensors = retrieve_sensors();
t_fans* fans = retrieve_fans();
......@@ -388,7 +395,6 @@ void mbpfan()
fan_speed = 2000;
set_fan_speed(fans, fan_speed);
if(verbose) {
printf("Sleeping for %d seconds\n", polling_interval);
......
......@@ -48,7 +48,7 @@ typedef struct s_fans t_fans;
* /etc/mbpfan.conf
* If it fails, the default hardcoded settings are used
*/
void retrieve_settings();
void retrieve_settings(const char* settings_path);
/**
* Detect the sensors in /sys/devices/platform/coretemp.0/temp
......
......@@ -3,6 +3,7 @@
#include <stdlib.h>
#include <time.h>
#include <limits.h>
#include <signal.h>
#include "global.h"
#include "mbpfan.h"
#include "settings.h"
......@@ -124,6 +125,54 @@ static const char *test_config_file()
return 0;
}
static const char *test_settings()
{
retrieve_settings("./mbpfan.conf.test1");
mu_assert("min_fan_speed value is not 6200", min_fan_speed == 6200);
mu_assert("polling_interval is not 1", polling_interval == 1);
retrieve_settings("./mbpfan.conf");
mu_assert("min_fan_speed value is not 2000", min_fan_speed == 2000);
mu_assert("polling_interval is not 7", polling_interval == 7);
return 0;
}
int received = 0;
static void handler(int signal)
{
switch(signal) {
case SIGHUP:
received = 1;
retrieve_settings("./mbpfan.conf.test1");
break;
default:
received = 0;
break;
}
}
static const char *test_sighup_receive()
{
signal(SIGHUP, handler);
raise(SIGHUP);
mu_assert("did not receive SIGHUP signal", received == 1);
return 0;
}
static const char *test_settings_reload()
{
signal(SIGHUP, handler);
retrieve_settings("./mbpfan.conf");
mu_assert("min_fan_speed value is not 2000 before SIGHUP", min_fan_speed == 2000);
mu_assert("polling_interval is not 7 before SIHUP", polling_interval == 7);
raise(SIGHUP);
mu_assert("min_fan_speed value is not 6200 after SIGHUP", min_fan_speed == 6200);
mu_assert("polling_interval is not 1 after SIHUP", polling_interval == 1);
retrieve_settings("./mbpfan.conf");
return 0;
}
static const char *all_tests()
{
......@@ -131,6 +180,9 @@ static const char *all_tests()
mu_run_test(test_fan_paths);
mu_run_test(test_get_temp);
mu_run_test(test_config_file);
mu_run_test(test_settings);
mu_run_test(test_sighup_receive);
mu_run_test(test_settings_reload);
return 0;
}
......
......@@ -15,7 +15,10 @@ static const char *test_sensor_paths();
static const char *test_fan_paths();
static const char *test_get_temp();
static const char *test_config_file();
static const char *test_settings();
static void handler(int signal);
static const char *test_sighup_receive();
static const char *test_settings_reload();
static const char *all_tests();
int tests();
......
File mode changed from 100755 to 100644
File mode changed from 100755 to 100644
File mode changed from 100755 to 100644
File mode changed from 100755 to 100644
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment