mbpfan-josie

Fixes #4. Adds some tests for #28

a2dacfde05704b8c7857edacfa8a2026a0b13b71
Daniel Graziotin <dgraziotin@task3.cc> · 2012-11-18T22:16 · browse files at this commit

parents: 33a3710

diff --git a/mbpfan.conf.test1 b/mbpfan.conf.test1
new file mode 100644
index 0000000..e17bdbd
--- /dev/null
+++ b/mbpfan.conf.test1
@@ -0,0 +1,7 @@
+[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
diff --git a/src/daemon.c b/src/daemon.c
old mode 100755
new mode 100644
index a714af3..9b33b61
--- a/src/daemon.c
+++ b/src/daemon.c
@@ -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);
         }
diff --git a/src/daemon.h b/src/daemon.h
index b20a185..67fa775 100644
--- a/src/daemon.h
+++ b/src/daemon.h
@@ -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
diff --git a/src/global.h b/src/global.h
index 093f4c9..472bad3 100644
--- a/src/global.h
+++ b/src/global.h
@@ -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
diff --git a/src/main.c b/src/main.c
index d275879..12947b8 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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);
         }
 
diff --git a/src/mbpfan.c b/src/mbpfan.c
index 73a3655..86cdbe5 100644
--- a/src/mbpfan.c
+++ b/src/mbpfan.c
@@ -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);
 
diff --git a/src/mbpfan.h b/src/mbpfan.h
index 70f6297..27cba96 100644
--- a/src/mbpfan.h
+++ b/src/mbpfan.h
@@ -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
diff --git a/src/minunit.c b/src/minunit.c
old mode 100755
new mode 100644
index 622ed95..01ba082
--- a/src/minunit.c
+++ b/src/minunit.c
@@ -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;
 }
 
diff --git a/src/minunit.h b/src/minunit.h
index 5fbff28..603f046 100644
--- a/src/minunit.h
+++ b/src/minunit.h
@@ -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();
diff --git a/src/settings.c b/src/settings.c
old mode 100755
new mode 100644
diff --git a/src/settings.h b/src/settings.h
old mode 100755
new mode 100644
diff --git a/src/strmap.c b/src/strmap.c
old mode 100755
new mode 100644
diff --git a/src/strmap.h b/src/strmap.h
old mode 100755
new mode 100644