Fixes #27
This commit is contained in:
parent
8d51412bc9
commit
33a3710995
9 changed files with 82 additions and 55 deletions
30
src/daemon.c
Normal file → Executable file
30
src/daemon.c
Normal file → Executable file
|
@ -33,7 +33,7 @@
|
|||
int write_pid(int pid)
|
||||
{
|
||||
FILE *file = NULL;
|
||||
file = fopen(program_pid, "w");
|
||||
file = fopen(PROGRAM_PID, "w");
|
||||
|
||||
if(file != NULL) {
|
||||
fprintf(file, "%d", pid);
|
||||
|
@ -49,7 +49,7 @@ int read_pid()
|
|||
{
|
||||
FILE *file = NULL;
|
||||
int pid = -1;
|
||||
file = fopen(program_pid, "r");
|
||||
file = fopen(PROGRAM_PID, "r");
|
||||
|
||||
if(file != NULL) {
|
||||
fscanf(file, "%d", &pid);
|
||||
|
@ -62,7 +62,7 @@ int read_pid()
|
|||
|
||||
int delete_pid()
|
||||
{
|
||||
return remove(program_pid);
|
||||
return remove(PROGRAM_PID);
|
||||
}
|
||||
|
||||
|
||||
|
@ -106,16 +106,16 @@ void go_daemon(void (*fan_control)())
|
|||
signal(SIGTERM, signal_handler);
|
||||
signal(SIGINT, signal_handler);
|
||||
|
||||
syslog(LOG_INFO, "%s starting up", program_name);
|
||||
syslog(LOG_INFO, "%s starting up", PROGRAM_NAME);
|
||||
|
||||
// Setup syslog logging - see SETLOGMASK(3)
|
||||
if(verbose) {
|
||||
setlogmask(LOG_UPTO(LOG_DEBUG));
|
||||
openlog(program_name, LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER);
|
||||
openlog(PROGRAM_NAME, LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER);
|
||||
|
||||
} else {
|
||||
setlogmask(LOG_UPTO(LOG_INFO));
|
||||
openlog(program_name, LOG_CONS, LOG_USER);
|
||||
openlog(PROGRAM_NAME, LOG_CONS, LOG_USER);
|
||||
}
|
||||
|
||||
|
||||
|
@ -161,31 +161,31 @@ void go_daemon(void (*fan_control)())
|
|||
|
||||
if (read_pid() == -1) {
|
||||
if (verbose) {
|
||||
printf("Writing a new .pid file with value %d at: %s\n", current_pid, program_pid);
|
||||
syslog(LOG_INFO, "Writing a new .pid file with value %d at: %s", current_pid, program_pid);
|
||||
printf("Writing a new .pid file with value %d at: %s\n", current_pid, PROGRAM_PID);
|
||||
syslog(LOG_INFO, "Writing a new .pid file with value %d at: %s", current_pid, PROGRAM_PID);
|
||||
}
|
||||
|
||||
if (write_pid(current_pid) == 0) {
|
||||
syslog(LOG_ERR, "Can not create a .pid file at: %s. Aborting", program_pid);
|
||||
syslog(LOG_ERR, "Can not create a .pid file at: %s. Aborting", PROGRAM_PID);
|
||||
|
||||
if (verbose) {
|
||||
printf("ERROR: Can not create a .pid file at: %s. Aborting\n", program_pid);
|
||||
printf("ERROR: Can not create a .pid file at: %s. Aborting\n", PROGRAM_PID);
|
||||
}
|
||||
|
||||
exit(EXIT_FAILURE);
|
||||
|
||||
} else {
|
||||
if (verbose) {
|
||||
printf("Successfully written a new .pid file with value %d at: %s\n", current_pid, program_pid);
|
||||
syslog(LOG_INFO, "Successfully written a new .pid file with value %d at: %s", current_pid, program_pid);
|
||||
printf("Successfully written a new .pid file with value %d at: %s\n", current_pid, PROGRAM_PID);
|
||||
syslog(LOG_INFO, "Successfully written a new .pid file with value %d at: %s", current_pid, PROGRAM_PID);
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
syslog(LOG_ERR, "A previously created .pid file exists at: %s. Aborting", program_pid);
|
||||
syslog(LOG_ERR, "A previously created .pid file exists at: %s. Aborting", PROGRAM_PID);
|
||||
|
||||
if (verbose) {
|
||||
printf("ERROR: a previously created .pid file exists at: %s.\n Aborting\n", program_pid);
|
||||
printf("ERROR: a previously created .pid file exists at: %s.\n Aborting\n", PROGRAM_PID);
|
||||
}
|
||||
|
||||
exit(EXIT_FAILURE);
|
||||
|
@ -195,7 +195,7 @@ void go_daemon(void (*fan_control)())
|
|||
fan_control();
|
||||
|
||||
if(daemonize) {
|
||||
syslog(LOG_INFO, "%s daemon exiting", program_name);
|
||||
syslog(LOG_INFO, "%s daemon exiting", PROGRAM_NAME);
|
||||
}
|
||||
|
||||
return;
|
||||
|
|
|
@ -4,8 +4,8 @@
|
|||
extern int daemonize;
|
||||
extern int verbose;
|
||||
|
||||
extern const char* program_name;
|
||||
extern const char* program_pid;
|
||||
extern const char* PROGRAM_NAME;
|
||||
extern const char* PROGRAM_PID;
|
||||
|
||||
struct s_sensors {
|
||||
char* path;
|
||||
|
|
93
src/main.c
93
src/main.c
|
@ -20,17 +20,23 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <syslog.h>
|
||||
#include <sys/types.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include "mbpfan.h"
|
||||
#include "daemon.h"
|
||||
#include "global.h"
|
||||
#include "minunit.h"
|
||||
#include <syslog.h>
|
||||
|
||||
int daemonize = 1;
|
||||
int verbose = 0;
|
||||
|
||||
const char *program_name = "mbpfan";
|
||||
const char *program_pid = "/var/run/mbpfan.pid";
|
||||
const char *PROGRAM_NAME = "mbpfan";
|
||||
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[])
|
||||
{
|
||||
|
@ -46,6 +52,54 @@ void print_usage(int argc, char *argv[])
|
|||
}
|
||||
|
||||
|
||||
void check_requirements()
|
||||
{
|
||||
|
||||
/**
|
||||
* Check for root
|
||||
*/
|
||||
|
||||
uid_t uid=getuid(), euid=geteuid();
|
||||
|
||||
if (uid != 0 || euid != 0) {
|
||||
syslog(LOG_INFO, "%s needs root privileges. Please run %s as root. Exiting.", PROGRAM_NAME, PROGRAM_NAME);
|
||||
printf("%s not started with root privileges. Please run %s as root. Exiting.\n", PROGRAM_NAME, PROGRAM_NAME);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)) {
|
||||
DIR* dir = opendir(CORETEMP_PATH);
|
||||
|
||||
if (ENOENT == errno) {
|
||||
syslog(LOG_INFO, "%s needs coretemp support. Please either load it or build it into the kernel. Exiting.", PROGRAM_NAME);
|
||||
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");
|
||||
|
||||
if (!(fread (buf, 1, sizeof (buf), fd) > 0)) {
|
||||
DIR* dir = opendir(APPLESMC_PATH);
|
||||
|
||||
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);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
|
@ -75,40 +129,9 @@ int main(int argc, char *argv[])
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check for root
|
||||
*/
|
||||
|
||||
uid_t uid=getuid(), euid=geteuid();
|
||||
|
||||
if (uid != 0 || euid != 0) {
|
||||
syslog(LOG_INFO, "Mbpfan needs root privileges. Please run mbpfan as root. Exiting.");
|
||||
printf("Mbpfan not started with root privileges. Please run mbpfan as root. Exiting.\n");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)) {
|
||||
syslog(LOG_INFO, "Mbpfan needs coretemp module. Please load it and run mbpfan again. Exiting.");
|
||||
printf("Mbpfan needs coretemp module. Please load it and run mbpfan again. Exiting.\n");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
fd = popen("lsmod | grep applesmc", "r");
|
||||
|
||||
if (!(fread (buf, 1, sizeof (buf), fd) > 0)) {
|
||||
syslog(LOG_INFO, "Mbpfan needs applesmc module. Please load it and run mbpfan again. Exiting.");
|
||||
printf("Mbpfan needs applescm module. Please load it and run mbpfan again. Exiting.\n");
|
||||
exit(0);
|
||||
}
|
||||
check_requirements();
|
||||
|
||||
// pointer to mbpfan() function in mbpfan.c
|
||||
void (*fan_control)() = mbpfan;
|
||||
|
|
10
src/mbpfan.c
10
src/mbpfan.c
|
@ -76,6 +76,7 @@ t_sensors *retrieve_sensors()
|
|||
|
||||
int counter = 0;
|
||||
int sensors_found = 0;
|
||||
|
||||
for(counter = 0; counter<10; counter++) {
|
||||
path = (char*) malloc(sizeof( char ) * path_size);
|
||||
|
||||
|
@ -139,7 +140,7 @@ t_fans *retrieve_fans()
|
|||
const char *path_begin = "/sys/devices/platform/applesmc.768/fan";
|
||||
const char *path_output_end = "_output";
|
||||
const char *path_man_end = "_manual";
|
||||
|
||||
|
||||
int path_min_size = strlen(path_begin) + strlen(path_output_end) + 2;
|
||||
int path_man_size = strlen(path_begin) + strlen(path_man_end) + 2;
|
||||
char number[2];
|
||||
|
@ -149,7 +150,7 @@ t_fans *retrieve_fans()
|
|||
int fans_found = 0;
|
||||
|
||||
for(int counter = 0; counter<10; counter++) {
|
||||
|
||||
|
||||
path_output = (char*) malloc(sizeof( char ) * path_min_size);
|
||||
path_output[0] = '\0';
|
||||
path_manual = (char*) malloc(sizeof( char ) * path_man_size);
|
||||
|
@ -201,6 +202,7 @@ t_fans *retrieve_fans()
|
|||
|
||||
if(verbose) {
|
||||
printf("Found %d fans\n", fans_found);
|
||||
|
||||
if(daemonize) {
|
||||
syslog(LOG_INFO, "Found %d fans", fans_found);
|
||||
}
|
||||
|
@ -278,6 +280,7 @@ unsigned short get_temp(t_sensors* sensors)
|
|||
t_sensors* tmp = sensors;
|
||||
|
||||
int number_sensors = 0;
|
||||
|
||||
while(tmp != NULL) {
|
||||
sum_temp += tmp->temperature;
|
||||
tmp = tmp->next;
|
||||
|
@ -285,8 +288,9 @@ unsigned short get_temp(t_sensors* sensors)
|
|||
}
|
||||
|
||||
// just to be safe
|
||||
if (number_sensors == 0)
|
||||
if (number_sensors == 0) {
|
||||
number_sensors++;
|
||||
}
|
||||
|
||||
temp = (unsigned short)( ceil( (float)( sum_temp ) / (number_sensors * 1000) ) );
|
||||
return temp;
|
||||
|
|
0
src/minunit.c
Normal file → Executable file
0
src/minunit.c
Normal file → Executable file
0
src/settings.c
Normal file → Executable file
0
src/settings.c
Normal file → Executable file
0
src/settings.h
Normal file → Executable file
0
src/settings.h
Normal file → Executable file
0
src/strmap.c
Normal file → Executable file
0
src/strmap.c
Normal file → Executable file
0
src/strmap.h
Normal file → Executable file
0
src/strmap.h
Normal file → Executable file
Loading…
Add table
Reference in a new issue