Skip to content

Commit

Permalink
Moved tests to separate binary (mbpfan-tests)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
localscope authored May 19, 2020
1 parent 10d6feb commit 52d8973
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 82 deletions.
20 changes: 15 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
7 changes: 1 addition & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
Binary file modified mbpfan.8.gz
Binary file not shown.
3 changes: 3 additions & 0 deletions src/daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
#include "daemon.h"
#include "util.h"

int daemonize = 1;
int verbose = 0;

int write_pid(int pid)
{
FILE *file = NULL;
Expand Down
2 changes: 1 addition & 1 deletion src/daemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ void signal_handler(int signal);
void go_daemon(void (*function)());


#endif
#endif
8 changes: 4 additions & 4 deletions src/global.h
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
61 changes: 2 additions & 59 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,84 +23,30 @@
#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) {
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");
}
}

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);
Expand All @@ -111,9 +57,6 @@ int main(int argc, char *argv[])
daemonize = 0;
break;

case 't':
return tests();

case 'v':
verbose = 1;
break;
Expand All @@ -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;
Expand Down
1 change: 0 additions & 1 deletion src/main.h

This file was deleted.

44 changes: 44 additions & 0 deletions src/mbpfan.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions src/mbpfan.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
7 changes: 7 additions & 0 deletions tests/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "minunit.h"

int main(int argc, char *argv[])
{
(void)argc;
tests(argv[0]);
}
13 changes: 7 additions & 6 deletions src/minunit.c → tests/minunit.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down
File renamed without changes.

0 comments on commit 52d8973

Please sign in to comment.