diff --git a/Makefile b/Makefile index d5223ae..5c466b8 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,8 @@ C = c OBJ = o OUTPUT_PATH = bin/ SOURCE_PATH = src/ +TESTS_PATH = tests/ +TESTS_BIN = bin/mbpfan-tests BIN = bin/mbpfan CONF = mbpfan.conf DOC = README.md @@ -20,24 +22,32 @@ CFLAGS += $(COPT) -g $(INCLUDES) -Wall -Wextra -Wno-unused-function -std=c99 -D_ LDFLAGS += $(LIBPATH) -g OBJS := $(patsubst %.$(C),%.$(OBJ),$(wildcard $(SOURCE_PATH)*.$(C))) +TESTS_OBJS := $(patsubst %.$(C),%.$(OBJ),$(wildcard $(TESTS_PATH)*.$(C))) +TESTS_OBJS += $(filter-out %main.$(OBJ),$(OBJS)) + +.PHONY: all clean tests uninstall install rebuild %.$(OBJ):%.$(C) mkdir -p bin @echo Compiling $(basename $<)... $(CC) -c $(CFLAGS) $< $(OBJFLAG)$@ -all: $(BIN) +all: $(BIN) $(TESTS_BIN) $(BIN): $(OBJS) @echo Linking... $(CC) $(LDFLAGS) $^ $(LIBS) $(BINFLAG) $(BIN) +$(TESTS_BIN): $(TESTS_OBJS) + @echo Linking... + $(CC) $(LDFLAGS) $^ $(LIBS) $(BINFLAG) $(TESTS_BIN) + clean: rm -rf $(SOURCE_PATH)*.$(OBJ) $(BIN) + rm -rf $(TESTS_PATH)*.$(OBJ) $(TESTS_BIN) -tests: - make install - /usr/sbin/mbpfan -f -v -t +tests: all + ./bin/mbpfan-tests uninstall: rm /usr/sbin/mbpfan @@ -46,7 +56,7 @@ uninstall: rm /usr/share/man/man8/mbpfan.8.gz rm -rf /usr/share/doc/mbpfan -install: $(BIN) +install: all install -d $(DESTDIR)/usr/sbin install -d $(DESTDIR)/etc install -d $(DESTDIR)/lib/systemd/system diff --git a/README.md b/README.md index f34d8b0..5852fbd 100644 --- a/README.md +++ b/README.md @@ -155,11 +155,7 @@ default compiler to be Clang. Tested with Clang 3.8 and 3.9. Tested with Clang Run The Tests (Optional) ------------------------ -Users may run the tests after installing the program. Please run the following command _from within the source directory_. - - sudo ./bin/mbpfan -t - -or +Users may run the tests after building the program. Please run the following command _from within the source directory_. sudo make tests @@ -233,7 +229,6 @@ execute the following (as root): -h Show the help screen -f Run in foreground - -t Run the tests -v Be (a lot) verbose ## References diff --git a/mbpfan.8.gz b/mbpfan.8.gz index 8b97c4c..76146d0 100644 Binary files a/mbpfan.8.gz and b/mbpfan.8.gz differ diff --git a/src/daemon.c b/src/daemon.c index 38104ac..3f933e8 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -35,6 +35,9 @@ #include "daemon.h" #include "util.h" +int daemonize = 1; +int verbose = 0; + int write_pid(int pid) { FILE *file = NULL; diff --git a/src/daemon.h b/src/daemon.h index dbbc17f..c52c4cc 100644 --- a/src/daemon.h +++ b/src/daemon.h @@ -51,4 +51,4 @@ void signal_handler(int signal); void go_daemon(void (*function)()); -#endif \ No newline at end of file +#endif diff --git a/src/global.h b/src/global.h index d524407..4c32551 100644 --- a/src/global.h +++ b/src/global.h @@ -1,13 +1,13 @@ #ifndef _GLOBAL_H_ #define _GLOBAL_H_ +#define PROGRAM_NAME "mbpfan" +#define PROGRAM_VERSION "2.2.0" +#define PROGRAM_PID "/var/run/mbpfan.pid" + extern int daemonize; extern int verbose; -extern const char* PROGRAM_NAME; -extern const char* PROGRAM_VERSION; -extern const char* PROGRAM_PID; - struct s_sensors { FILE* file; char* path; diff --git a/src/main.c b/src/main.c index 2b797c9..41bc86f 100644 --- a/src/main.c +++ b/src/main.c @@ -23,26 +23,12 @@ #include #include #include -#include -#include #include #include "mbpfan.h" #include "daemon.h" #include "global.h" -#include "main.h" -#include "minunit.h" #include "util.h" -int daemonize = 1; -int verbose = 0; - -const char *PROGRAM_NAME = "mbpfan"; -const char *PROGRAM_VERSION = "2.2.0"; -const char *PROGRAM_PID = "/var/run/mbpfan.pid"; - -const char *CORETEMP_PATH = "/sys/devices/platform/coretemp.0"; -const char *APPLESMC_PATH = "/sys/devices/platform/applesmc.768"; - void print_usage(int argc, char *argv[]) { if (argc >=1) { @@ -50,57 +36,17 @@ void print_usage(int argc, char *argv[]) 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"); } } -void check_requirements() -{ - - /** - * Check for root - */ - - uid_t uid=getuid(), euid=geteuid(); - - if (uid != 0 || euid != 0) { - mbp_log(LOG_ERR, "%s needs root privileges. Please run %s as root. Exiting.", PROGRAM_NAME, PROGRAM_NAME); - exit(EXIT_FAILURE); - } - - /** - * Check for coretemp and applesmc modules - */ - DIR* dir = opendir(CORETEMP_PATH); - - if (ENOENT == errno) { - mbp_log(LOG_ERR, "%s needs coretemp support. Please either load it or build it into the kernel. Exiting.", PROGRAM_NAME); - exit(EXIT_FAILURE); - } - - closedir(dir); - - - dir = opendir(APPLESMC_PATH); - - if (ENOENT == errno) { - mbp_log(LOG_ERR, "%s needs applesmc support. Please either load it or build it into the kernel. Exiting.", PROGRAM_NAME); - exit(EXIT_FAILURE); - } - - closedir(dir); - - -} - int main(int argc, char *argv[]) { int c; - while( (c = getopt(argc, argv, "hftv|help")) != -1) { + while( (c = getopt(argc, argv, "hfv|help")) != -1) { switch(c) { case 'h': print_usage(argc, argv); @@ -111,9 +57,6 @@ int main(int argc, char *argv[]) daemonize = 0; break; - case 't': - return tests(); - case 'v': verbose = 1; break; @@ -125,7 +68,7 @@ int main(int argc, char *argv[]) } } - check_requirements(); + check_requirements(argv[0]); // pointer to mbpfan() function in mbpfan.c void (*fan_control)() = mbpfan; diff --git a/src/main.h b/src/main.h deleted file mode 100644 index 13013e8..0000000 --- a/src/main.h +++ /dev/null @@ -1 +0,0 @@ -void check_requirements(); diff --git a/src/mbpfan.c b/src/mbpfan.c index 129c708..1916d64 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -37,6 +37,8 @@ #include #include #include +#include +#include #include #include #include "mbpfan.h" @@ -48,6 +50,9 @@ #define min(a,b) ((a) < (b) ? (a) : (b)) #define max(a,b) ((a) > (b) ? (a) : (b)) +#define CORETEMP_PATH "/sys/devices/platform/coretemp.0" +#define APPLESMC_PATH "/sys/devices/platform/applesmc.768" + /* temperature thresholds * low_temp - temperature below which fan speed will be at minimum * high_temp - fan will increase speed when higher than this temperature @@ -525,6 +530,45 @@ void retrieve_settings(const char* settings_path, t_fans* fans) } } +void check_requirements(const char* program_path) +{ + + /** + * Check for root + */ + + uid_t uid=getuid(), euid=geteuid(); + + if (uid != 0 || euid != 0) { + mbp_log(LOG_ERR, "%s needs root privileges. Please run %s as root. Exiting.", program_path, program_path); + exit(EXIT_FAILURE); + } + + /** + * Check for coretemp and applesmc modules + */ + DIR* dir = opendir(CORETEMP_PATH); + + if (ENOENT == errno) { + mbp_log(LOG_ERR, "%s needs coretemp support. Please either load it or build it into the kernel. Exiting.", program_path); + exit(EXIT_FAILURE); + } + + closedir(dir); + + + dir = opendir(APPLESMC_PATH); + + if (ENOENT == errno) { + mbp_log(LOG_ERR, "%s needs applesmc support. Please either load it or build it into the kernel. Exiting.", program_path); + exit(EXIT_FAILURE); + } + + closedir(dir); + + +} + void mbpfan() { int old_temp, new_temp, fan_speed, steps; diff --git a/src/mbpfan.h b/src/mbpfan.h index ce9aa8c..f673a58 100644 --- a/src/mbpfan.h +++ b/src/mbpfan.h @@ -100,6 +100,12 @@ void set_fan_minimum_speed(t_fans* fans); */ unsigned short get_temp(t_sensors* sensors); +/** + * Check if user has proper access and that required + * kernel modules are available + */ +void check_requirements(const char* program_path); + /** * Main Program */ diff --git a/tests/main.c b/tests/main.c new file mode 100644 index 0000000..2870554 --- /dev/null +++ b/tests/main.c @@ -0,0 +1,7 @@ +#include "minunit.h" + +int main(int argc, char *argv[]) +{ + (void)argc; + tests(argv[0]); +} diff --git a/src/minunit.c b/tests/minunit.c similarity index 97% rename from src/minunit.c rename to tests/minunit.c index a68908c..5a54299 100644 --- a/src/minunit.c +++ b/tests/minunit.c @@ -6,10 +6,9 @@ #include #include #include -#include "global.h" -#include "mbpfan.h" -#include "settings.h" -#include "main.h" +#include "../src/global.h" +#include "../src/mbpfan.h" +#include "../src/settings.h" #include "minunit.h" int tests_run = 0; @@ -252,9 +251,11 @@ static const char *all_tests() return 0; } -int tests() +int tests(const char *program_path) { - check_requirements(); + verbose = 1; + + check_requirements(program_path); printf("Starting the tests..\n"); printf("It is normal for them to take a bit to finish.\n"); diff --git a/src/minunit.h b/tests/minunit.h similarity index 100% rename from src/minunit.h rename to tests/minunit.h