Skip to content

Commit 52d8973

Browse files
authored
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
1 parent 10d6feb commit 52d8973

File tree

13 files changed

+90
-82
lines changed

13 files changed

+90
-82
lines changed

Makefile

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ C = c
44
OBJ = o
55
OUTPUT_PATH = bin/
66
SOURCE_PATH = src/
7+
TESTS_PATH = tests/
8+
TESTS_BIN = bin/mbpfan-tests
79
BIN = bin/mbpfan
810
CONF = mbpfan.conf
911
DOC = README.md
@@ -20,24 +22,32 @@ CFLAGS += $(COPT) -g $(INCLUDES) -Wall -Wextra -Wno-unused-function -std=c99 -D_
2022
LDFLAGS += $(LIBPATH) -g
2123

2224
OBJS := $(patsubst %.$(C),%.$(OBJ),$(wildcard $(SOURCE_PATH)*.$(C)))
25+
TESTS_OBJS := $(patsubst %.$(C),%.$(OBJ),$(wildcard $(TESTS_PATH)*.$(C)))
26+
TESTS_OBJS += $(filter-out %main.$(OBJ),$(OBJS))
27+
28+
.PHONY: all clean tests uninstall install rebuild
2329

2430
%.$(OBJ):%.$(C)
2531
mkdir -p bin
2632
@echo Compiling $(basename $<)...
2733
$(CC) -c $(CFLAGS) $< $(OBJFLAG)$@
2834

29-
all: $(BIN)
35+
all: $(BIN) $(TESTS_BIN)
3036

3137
$(BIN): $(OBJS)
3238
@echo Linking...
3339
$(CC) $(LDFLAGS) $^ $(LIBS) $(BINFLAG) $(BIN)
3440

41+
$(TESTS_BIN): $(TESTS_OBJS)
42+
@echo Linking...
43+
$(CC) $(LDFLAGS) $^ $(LIBS) $(BINFLAG) $(TESTS_BIN)
44+
3545
clean:
3646
rm -rf $(SOURCE_PATH)*.$(OBJ) $(BIN)
47+
rm -rf $(TESTS_PATH)*.$(OBJ) $(TESTS_BIN)
3748

38-
tests:
39-
make install
40-
/usr/sbin/mbpfan -f -v -t
49+
tests: all
50+
./bin/mbpfan-tests
4151

4252
uninstall:
4353
rm /usr/sbin/mbpfan
@@ -46,7 +56,7 @@ uninstall:
4656
rm /usr/share/man/man8/mbpfan.8.gz
4757
rm -rf /usr/share/doc/mbpfan
4858

49-
install: $(BIN)
59+
install: all
5060
install -d $(DESTDIR)/usr/sbin
5161
install -d $(DESTDIR)/etc
5262
install -d $(DESTDIR)/lib/systemd/system

README.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,7 @@ default compiler to be Clang. Tested with Clang 3.8 and 3.9. Tested with Clang
155155

156156
Run The Tests (Optional)
157157
------------------------
158-
Users may run the tests after installing the program. Please run the following command _from within the source directory_.
159-
160-
sudo ./bin/mbpfan -t
161-
162-
or
158+
Users may run the tests after building the program. Please run the following command _from within the source directory_.
163159

164160
sudo make tests
165161

@@ -233,7 +229,6 @@ execute the following (as root):
233229

234230
-h Show the help screen
235231
-f Run in foreground
236-
-t Run the tests
237232
-v Be (a lot) verbose
238233

239234
## References

mbpfan.8.gz

-8 Bytes
Binary file not shown.

src/daemon.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
#include "daemon.h"
3636
#include "util.h"
3737

38+
int daemonize = 1;
39+
int verbose = 0;
40+
3841
int write_pid(int pid)
3942
{
4043
FILE *file = NULL;

src/daemon.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,4 @@ void signal_handler(int signal);
5151
void go_daemon(void (*function)());
5252

5353

54-
#endif
54+
#endif

src/global.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#ifndef _GLOBAL_H_
22
#define _GLOBAL_H_
33

4+
#define PROGRAM_NAME "mbpfan"
5+
#define PROGRAM_VERSION "2.2.0"
6+
#define PROGRAM_PID "/var/run/mbpfan.pid"
7+
48
extern int daemonize;
59
extern int verbose;
610

7-
extern const char* PROGRAM_NAME;
8-
extern const char* PROGRAM_VERSION;
9-
extern const char* PROGRAM_PID;
10-
1111
struct s_sensors {
1212
FILE* file;
1313
char* path;

src/main.c

Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -23,84 +23,30 @@
2323
#include <unistd.h>
2424
#include <syslog.h>
2525
#include <stdbool.h>
26-
#include <sys/types.h>
27-
#include <dirent.h>
2826
#include <errno.h>
2927
#include "mbpfan.h"
3028
#include "daemon.h"
3129
#include "global.h"
32-
#include "main.h"
33-
#include "minunit.h"
3430
#include "util.h"
3531

36-
int daemonize = 1;
37-
int verbose = 0;
38-
39-
const char *PROGRAM_NAME = "mbpfan";
40-
const char *PROGRAM_VERSION = "2.2.0";
41-
const char *PROGRAM_PID = "/var/run/mbpfan.pid";
42-
43-
const char *CORETEMP_PATH = "/sys/devices/platform/coretemp.0";
44-
const char *APPLESMC_PATH = "/sys/devices/platform/applesmc.768";
45-
4632
void print_usage(int argc, char *argv[])
4733
{
4834
if (argc >=1) {
4935
printf("Usage: %s OPTION(S) \n", argv[0]);
5036
printf("Options:\n");
5137
printf("\t-h Show this help screen\n");
5238
printf("\t-f Run in foreground\n");
53-
printf("\t-t Run the tests\n");
5439
printf("\t-v Be (a lot) verbose\n");
5540
printf("\n");
5641
}
5742
}
5843

59-
void check_requirements()
60-
{
61-
62-
/**
63-
* Check for root
64-
*/
65-
66-
uid_t uid=getuid(), euid=geteuid();
67-
68-
if (uid != 0 || euid != 0) {
69-
mbp_log(LOG_ERR, "%s needs root privileges. Please run %s as root. Exiting.", PROGRAM_NAME, PROGRAM_NAME);
70-
exit(EXIT_FAILURE);
71-
}
72-
73-
/**
74-
* Check for coretemp and applesmc modules
75-
*/
76-
DIR* dir = opendir(CORETEMP_PATH);
77-
78-
if (ENOENT == errno) {
79-
mbp_log(LOG_ERR, "%s needs coretemp support. Please either load it or build it into the kernel. Exiting.", PROGRAM_NAME);
80-
exit(EXIT_FAILURE);
81-
}
82-
83-
closedir(dir);
84-
85-
86-
dir = opendir(APPLESMC_PATH);
87-
88-
if (ENOENT == errno) {
89-
mbp_log(LOG_ERR, "%s needs applesmc support. Please either load it or build it into the kernel. Exiting.", PROGRAM_NAME);
90-
exit(EXIT_FAILURE);
91-
}
92-
93-
closedir(dir);
94-
95-
96-
}
97-
9844
int main(int argc, char *argv[])
9945
{
10046

10147
int c;
10248

103-
while( (c = getopt(argc, argv, "hftv|help")) != -1) {
49+
while( (c = getopt(argc, argv, "hfv|help")) != -1) {
10450
switch(c) {
10551
case 'h':
10652
print_usage(argc, argv);
@@ -111,9 +57,6 @@ int main(int argc, char *argv[])
11157
daemonize = 0;
11258
break;
11359

114-
case 't':
115-
return tests();
116-
11760
case 'v':
11861
verbose = 1;
11962
break;
@@ -125,7 +68,7 @@ int main(int argc, char *argv[])
12568
}
12669
}
12770

128-
check_requirements();
71+
check_requirements(argv[0]);
12972

13073
// pointer to mbpfan() function in mbpfan.c
13174
void (*fan_control)() = mbpfan;

src/main.h

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/mbpfan.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
#include <math.h>
3838
#include <syslog.h>
3939
#include <stdbool.h>
40+
#include <dirent.h>
41+
#include <sys/types.h>
4042
#include <sys/utsname.h>
4143
#include <sys/errno.h>
4244
#include "mbpfan.h"
@@ -48,6 +50,9 @@
4850
#define min(a,b) ((a) < (b) ? (a) : (b))
4951
#define max(a,b) ((a) > (b) ? (a) : (b))
5052

53+
#define CORETEMP_PATH "/sys/devices/platform/coretemp.0"
54+
#define APPLESMC_PATH "/sys/devices/platform/applesmc.768"
55+
5156
/* temperature thresholds
5257
* low_temp - temperature below which fan speed will be at minimum
5358
* 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)
525530
}
526531
}
527532

533+
void check_requirements(const char* program_path)
534+
{
535+
536+
/**
537+
* Check for root
538+
*/
539+
540+
uid_t uid=getuid(), euid=geteuid();
541+
542+
if (uid != 0 || euid != 0) {
543+
mbp_log(LOG_ERR, "%s needs root privileges. Please run %s as root. Exiting.", program_path, program_path);
544+
exit(EXIT_FAILURE);
545+
}
546+
547+
/**
548+
* Check for coretemp and applesmc modules
549+
*/
550+
DIR* dir = opendir(CORETEMP_PATH);
551+
552+
if (ENOENT == errno) {
553+
mbp_log(LOG_ERR, "%s needs coretemp support. Please either load it or build it into the kernel. Exiting.", program_path);
554+
exit(EXIT_FAILURE);
555+
}
556+
557+
closedir(dir);
558+
559+
560+
dir = opendir(APPLESMC_PATH);
561+
562+
if (ENOENT == errno) {
563+
mbp_log(LOG_ERR, "%s needs applesmc support. Please either load it or build it into the kernel. Exiting.", program_path);
564+
exit(EXIT_FAILURE);
565+
}
566+
567+
closedir(dir);
568+
569+
570+
}
571+
528572
void mbpfan()
529573
{
530574
int old_temp, new_temp, fan_speed, steps;

src/mbpfan.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ void set_fan_minimum_speed(t_fans* fans);
100100
*/
101101
unsigned short get_temp(t_sensors* sensors);
102102

103+
/**
104+
* Check if user has proper access and that required
105+
* kernel modules are available
106+
*/
107+
void check_requirements(const char* program_path);
108+
103109
/**
104110
* Main Program
105111
*/

tests/main.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "minunit.h"
2+
3+
int main(int argc, char *argv[])
4+
{
5+
(void)argc;
6+
tests(argv[0]);
7+
}

src/minunit.c renamed to tests/minunit.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
#include <signal.h>
77
#include <stdbool.h>
88
#include <sys/utsname.h>
9-
#include "global.h"
10-
#include "mbpfan.h"
11-
#include "settings.h"
12-
#include "main.h"
9+
#include "../src/global.h"
10+
#include "../src/mbpfan.h"
11+
#include "../src/settings.h"
1312
#include "minunit.h"
1413

1514
int tests_run = 0;
@@ -252,9 +251,11 @@ static const char *all_tests()
252251
return 0;
253252
}
254253

255-
int tests()
254+
int tests(const char *program_path)
256255
{
257-
check_requirements();
256+
verbose = 1;
257+
258+
check_requirements(program_path);
258259

259260
printf("Starting the tests..\n");
260261
printf("It is normal for them to take a bit to finish.\n");
File renamed without changes.

0 commit comments

Comments
 (0)