Debian Codegraph @

macfanctl.c

raw symbols SCIP
1 /*
2 * macfanctl.c - Fan control daemon for MacBook
3 *
4 * Copyright (C) 2010 Mikael Strom <[email protected]>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * Note:
17 * This was written for MacBook Pro 5,1 and Ubutnu 10.04
18 * Requires applesmc-dkms
19 *
20 */
21
22 #include <unistd.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <sys/time.h>
29 #include <fcntl.h>
30 #include <signal.h>
31
32 #include "control.h"
33 #include "config.h"
34
35 //------------------------------------------------------------------------------
36
37 #define PID_FILE "/var/run/macfanctld.pid"
38 #define LOG_FILE "/var/log/macfanctl.log"
39 #define CFG_FILE "/etc/macfanctl.conf"
40
41 int running = 1;
42 int lock_fd = -1;
43 int reload = 0;
44
45 //------------------------------------------------------------------------------
46
47 void signal_handler(int sig)
48 {
49 switch (sig)
50 {
51 case SIGHUP:
52 reload = 1;
53 break;
54 case SIGINT:
55 case SIGTERM:
56 running = 0;
57 break;
58 }
59 }
60
61 //-----------------------------------------------------------------------------
62
63 void daemonize()
64 {
65 if (getppid() == 1)
66 return; // already a daemon
67
68 // fork of new process
69 pid_t pid = fork();
70
71 if(pid < 0)
72 exit(1); // fork error
73
74 if(pid > 0)
75 exit(0); // parent exits
76
77 // child (daemon) continues
78
79 #ifdef DEBUG
80 sleep(20); // time to attach debugger to this process
81 #endif
82
83 setsid(); // create a new session
84
85 #ifdef DEBUG
86 umask(0);
87 #else
88 umask(022); // set createfile permissions
89 #endif
90
91 freopen(LOG_FILE, "w", stdout);
92 freopen("/dev/null", "r", stdin);
93
94 chdir("/");
95
96 // create lockfile
97 int lock_fd = open(PID_FILE, O_RDWR | O_CREAT, 0640);
98
99 if(lock_fd < 0)
100 exit(1); // open failed, we're a duplicate
101
102 if(lockf(lock_fd, F_TLOCK, 0) < 0)
103 exit(0); // lock failed - no idea what this means...
104
105 // first instance continues...
106 // write pid to file, and leave file open (blocking duplicates)
107
108 char str[32];
109 sprintf(str, "%d\n", getpid());
110 write(lock_fd, str, strlen(str));
111 }
112
113 //-----------------------------------------------------------------------------
114
115 void usage()
116 {
117 printf("usage: macfanctld [-f]\n");
118 printf(" -f run in foregound\n");
119 exit(-1);
120 }
121
122 //-----------------------------------------------------------------------------
123
124 int main(int argc, char *argv[])
125 {
126 int i;
127 int daemon = 1;
128
129 // setup daemon
130 signal(SIGCHLD, SIG_IGN); // ignore child
131 signal(SIGTSTP, SIG_IGN); // ignore tty signals
132 signal(SIGTTOU, SIG_IGN);
133 signal(SIGTTIN, SIG_IGN);
134 signal(SIGINT, signal_handler); // catch Ctrl-C signal (terminating in foreground mode)
135 signal(SIGHUP, signal_handler); // catch hangup signal (reload config)
136 signal(SIGTERM, signal_handler); // catch kill signal
137
138 for(i = 1; i < argc; ++i)
139 {
140 if(strcmp(argv[i], "-f") == 0)
141 {
142 daemon = 0;
143 }
144 else
145 {
146 usage();
147 }
148 }
149
150 if(daemon)
151 {
152 daemonize();
153 }
154 else
155 {
156 printf("Running in foreground, log to stdout.\n");
157 }
158
159 // main loop
160
161 read_cfg(CFG_FILE);
162
163 find_applesmc();
164 scan_sensors();
165
166 running = 1;
167 while(running)
168 {
169 adjust();
170
171 logger();
172
173 if(reload)
174 {
175 read_cfg(CFG_FILE);
176 scan_sensors();
177 reload = 0;
178 }
179
180 sleep(5);
181 }
182
183 // close pid file and delete it
184
185 if(lock_fd != -1)
186 {
187 close(lock_fd);
188 unlink(PID_FILE);
189 }
190
191 printf("Exiting.\n");
192
193 return 0;
194 }
195
196 //-----------------------------------------------------------------------------
197