Moved tests to separate binary (mbpfan-tests)

Removed instructions for using -t flag
Moved check_requirements function from main to mbpfan
Moved daemonize and verbose globals from main to daemon
Moved strings for program information from main to global.h
Moved strings for coretemp and applesmc paths from main to mbpfan.c
Updated Makefile
Turned on verboseness for testing
This commit is contained in:
localscope 2020-05-19 08:06:27 -06:00 committed by GitHub
parent 10d6febeb4
commit 52d897374d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 90 additions and 82 deletions

View file

@ -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

View file

@ -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

Binary file not shown.

View file

@ -35,6 +35,9 @@
#include "daemon.h"
#include "util.h"
int daemonize = 1;
int verbose = 0;
int write_pid(int pid)
{
FILE *file = NULL;

View file

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

View file

@ -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;

View file

@ -23,26 +23,12 @@
#include <unistd.h>
#include <syslog.h>
#include <stdbool.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#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;

View file

@ -1 +0,0 @@
void check_requirements();

View file

@ -37,6 +37,8 @@
#include <math.h>
#include <syslog.h>
#include <stdbool.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include <sys/errno.h>
#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;

View file

@ -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
*/

7
tests/main.c Normal file
View file

@ -0,0 +1,7 @@
#include "minunit.h"
int main(int argc, char *argv[])
{
(void)argc;
tests(argv[0]);
}

View file

@ -6,10 +6,9 @@
#include <signal.h>
#include <stdbool.h>
#include <sys/utsname.h>
#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");