updated on Thu Jan 19 20:01:47 UTC 2012
[aur-mirror.git] / cmp-daemon / cmp-daemon.c
blobc080f61d193e124fa024a3d5956bebd5cb5c9a1f
1 //cmp-daemon V 0.21
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <sys/stat.h>
7 #include <time.h>
8 #include <signal.h>
9 #include <string.h>
10 #include <syslog.h>
12 #define CPUINFO "/proc/cpuinfo"
13 #define PIDFILE "/var/run/cmp-daemon.pid"
15 #define FAN_1_MANUAL "/sys/devices/platform/applesmc.768/fan1_manual"
16 #define FAN_2_MANUAL "/sys/devices/platform/applesmc.768/fan2_manual"
18 #define RD_CPU_1_TEMP "/sys/devices/platform/coretemp.0/temp1_input"
19 #define RD_CPU_2_TEMP "/sys/devices/platform/coretemp.1/temp1_input"
21 #define RD_FAN_1 "/sys/devices/platform/applesmc.768/fan1_input"
22 #define RD_FAN_2 "/sys/devices/platform/applesmc.768/fan2_input"
23 #define WR_FAN_1 "/sys/devices/platform/applesmc.768/fan1_output"
24 #define WR_FAN_2 "/sys/devices/platform/applesmc.768/fan2_output"
26 #define MIN_SPEED 2000
27 #define MAX_SPEED 6000
29 #define ERROR -1
30 #define OK 0
32 int read_cpu_1_temp(void);
33 int read_cpu_2_temp(void);
34 void write_fan_1_manual(int);
35 void write_fan_2_manual(int);
36 void write_fan_1_speed(int);
37 void write_fan_2_speed(int);
38 void write_pidfile(void);
39 void check_pidfile(void);
40 void check_cpu(void);
41 int log_fan_speed(int,int,int);
42 int set_min_max_fan_speed(int);
45 void Signal_Handler(int sig){
46 switch(sig){
47 case SIGHUP:
48 break;
49 case SIGTERM:
50 write_fan_1_manual(0);
51 write_fan_2_manual(0);
52 unlink(PIDFILE);
53 syslog(LOG_INFO, "Stop");
54 closelog();
55 exit(OK);
56 break;
61 void start_daemon(void){
62 int i=0;
63 pid_t pid;
64 pid=fork();
66 if (pid<0){
67 /* fork error */
68 syslog(LOG_ERR,"Error cannot fork");
69 closelog();
70 exit(ERROR);
73 else if (pid>0){
74 exit(OK);
75 /* child continues */
78 if(setsid() == ERROR){
79 syslog(LOG_ERR,"Error setsid");
80 closelog();
81 exit(ERROR);
84 for (i=getdtablesize();i>=0;--i) close(i);
85 umask(027);
86 chdir ("/");
90 int main(int argc, char **argv){
92 signal(SIGHUP,Signal_Handler); /* hangup signal */
93 signal(SIGTERM,Signal_Handler); /* software termination signal from kill */
95 struct timespec timx,tim1;
97 openlog("cmp-daemon", LOG_PID, LOG_DAEMON);
99 /* check machine and pidfile*/
100 check_cpu();
101 check_pidfile();
102 write_pidfile();
103 write_fan_1_manual(1);
104 write_fan_2_manual(1);
106 start_daemon();
108 tim1.tv_sec = 0;
109 tim1.tv_nsec = 550000000;
111 //init
112 int cold=2;
113 int hot=2;
114 int wr_manual=0;
115 int change_number=0;
116 int old_fan_speed=-1;
118 syslog(LOG_INFO,"Start");
120 int rd_cpu_1_temp=read_cpu_1_temp();
121 int rd_cpu_2_temp=read_cpu_2_temp();
122 int temp=(rd_cpu_1_temp + rd_cpu_2_temp)/2000;
123 int old_temp=(rd_cpu_1_temp + rd_cpu_2_temp)/2000;
124 int fan_speed=(temp-38)*160;
126 write_fan_1_manual(1);
127 write_fan_2_manual(1);
128 fan_speed=set_min_max_fan_speed(fan_speed);
129 write_fan_1_speed(fan_speed);
130 write_fan_2_speed(fan_speed);
132 while(1){
134 rd_cpu_1_temp = read_cpu_1_temp();
135 rd_cpu_2_temp = read_cpu_2_temp();
137 wr_manual++;
139 if (wr_manual==9){
140 write_fan_1_manual(1);
141 write_fan_2_manual(1);
142 wr_manual=0;
145 temp=(rd_cpu_1_temp+rd_cpu_2_temp)/2000;
147 if (temp<old_temp){
148 cold++;
149 hot=0;
151 if (temp>old_temp){
152 hot++;
153 cold=0;
156 if ((cold==3)||(hot==3)){
157 // temp = average of both cpu's
158 fan_speed=(temp-38)*160;
159 // (50 - 38) * 160 = 1920
160 // (60 - 38) * 160 = 3520
161 // (70 - 38) * 160 = 5120
162 fan_speed=set_min_max_fan_speed(fan_speed);
164 if (fan_speed!=old_fan_speed){
165 write_fan_1_speed(fan_speed);
166 write_fan_2_speed(fan_speed);
167 change_number=log_fan_speed(fan_speed,change_number,temp);
168 old_fan_speed=fan_speed;
171 cold=0;
172 hot=0;
175 old_temp=temp;
177 if (nanosleep(&tim1,&timx)<OK){
178 closelog();
179 exit(ERROR);
185 int read_cpu_1_temp(void){
186 int temp;
187 FILE *file;
189 if ((file=fopen(RD_CPU_1_TEMP,"r"))!=NULL){
190 fscanf(file,"%d",&temp);
191 fclose(file);
192 return temp;
194 else{
195 syslog(LOG_ERR,"Error read_cpu_1_temp");
196 unlink(PIDFILE);
197 closelog();
198 exit(ERROR);
203 int read_cpu_2_temp(void){
204 int temp;
205 FILE *file;
207 if ((file=fopen(RD_CPU_2_TEMP,"r"))!=NULL){
208 fscanf(file,"%d", &temp);
209 fclose(file);
210 return temp;
212 else {
213 syslog(LOG_ERR,"Error read_cpu_2_temp");
214 unlink(PIDFILE);
215 closelog();
216 exit(ERROR);
220 int set_min_max_fan_speed(int fan_speed){
222 if (fan_speed<MIN_SPEED){
223 fan_speed=MIN_SPEED;
225 if (fan_speed>MAX_SPEED){
226 fan_speed=MAX_SPEED;
229 return fan_speed;
232 void write_fan_1_speed(int fan_speed_1){
233 FILE *file;
235 if((file=fopen(WR_FAN_1, "w"))!=NULL){
236 fprintf(file,"%d",fan_speed_1);
237 fclose(file);
239 else{
240 syslog(LOG_ERR, "Error write_fan_1_speed, applesmc loaded?");
241 unlink(PIDFILE);
242 closelog();
243 exit(ERROR);
248 void write_fan_2_speed(int fan_speed_2){
249 FILE *file;
251 if((file=fopen(WR_FAN_2, "w"))!=NULL){
252 fprintf(file,"%d",fan_speed_2);
253 fclose(file);
255 else{
256 unlink(PIDFILE);
257 syslog(LOG_ERR, "Error write_fan_2_speed, applesmc loaded?");
258 closelog();
259 exit(ERROR);
264 int log_fan_speed(int fan_speed,int change_number,int temp){
265 change_number++;
266 syslog(LOG_INFO, "Change %d: fan speed %d RPM temperature %d degree celsius",change_number,fan_speed,temp);
267 return change_number;
271 void write_fan_1_manual(int fan_manual_1){
272 FILE *file;
273 if((file=fopen(FAN_1_MANUAL, "w"))!=NULL){
274 fprintf(file,"%d",fan_manual_1);
275 fclose(file);
277 else{
278 syslog(LOG_ERR, "Error write_fan_1_manual, applesmc loaded?");
279 unlink(PIDFILE);
280 closelog();
281 exit(ERROR);
286 void write_fan_2_manual(int fan_manual_2){
287 FILE *file;
288 if((file=fopen(FAN_2_MANUAL, "w"))!=NULL){
289 fprintf(file,"%d",fan_manual_2);
290 fclose(file);
292 else{
293 syslog(LOG_ERR, "Error write_fan_2_manual, applesmc loaded?");
294 unlink(PIDFILE);
295 closelog();
296 exit(ERROR);
301 void write_pidfile(){
302 FILE *file;
303 if((file=fopen(PIDFILE,"w"))!=NULL){
304 fprintf(file,"%d",getpid());
305 fclose(file);
307 else{
308 syslog(LOG_ERR, "Error write_pidfile");
309 closelog();
310 exit(ERROR);
315 void check_pidfile(){
316 FILE *file;
317 if((file=fopen(PIDFILE,"r"))!=NULL){
318 /* if PIDFILE exist */
319 fclose(file);
320 syslog(LOG_ERR,"Error check_pidfile");
321 closelog();
322 exit(ERROR);
327 void check_cpu(){
328 FILE *file;
329 char buffer[80];
330 int cpucount=0;
332 if((file=fopen(CPUINFO,"r"))!=NULL){
333 while (!feof(file)) {
334 fgets(buffer, sizeof(buffer),file);
335 if (!strncmp(buffer,"model name : Intel(R) Core(TM)2 Duo CPU",39)){
336 cpucount++;
337 if (cpucount==1){
338 syslog(LOG_INFO,"CPU: %s",buffer);
342 fclose(file);
344 else{
345 syslog(LOG_ERR,"Error check_cpu");
346 closelog();
347 exit(ERROR);
350 if (cpucount!=2){
351 syslog(LOG_ERR,"Error check_cpu count: %d",cpucount);
352 closelog();
353 exit(ERROR);