Skip to content

Commit

Permalink
Added support for configfile /etc/mbpfan.conf
Browse files Browse the repository at this point in the history
  • Loading branch information
CeRiAl committed Jul 4, 2012
1 parent 1b84085 commit 6ea021a
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 13 deletions.
8 changes: 4 additions & 4 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
MAINTAINERS AND CONTRIBUTORS
----------------------------

Daniel Graziotin <dgraziotin AT task3 DOT cc>
CeRiAl <ikhatib AT imail DOT de>
Daniel Graziotin <dgraziotin AT task3 DOT cc>
Ismail Khatib <ikhatib AT gmail DOT com>


ORIGINARY AUTHORS
-----------------

Allan McRae mbpfan <http://allanmcrae.com/2010/05/simple-macbook-pro-fan-daemon>
rvega <https://github.com/rvega/Fan-Control-Daemon>
Allan McRae mbpfan <http://allanmcrae.com/2010/05/simple-macbook-pro-fan-daemon>
rvega <https://github.com/rvega/Fan-Control-Daemon>
22 changes: 20 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ C = c
OUTPUT_PATH = bin/
SOURCE_PATH = src/
EXE = bin/mbpfan
CONF = mbpfan.conf

ifeq ($(COMPILER), G++)
ifeq ($(OS),Windows_NT)
Expand Down Expand Up @@ -55,12 +56,29 @@ clean:

install:
cp $(EXE) /usr/sbin
cp -n $(CONF) /etc
@echo "-----------------------------------------------------------------------------"
@echo "An init file suitable for /lib/lsb/init-functions (Debian & Ubuntu fur sure)"
@echo "Is located in the main folder of the source files. It is called mbpfan.init."
@echo "An init file suitable for /lib/lsb/init-functions (Debian & Ubuntu for sure)"
@echo "is located in the main folder of the source files, called mbpfan.init.debian"
@echo "Rename it to mbpfan, give it execution permissions (chmod +x mbpfan)"
@echo "and move it to /etc/init.d"
@echo "Then, add it to the default runlevels with sudo update-rc.d mbpfan defaults"
@echo ""
@echo "Additionally, an init file suitable for /etc/rc.d/init.d/functions"
@echo "(RHEL/CentOS & Fedora) is also located at the same place, this file is called"
@echo "mbpfan.init.redhat. Also rename it to mbpfan, give it execution permissions"
@echo "and move it to /etc/init.d"
@echo "To add the script to the default runlevels, run the following as root:"
@echo "chkconfig --level 2345 mbpfan on && chkconfig --level 016 mbpfan off"
@echo ""
@echo "As a special bonus, a service file for systemd is also included. To use it,"
@echo "execute the following as root:"
@echo "cp mbpfan.service /usr/lib/systemd/system"
@echo "ln -s /usr/lib/systemd/system/mbpfan.service /etc/systemd/system/mbpfan.service"
@echo "systemctl daemon-reload"
@echo "systemctl start mbpfan.service"
@echo "To start the service automatically at boot, also execute the following:"
@echo "systemctl enable mbpfan.service"
@echo "-----------------------------------------------------------------------------"

rebuild: clean all
Expand Down
7 changes: 7 additions & 0 deletions mbpfan.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[general]
min_fan_speed = 2000 # default is 2000
max_fan_speed = 6200 # default is 6200
low_temp = 63 # try ranges 55-63, default is 63
high_temp = 66 # try ranges 58-66, default is 66
max_temp = 86 # do not set it > 90, default is 86
polling_interval = 7 # default is 7
7 changes: 4 additions & 3 deletions src/daemon.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* Copyright (C) 2012 Peter Lombardo <http://peterlombardo.wikidot.com/linux-daemon-in-c>
* Modifications (2012) by Daniel Graziotin <[email protected]>
* Modifications (2012) by Ismail Khatib <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -162,23 +163,23 @@ void go_daemon(void (*fan_control)())
{
if (verbose)
{
printf("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);
if (verbose)
{
printf("ERROR: Can not create a .pid file at: %s. Aborting", 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", 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);
}
}
Expand Down
64 changes: 60 additions & 4 deletions src/mbpfan.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright (C) 2010 Allan McRae <[email protected]>
* Modifications by Rafael Vega <[email protected]>
* Modifications (2012) by Daniel Graziotin <[email protected]>
* Modifications (2012) by Ismail Khatib <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -24,7 +25,8 @@
*
* Tested models:
* MacBook Pro 8.1 13" (Intel i7 - Linux 3.2)
* Macbook Pro 6,2 15" (Intel i7 - Linux 3.2)
* MacBook Pro 6,2 15" (Intel i7 - Linux 3.2)
* MacBook Pro 2,2 15" (Intel Core 2 Duo - Linux 3.4.4)
*/


Expand All @@ -36,6 +38,7 @@
#include <syslog.h>
#include "mbpfan.h"
#include "global.h"
#include "settings.h"

/* lazy min/max... */
#define min(a,b) a < b ? a : b
Expand Down Expand Up @@ -273,19 +276,72 @@ void mbpfan()
int old_temp, new_temp, fan_speed, steps;
int temp_change;
int step_up, step_down;
FILE *f = NULL;
Settings *settings = NULL;
int result = 0;

t_sensors* sensors = retrieve_sensors();
set_fans_man(sensors);
new_temp = get_temp(sensors);
fan_speed = 2000;
set_fan_speed(sensors, fan_speed);

f = fopen("/etc/mbpfan.conf", "r");
if (f == NULL)
{
/* Could not open configfile */
if(verbose)
{
printf("Couldn't open configfile, using defaults\n");
if(daemonize)
{
syslog(LOG_INFO, "Couldn't open configfile, using defaults");
}
}
}
else
{
settings = settings_open(f);
fclose(f);
if (settings == NULL)
{
/* Could not read configfile */
if(verbose)
{
printf("Couldn't read configfile\n");
if(daemonize)
{
syslog(LOG_INFO, "Couldn't read configfile");
}
}
}
else
{
/* Read configfile values */
result = settings_get_int(settings, "general", "min_fan_speed");
if (result != 0) min_fan_speed = result;
result = settings_get_int(settings, "general", "max_fan_speed");
if (result != 0) max_fan_speed = result;
result = settings_get_int(settings, "general", "low_temp");
if (result != 0) low_temp = result;
result = settings_get_int(settings, "general", "high_temp");
if (result != 0) high_temp = result;
result = settings_get_int(settings, "general", "max_temp");
if (result != 0) max_temp = result;
result = settings_get_int(settings, "general", "polling_interval");
if (result != 0) polling_interval = result;

/* Destroy the settings object */
settings_delete(settings);
}
}

if(verbose)
{
printf("Sleeping for %d seconds\n", polling_interval);
if(daemonize)
{
syslog(LOG_INFO, "Sleeping for %d seconds\n", polling_interval);
syslog(LOG_INFO, "Sleeping for %d seconds", polling_interval);
}
}
sleep(polling_interval);
Expand Down Expand Up @@ -330,7 +386,7 @@ void mbpfan()
printf("Old Temp %d: New Temp: %d, Fan Speed: %d\n", old_temp, new_temp, fan_speed);
if(daemonize)
{
syslog(LOG_INFO, "Old Temp %d: New Temp: %d, Fan Speed: %d\n", old_temp, new_temp, fan_speed);
syslog(LOG_INFO, "Old Temp %d: New Temp: %d, Fan Speed: %d", old_temp, new_temp, fan_speed);
}
}

Expand All @@ -341,7 +397,7 @@ void mbpfan()
printf("Sleeping for %d seconds\n", polling_interval);
if(daemonize)
{
syslog(LOG_INFO, "Sleeping for %d seconds\n", polling_interval);
syslog(LOG_INFO, "Sleeping for %d seconds", polling_interval);
}
}
sleep(polling_interval);
Expand Down

0 comments on commit 6ea021a

Please sign in to comment.