-
Notifications
You must be signed in to change notification settings - Fork 1
/
mfc-daemon.c
508 lines (398 loc) · 10.4 KB
/
mfc-daemon.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/*
* mfc-daemon macbook auto fan control deamon
*
* (C) Copyright 2009
* Author: Xiangfu Liu <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 3 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <signal.h>
#include <string.h>
#include <syslog.h>
#include <stdarg.h>
#include <errno.h>
#include <getopt.h>
#include <dirent.h>
#include <regex.h>
#include <libgen.h>
#include "config.h"
#define TRUE 1
#define FALSE 0
#define MODE_WRITE "w"
#define MODE_READ "r"
#define CPU_LABEL "model name : Intel(R) Core(TM)2 Duo CPU"
#define QUIT_DAEMON(message, ...) quit_daemon(message " [%s:%d]", ##__VA_ARGS__, __FUNCTION__, __LINE__)
#define FILE_EXISTS(filename) (access(filename, F_OK) == 0)
#define INFO(message, ...) LOG(LOG_INFO, message, ##__VA_ARGS__)
#define ERROR(message, ...) LOG(LOG_ERR, message, ##__VA_ARGS__)
#define LOG(level, message, ...) mfc_log(level, message " [%s:%d]", ##__VA_ARGS__, __FUNCTION__, __LINE__)
#define OPTION_END { 0, }
#define OPTION_NOARG(name, letter, var) { name, no_argument, var, letter }
#define OPTION_REQUIRED(name, letter, var) { name, required_argument, var, letter }
#define OPTION_OPTIONAL(name, letter, var) { name, optional_argument, var, letter }
/**
* The daemon's global context. This data structure contains all global
* variables needed by daemon.
*/
typedef struct _MfcCtx {
int total_cpus;
int total_fans;
int pidfile;
int syslog;
int fork;
int stdout;
} MfcCtx;
MfcCtx MFC = {0,};
void write_fan_manual(int, int);
void write_fan_speed(int, int);
int read_cpu_temp(int);
void write_pidfile(void);
void check_pidfile(void);
int check_cpu(void);
int check_fan(void);
int log_fan_speed(int,int,int);
int set_min_max_fan_speed(int);
int get_cpu_temperature(void);
void parse_options (int argc, char * const argv[]);
void mfc_log (int level, char *format, ...) {
va_list args;
va_start(args, format);
if (MFC.syslog) {
vsyslog(level, format, args);
}
if (MFC.stdout) {
vprintf(format, args);
printf("\n");
}
va_end(args);
}
void quit_daemon (char *format, ...) {
va_list args;
va_start(args, format);
if (MFC.syslog) {
vsyslog(LOG_ERR, format, args);
closelog();
}
if (MFC.stdout) {
vprintf(format, args);
printf("\n");
}
va_end(args);
if (MFC.pidfile) {
unlink(PIDFILE);
}
exit(EXIT_FAILURE);
}
char* mfc_vsprintf (const char *format, va_list args) {
char *string;
int code;
code = vasprintf(&string, format, args);
if (code < 0) {
va_end(args);
QUIT_DAEMON("Failed to allocate filename for pattern %s", format);
}
return string;
}
char* mfc_sprintf (const char *format, ...) {
char *string;
va_list args;
va_start(args, format);
string = mfc_vsprintf(format, args);
va_end(args);
return string;
}
FILE* mfc_fopen (const char* mode, const char *format, ...) {
char *filename;
va_list args;
FILE *file;
va_start(args, format);
filename = mfc_vsprintf(format, args);
va_end(args);
errno = 0;
file = fopen(filename, mode);
if (file == NULL) {
ERROR(
"Failed to open %s in mode %s because: %s",
filename, mode, strerror(errno)
);
}
free(filename);
return file;
}
void Signal_Handler(int sig){
switch(sig){
case SIGHUP:
break;
case SIGTERM:
{
int fan;
INFO("Signal handler caught SIGTERM");
for (fan = 1; fan <= MFC.total_fans; ++fan) {
write_fan_manual(fan, 0);
}
QUIT_DAEMON("Stop");
}
break;
}
}
void start_daemon(void){
int i=0;
pid_t pid;
pid=fork();
if (pid<0){
/* fork error */
QUIT_DAEMON("Error cannot fork");
}
else if (pid>0){
exit(EXIT_SUCCESS);
/* child continues */
}
if(setsid() == -1){
QUIT_DAEMON("Error setsid");
}
for (i=getdtablesize();i>=0;--i) close(i);
umask(027);
chdir("/");
}
int main(int argc, char * const argv[]){
/* Parse the command line arguments */
MFC.fork = TRUE;
parse_options(argc, argv);
signal(SIGHUP,Signal_Handler); /* hangup signal */
signal(SIGTERM,Signal_Handler); /* software termination signal from kill */
struct timespec timx,tim1;
openlog("mfc-daemon", LOG_PID, LOG_DAEMON);
MFC.syslog = TRUE;
/* check machine and pidfile*/
MFC.total_cpus = check_cpu();
MFC.total_fans = check_fan();
check_pidfile();
write_pidfile();
MFC.pidfile = TRUE;
if (MFC.fork) {
start_daemon();
}
int fan;
for (fan = 1; fan <= MFC.total_fans; ++fan) {
write_fan_manual(fan, 1);
}
tim1.tv_sec = TV_SEC;
tim1.tv_nsec = TV_NSEC;
//init
int wr_manual=0;
int change_number=0;
int old_fan_speed=-1;
INFO("Start");
int temp = get_cpu_temperature();
int old_temp_change = 0;
int fan_speed=GET_FAN_SPEED(temp);
fan_speed=set_min_max_fan_speed(fan_speed);
for (fan = 1; fan <= MFC.total_fans; ++fan) {
write_fan_speed(fan, fan_speed);
}
while (1){
wr_manual++;
if (wr_manual==9){
for (fan = 1; fan <= MFC.total_fans; ++fan) {
write_fan_manual(fan, 1);
}
wr_manual=0;
}
temp = get_cpu_temperature();
int diff = abs(temp - old_temp_change);
if (diff >= 2){
// temp = average of both cpu's
fan_speed=GET_FAN_SPEED(temp);
fan_speed=set_min_max_fan_speed(fan_speed);
if (fan_speed!=old_fan_speed){
for (fan = 1; fan <= MFC.total_fans; ++fan) {
write_fan_speed(fan, fan_speed);
}
change_number=log_fan_speed(fan_speed,change_number,temp);
old_fan_speed=fan_speed;
}
old_temp_change = temp;
}
if (nanosleep(&tim1,&timx) == -1){
QUIT_DAEMON("Error nanosleep");
}
}
}
int read_cpu_temp(int index){
int temp;
FILE *file;
if (MFC.total_cpus == 1) {
// If there's a single core pretend that the second core has the same
// temperature as the first core.
index = 1;
}
/* The CPU temperature file index starts at 0 */
file = mfc_fopen(MODE_READ, FILE_CPU_TEMP, index - 1);
if (file == NULL) {
QUIT_DAEMON("Failed to read the temperature of CPU %d", index);
}
fscanf(file, "%d", &temp);
fclose(file);
return temp;
}
void write_fan_speed(int index, int speed){
FILE *file = mfc_fopen(MODE_WRITE, FILE_FAN_SPEED, index);
if (file == NULL) {
QUIT_DAEMON("Failed to set the speed of fan %d, is applesmc loaded?", index);
}
fprintf(file, "%d", speed);
fclose(file);
}
void write_fan_manual(int index, int manual){
FILE *file = mfc_fopen(MODE_WRITE, FILE_FAN_MANUAL, index);
if (file == NULL) {
QUIT_DAEMON("Failed to change the manual setting of fan %d, is applesmc loaded?", index);
}
fprintf(file, "%d", manual);
fclose(file);
}
int set_min_max_fan_speed(int fan_speed){
if (fan_speed<MIN_SPEED){
fan_speed=MIN_SPEED;
}
if (fan_speed>MAX_SPEED){
fan_speed=MAX_SPEED;
}
return fan_speed;
}
int log_fan_speed(int fan_speed,int change_number,int temp){
change_number++;
INFO("Change %d: fan speed %d RPM temperature %d degree celsius", change_number, fan_speed, temp);
return change_number;
}
void write_pidfile(){
FILE *file = mfc_fopen(MODE_WRITE, PIDFILE);
if (file == NULL) {
QUIT_DAEMON("Can't write PID file %s", PIDFILE);
}
fprintf(file,"%d",getpid());
fclose(file);
}
void check_pidfile(){
if (FILE_EXISTS(PIDFILE)) {
/* We are expecting that the file DOES NOT exist */
QUIT_DAEMON("PID file %s already exists, is the daemon running?", PIDFILE);
}
return;
}
int check_cpu(){
FILE *file;
char *line = NULL;
size_t size = 0;
int total_cpus = 0;
file = mfc_fopen(MODE_READ, CPUINFO);
if (file == NULL) {
QUIT_DAEMON("Can't read the CPU type from %s", CPUINFO);
}
while (!feof(file)) {
getline(&line, &size, file);
if (!strncmp(line, CPU_LABEL, strlen(CPU_LABEL))) {
total_cpus++;
if (total_cpus == 1) {
syslog(LOG_INFO, "CPU: %s", line);
}
}
}
fclose(file);
INFO("Detected %d CPUs", total_cpus);
return total_cpus;
}
int check_fan() {
// Regexp that matches "fan1_manual", "fan2_manual", etc
regex_t regexp = {0,};
regcomp(®exp, "^fan[0-9]+_manual$", REG_NOSUB | REG_EXTENDED);
char *copy = strdup(FILE_FAN_MANUAL);
char *foldername = dirname(copy);
DIR *dir = opendir(foldername);
if (dir == NULL) {
QUIT_DAEMON("Failed to scan fans in folder %s\n", foldername);
free(copy);
}
free(copy);
int fans = 0;
while (TRUE) {
struct dirent *entry = readdir(dir);
if (entry == NULL) {
break;
}
if (regexec(®exp, entry->d_name, 0, NULL, 0) == 0) {
++fans;
}
}
closedir(dir);
regfree(®exp);
INFO("Detected %d fans", fans);
return fans;
}
int get_cpu_temperature() {
int total_cpus = MFC.total_cpus;
if (total_cpus == 1) {
/* Assume that the computer has 2 CPUs (2 cores). Some Mac laptops have
to be booted with maxcores=1 or acpi=off, this is true for the
MacBook 5,1. Both options diseable the second core and Linux sees a
single one.
When computing the temperature it could be wiser to assume that the
temperature was computed for two cores. Although I'm not too sure
about this.
*/
total_cpus = 2;
}
int temp = 0;
int cpu;
for (cpu = 1; cpu <= total_cpus; ++cpu) {
temp += read_cpu_temp(cpu);
}
temp /= 2000;
return temp;
}
void parse_options (int argc, char * const argv[]) {
struct option longopts[] = {
OPTION_NOARG("help", 'h', NULL),
OPTION_NOARG("no-fork", FALSE, &MFC.fork),
OPTION_NOARG("stdout", 's', &MFC.stdout),
OPTION_END,
};
while (TRUE) {
int c = getopt_long(argc, argv, "hXs", longopts, NULL);
if (c == -1) {
break;
}
switch (c) {
case 'h':
MFC.stdout = TRUE;
QUIT_DAEMON("mfc-daemon - Macbook Fan Control daemon\n"
"(c) 2009 Xiangfu Liu, Emmanuel Rodriguez\n"
"Patched by Luigi R. Viggiano for simpler fan control\n"
"This program is Free Software and comes with ABSOLUTELY NO WARRANTY.\n\n"
"Usage: mfc-daemon [OPTION]...\n"
" -h --help\t\t\tPrint this help message\n"
" --no-fork\t\t\tStart in stand alone mode\n"
" -s --stdout\t\t\tOutput message to STDOUT\n");
break;
}
}
}