Commit 33a37109 authored by Daniel Graziotin's avatar Daniel Graziotin

Fixes #27

parent 8d51412b
...@@ -33,7 +33,7 @@ ...@@ -33,7 +33,7 @@
int write_pid(int pid) int write_pid(int pid)
{ {
FILE *file = NULL; FILE *file = NULL;
file = fopen(program_pid, "w"); file = fopen(PROGRAM_PID, "w");
if(file != NULL) { if(file != NULL) {
fprintf(file, "%d", pid); fprintf(file, "%d", pid);
...@@ -49,7 +49,7 @@ int read_pid() ...@@ -49,7 +49,7 @@ int read_pid()
{ {
FILE *file = NULL; FILE *file = NULL;
int pid = -1; int pid = -1;
file = fopen(program_pid, "r"); file = fopen(PROGRAM_PID, "r");
if(file != NULL) { if(file != NULL) {
fscanf(file, "%d", &pid); fscanf(file, "%d", &pid);
...@@ -62,7 +62,7 @@ int read_pid() ...@@ -62,7 +62,7 @@ int read_pid()
int delete_pid() int delete_pid()
{ {
return remove(program_pid); return remove(PROGRAM_PID);
} }
...@@ -106,16 +106,16 @@ void go_daemon(void (*fan_control)()) ...@@ -106,16 +106,16 @@ void go_daemon(void (*fan_control)())
signal(SIGTERM, signal_handler); signal(SIGTERM, signal_handler);
signal(SIGINT, 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) // Setup syslog logging - see SETLOGMASK(3)
if(verbose) { if(verbose) {
setlogmask(LOG_UPTO(LOG_DEBUG)); 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 { } else {
setlogmask(LOG_UPTO(LOG_INFO)); 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)()) ...@@ -161,31 +161,31 @@ void go_daemon(void (*fan_control)())
if (read_pid() == -1) { if (read_pid() == -1) {
if (verbose) { if (verbose) {
printf("Writing a new .pid file with value %d at: %s\n", 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); syslog(LOG_INFO, "Writing a new .pid file with value %d at: %s", current_pid, PROGRAM_PID);
} }
if (write_pid(current_pid) == 0) { 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) { 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); exit(EXIT_FAILURE);
} else { } else {
if (verbose) { if (verbose) {
printf("Successfully written a new .pid file with value %d at: %s\n", 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); syslog(LOG_INFO, "Successfully written a new .pid file with value %d at: %s", current_pid, PROGRAM_PID);
} }
} }
} else { } 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) { 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); exit(EXIT_FAILURE);
...@@ -195,7 +195,7 @@ void go_daemon(void (*fan_control)()) ...@@ -195,7 +195,7 @@ void go_daemon(void (*fan_control)())
fan_control(); fan_control();
if(daemonize) { if(daemonize) {
syslog(LOG_INFO, "%s daemon exiting", program_name); syslog(LOG_INFO, "%s daemon exiting", PROGRAM_NAME);
} }
return; return;
......
...@@ -4,8 +4,8 @@ ...@@ -4,8 +4,8 @@
extern int daemonize; extern int daemonize;
extern int verbose; extern int verbose;
extern const char* program_name; extern const char* PROGRAM_NAME;
extern const char* program_pid; extern const char* PROGRAM_PID;
struct s_sensors { struct s_sensors {
char* path; char* path;
......
...@@ -20,17 +20,23 @@ ...@@ -20,17 +20,23 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <syslog.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include "mbpfan.h" #include "mbpfan.h"
#include "daemon.h" #include "daemon.h"
#include "global.h" #include "global.h"
#include "minunit.h" #include "minunit.h"
#include <syslog.h>
int daemonize = 1; int daemonize = 1;
int verbose = 0; int verbose = 0;
const char *program_name = "mbpfan"; const char *PROGRAM_NAME = "mbpfan";
const char *program_pid = "/var/run/mbpfan.pid"; 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[]) void print_usage(int argc, char *argv[])
{ {
...@@ -46,6 +52,54 @@ 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[]) int main(int argc, char *argv[])
{ {
...@@ -75,40 +129,9 @@ 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) { check_requirements();
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);
}
// pointer to mbpfan() function in mbpfan.c // pointer to mbpfan() function in mbpfan.c
void (*fan_control)() = mbpfan; void (*fan_control)() = mbpfan;
......
...@@ -76,6 +76,7 @@ t_sensors *retrieve_sensors() ...@@ -76,6 +76,7 @@ t_sensors *retrieve_sensors()
int counter = 0; int counter = 0;
int sensors_found = 0; int sensors_found = 0;
for(counter = 0; counter<10; counter++) { for(counter = 0; counter<10; counter++) {
path = (char*) malloc(sizeof( char ) * path_size); path = (char*) malloc(sizeof( char ) * path_size);
...@@ -139,7 +140,7 @@ t_fans *retrieve_fans() ...@@ -139,7 +140,7 @@ t_fans *retrieve_fans()
const char *path_begin = "/sys/devices/platform/applesmc.768/fan"; const char *path_begin = "/sys/devices/platform/applesmc.768/fan";
const char *path_output_end = "_output"; const char *path_output_end = "_output";
const char *path_man_end = "_manual"; const char *path_man_end = "_manual";
int path_min_size = strlen(path_begin) + strlen(path_output_end) + 2; int path_min_size = strlen(path_begin) + strlen(path_output_end) + 2;
int path_man_size = strlen(path_begin) + strlen(path_man_end) + 2; int path_man_size = strlen(path_begin) + strlen(path_man_end) + 2;
char number[2]; char number[2];
...@@ -149,7 +150,7 @@ t_fans *retrieve_fans() ...@@ -149,7 +150,7 @@ t_fans *retrieve_fans()
int fans_found = 0; int fans_found = 0;
for(int counter = 0; counter<10; counter++) { for(int counter = 0; counter<10; counter++) {
path_output = (char*) malloc(sizeof( char ) * path_min_size); path_output = (char*) malloc(sizeof( char ) * path_min_size);
path_output[0] = '\0'; path_output[0] = '\0';
path_manual = (char*) malloc(sizeof( char ) * path_man_size); path_manual = (char*) malloc(sizeof( char ) * path_man_size);
...@@ -201,6 +202,7 @@ t_fans *retrieve_fans() ...@@ -201,6 +202,7 @@ t_fans *retrieve_fans()
if(verbose) { if(verbose) {
printf("Found %d fans\n", fans_found); printf("Found %d fans\n", fans_found);
if(daemonize) { if(daemonize) {
syslog(LOG_INFO, "Found %d fans", fans_found); syslog(LOG_INFO, "Found %d fans", fans_found);
} }
...@@ -278,6 +280,7 @@ unsigned short get_temp(t_sensors* sensors) ...@@ -278,6 +280,7 @@ unsigned short get_temp(t_sensors* sensors)
t_sensors* tmp = sensors; t_sensors* tmp = sensors;
int number_sensors = 0; int number_sensors = 0;
while(tmp != NULL) { while(tmp != NULL) {
sum_temp += tmp->temperature; sum_temp += tmp->temperature;
tmp = tmp->next; tmp = tmp->next;
...@@ -285,8 +288,9 @@ unsigned short get_temp(t_sensors* sensors) ...@@ -285,8 +288,9 @@ unsigned short get_temp(t_sensors* sensors)
} }
// just to be safe // just to be safe
if (number_sensors == 0) if (number_sensors == 0) {
number_sensors++; number_sensors++;
}
temp = (unsigned short)( ceil( (float)( sum_temp ) / (number_sensors * 1000) ) ); temp = (unsigned short)( ceil( (float)( sum_temp ) / (number_sensors * 1000) ) );
return temp; return temp;
......
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment