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
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);
41 int log_fan_speed(int,int,int);
42 int set_min_max_fan_speed(int);
45 void Signal_Handler(int sig
){
50 write_fan_1_manual(0);
51 write_fan_2_manual(0);
53 syslog(LOG_INFO
, "Stop");
61 void start_daemon(void){
68 syslog(LOG_ERR
,"Error cannot fork");
78 if(setsid() == ERROR
){
79 syslog(LOG_ERR
,"Error setsid");
84 for (i
=getdtablesize();i
>=0;--i
) close(i
);
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*/
103 write_fan_1_manual(1);
104 write_fan_2_manual(1);
109 tim1
.tv_nsec
= 550000000;
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
);
134 rd_cpu_1_temp
= read_cpu_1_temp();
135 rd_cpu_2_temp
= read_cpu_2_temp();
140 write_fan_1_manual(1);
141 write_fan_2_manual(1);
145 temp
=(rd_cpu_1_temp
+rd_cpu_2_temp
)/2000;
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
;
177 if (nanosleep(&tim1
,&timx
)<OK
){
185 int read_cpu_1_temp(void){
189 if ((file
=fopen(RD_CPU_1_TEMP
,"r"))!=NULL
){
190 fscanf(file
,"%d",&temp
);
195 syslog(LOG_ERR
,"Error read_cpu_1_temp");
203 int read_cpu_2_temp(void){
207 if ((file
=fopen(RD_CPU_2_TEMP
,"r"))!=NULL
){
208 fscanf(file
,"%d", &temp
);
213 syslog(LOG_ERR
,"Error read_cpu_2_temp");
220 int set_min_max_fan_speed(int fan_speed
){
222 if (fan_speed
<MIN_SPEED
){
225 if (fan_speed
>MAX_SPEED
){
232 void write_fan_1_speed(int fan_speed_1
){
235 if((file
=fopen(WR_FAN_1
, "w"))!=NULL
){
236 fprintf(file
,"%d",fan_speed_1
);
240 syslog(LOG_ERR
, "Error write_fan_1_speed, applesmc loaded?");
248 void write_fan_2_speed(int fan_speed_2
){
251 if((file
=fopen(WR_FAN_2
, "w"))!=NULL
){
252 fprintf(file
,"%d",fan_speed_2
);
257 syslog(LOG_ERR
, "Error write_fan_2_speed, applesmc loaded?");
264 int log_fan_speed(int fan_speed
,int change_number
,int temp
){
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
){
273 if((file
=fopen(FAN_1_MANUAL
, "w"))!=NULL
){
274 fprintf(file
,"%d",fan_manual_1
);
278 syslog(LOG_ERR
, "Error write_fan_1_manual, applesmc loaded?");
286 void write_fan_2_manual(int fan_manual_2
){
288 if((file
=fopen(FAN_2_MANUAL
, "w"))!=NULL
){
289 fprintf(file
,"%d",fan_manual_2
);
293 syslog(LOG_ERR
, "Error write_fan_2_manual, applesmc loaded?");
301 void write_pidfile(){
303 if((file
=fopen(PIDFILE
,"w"))!=NULL
){
304 fprintf(file
,"%d",getpid());
308 syslog(LOG_ERR
, "Error write_pidfile");
315 void check_pidfile(){
317 if((file
=fopen(PIDFILE
,"r"))!=NULL
){
318 /* if PIDFILE exist */
320 syslog(LOG_ERR
,"Error check_pidfile");
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)){
338 syslog(LOG_INFO
,"CPU: %s",buffer
);
345 syslog(LOG_ERR
,"Error check_cpu");
351 syslog(LOG_ERR
,"Error check_cpu count: %d",cpucount
);