From 6ea021ad785ccdde84df2fd26c535c380ebee74f Mon Sep 17 00:00:00 2001 From: CeRiAl Date: Thu, 5 Jul 2012 01:22:48 +0200 Subject: [PATCH] Added support for configfile /etc/mbpfan.conf --- AUTHORS | 8 +++---- Makefile | 22 ++++++++++++++++-- mbpfan.conf | 7 ++++++ src/daemon.c | 7 +++--- src/mbpfan.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++---- 5 files changed, 95 insertions(+), 13 deletions(-) create mode 100644 mbpfan.conf diff --git a/AUTHORS b/AUTHORS index 98414f3..fe4287c 100644 --- a/AUTHORS +++ b/AUTHORS @@ -1,12 +1,12 @@ MAINTAINERS AND CONTRIBUTORS ---------------------------- -Daniel Graziotin -CeRiAl +Daniel Graziotin +Ismail Khatib ORIGINARY AUTHORS ----------------- -Allan McRae mbpfan -rvega \ No newline at end of file +Allan McRae mbpfan +rvega diff --git a/Makefile b/Makefile index a67d557..073f4fb 100644 --- a/Makefile +++ b/Makefile @@ -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) @@ -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 diff --git a/mbpfan.conf b/mbpfan.conf new file mode 100644 index 0000000..5f2fd5c --- /dev/null +++ b/mbpfan.conf @@ -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 diff --git a/src/daemon.c b/src/daemon.c index 3909127..ba05549 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -1,6 +1,7 @@ /** * Copyright (C) 2012 Peter Lombardo * Modifications (2012) by Daniel Graziotin + * Modifications (2012) by Ismail Khatib * * 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 @@ -162,7 +163,7 @@ 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) @@ -170,7 +171,7 @@ void go_daemon(void (*fan_control)()) 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); } @@ -178,7 +179,7 @@ void go_daemon(void (*fan_control)()) { 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); } } diff --git a/src/mbpfan.c b/src/mbpfan.c index 56fdf32..d586c1d 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -3,6 +3,7 @@ * Copyright (C) 2010 Allan McRae * Modifications by Rafael Vega * Modifications (2012) by Daniel Graziotin + * Modifications (2012) by Ismail Khatib * * 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 @@ -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) */ @@ -36,6 +38,7 @@ #include #include "mbpfan.h" #include "global.h" +#include "settings.h" /* lazy min/max... */ #define min(a,b) a < b ? a : b @@ -273,6 +276,9 @@ 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); @@ -280,12 +286,62 @@ void mbpfan() 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); @@ -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); } } @@ -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);