1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include <getopt.h>
20#include <unistd.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <ncurses.h>
27#include <ctype.h>
28#include <time.h>
29#include <signal.h>
30#include <limits.h>
31#include <sys/time.h>
32#include <pthread.h>
33#include <math.h>
34#include <stdarg.h>
35#include <syslog.h>
36
37#include "tmon.h"
38
39unsigned long ticktime = 1;
40unsigned long no_control = 1;
41
42
43double time_elapsed = 0.0;
44unsigned long target_temp_user = 65;
45int dialogue_on;
46int tmon_exit;
47static short daemon_mode;
48static int logging;
49static int debug_on;
50FILE *tmon_log;
51
52char ctrl_cdev[CDEV_NAME_SIZE] = "None";
53int target_thermal_zone;
54static void start_daemon_mode(void);
55
56pthread_t event_tid;
57pthread_mutex_t input_lock;
58void usage(void)
59{
60 printf("Usage: tmon [OPTION...]\n");
61 printf(" -c, --control cooling device in control\n");
62 printf(" -d, --daemon run as daemon, no TUI\n");
63 printf(" -g, --debug debug message in syslog\n");
64 printf(" -h, --help show this help message\n");
65 printf(" -l, --log log data to /var/tmp/tmon.log\n");
66 printf(" -t, --time-interval sampling time interval, > 1 sec.\n");
67 printf(" -T, --target-temp initial target temperature\n");
68 printf(" -v, --version show version\n");
69 printf(" -z, --zone target thermal zone id\n");
70
71 exit(0);
72}
73
74void version(void)
75{
76 printf("TMON version %s\n", VERSION);
77 exit(EXIT_SUCCESS);
78}
79
80static void tmon_cleanup(void)
81{
82 syslog(LOG_INFO, "TMON exit cleanup\n");
83 fflush(stdout);
84 refresh();
85 if (tmon_log)
86 fclose(tmon_log);
87 if (event_tid) {
88 pthread_mutex_lock(&input_lock);
89 pthread_cancel(event_tid);
90 pthread_mutex_unlock(&input_lock);
91 pthread_mutex_destroy(&input_lock);
92 }
93 closelog();
94
95 set_ctrl_state(0);
96
97 keypad(stdscr, FALSE);
98 echo();
99 nocbreak();
100 close_windows();
101 endwin();
102 free_thermal_data();
103
104 exit(1);
105}
106
107static void tmon_sig_handler(int sig)
108{
109 syslog(LOG_INFO, "TMON caught signal %d\n", sig);
110 refresh();
111 switch (sig) {
112 case SIGTERM:
113 printf("sigterm, exit and clean up\n");
114 fflush(stdout);
115 break;
116 case SIGKILL:
117 printf("sigkill, exit and clean up\n");
118 fflush(stdout);
119 break;
120 case SIGINT:
121 printf("ctrl-c, exit and clean up\n");
122 fflush(stdout);
123 break;
124 default:
125 break;
126 }
127 tmon_exit = true;
128}
129
130static void start_syslog(void)
131{
132 if (debug_on)
133 setlogmask(LOG_UPTO(LOG_DEBUG));
134 else
135 setlogmask(LOG_UPTO(LOG_ERR));
136 openlog("tmon.log", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL0);
137 syslog(LOG_NOTICE, "TMON started by User %d", getuid());
138}
139
140static void prepare_logging(void)
141{
142 int i;
143 struct stat logstat;
144
145 if (!logging)
146 return;
147
148 tmon_log = fopen(TMON_LOG_FILE, "w+");
149 if (!tmon_log) {
150 syslog(LOG_ERR, "failed to open log file %s\n", TMON_LOG_FILE);
151 return;
152 }
153
154 if (lstat(TMON_LOG_FILE, &logstat) < 0) {
155 syslog(LOG_ERR, "Unable to stat log file %s\n", TMON_LOG_FILE);
156 fclose(tmon_log);
157 tmon_log = NULL;
158 return;
159 }
160
161
162 if (S_ISLNK(logstat.st_mode)) {
163 syslog(LOG_ERR, "Log file is a symlink. Will not log\n");
164 fclose(tmon_log);
165 tmon_log = NULL;
166 return;
167 }
168
169 if (logstat.st_uid != getuid()) {
170 syslog(LOG_ERR, "We don't own the log file. Not logging\n");
171 fclose(tmon_log);
172 tmon_log = NULL;
173 return;
174 }
175
176 fprintf(tmon_log, "#----------- THERMAL SYSTEM CONFIG -------------\n");
177 for (i = 0; i < ptdata.nr_tz_sensor; i++) {
178 char binding_str[33];
179 int j;
180
181 memset(binding_str, 0, sizeof(binding_str));
182 for (j = 0; j < 32; j++)
183 binding_str[j] = (ptdata.tzi[i].cdev_binding & (1 << j)) ?
184 '1' : '0';
185
186 fprintf(tmon_log, "#thermal zone %s%02d cdevs binding: %32s\n",
187 ptdata.tzi[i].type,
188 ptdata.tzi[i].instance,
189 binding_str);
190 for (j = 0; j < ptdata.tzi[i].nr_trip_pts; j++) {
191 fprintf(tmon_log, "#\tTP%02d type:%s, temp:%lu\n", j,
192 trip_type_name[ptdata.tzi[i].tp[j].type],
193 ptdata.tzi[i].tp[j].temp);
194 }
195 }
196
197 for (i = 0; i < ptdata.nr_cooling_dev; i++)
198 fprintf(tmon_log, "#cooling devices%02d: %s\n",
199 i, ptdata.cdi[i].type);
200
201 fprintf(tmon_log, "#---------- THERMAL DATA LOG STARTED -----------\n");
202 fprintf(tmon_log, "Samples TargetTemp ");
203 for (i = 0; i < ptdata.nr_tz_sensor; i++) {
204 fprintf(tmon_log, "%s%d ", ptdata.tzi[i].type,
205 ptdata.tzi[i].instance);
206 }
207 for (i = 0; i < ptdata.nr_cooling_dev; i++)
208 fprintf(tmon_log, "%s%d ", ptdata.cdi[i].type,
209 ptdata.cdi[i].instance);
210
211 fprintf(tmon_log, "\n");
212}
213
214static struct option opts[] = {
215 { "control", 1, NULL, 'c' },
216 { "daemon", 0, NULL, 'd' },
217 { "time-interval", 1, NULL, 't' },
218 { "target-temp", 1, NULL, 'T' },
219 { "log", 0, NULL, 'l' },
220 { "help", 0, NULL, 'h' },
221 { "version", 0, NULL, 'v' },
222 { "debug", 0, NULL, 'g' },
223 { 0, 0, NULL, 0 }
224};
225
226int main(int argc, char **argv)
227{
228 int err = 0;
229 int id2 = 0, c;
230 double yk = 0.0, temp;
231 int target_tz_index;
232
233 if (geteuid() != 0) {
234 printf("TMON needs to be run as root\n");
235 exit(EXIT_FAILURE);
236 }
237
238 while ((c = getopt_long(argc, argv, "c:dlht:T:vgz:", opts, &id2)) != -1) {
239 switch (c) {
240 case 'c':
241 no_control = 0;
242 strncpy(ctrl_cdev, optarg, CDEV_NAME_SIZE);
243 break;
244 case 'd':
245 start_daemon_mode();
246 printf("Run TMON in daemon mode\n");
247 break;
248 case 't':
249 ticktime = strtod(optarg, NULL);
250 if (ticktime < 1)
251 ticktime = 1;
252 break;
253 case 'T':
254 temp = strtod(optarg, NULL);
255 if (temp < 0) {
256 fprintf(stderr, "error: temperature must be positive\n");
257 return 1;
258 }
259 target_temp_user = temp;
260 break;
261 case 'l':
262 printf("Logging data to /var/tmp/tmon.log\n");
263 logging = 1;
264 break;
265 case 'h':
266 usage();
267 break;
268 case 'v':
269 version();
270 break;
271 case 'g':
272 debug_on = 1;
273 break;
274 case 'z':
275 target_thermal_zone = strtod(optarg, NULL);
276 break;
277 default:
278 break;
279 }
280 }
281 if (pthread_mutex_init(&input_lock, NULL) != 0) {
282 fprintf(stderr, "\n mutex init failed, exit\n");
283 return 1;
284 }
285 start_syslog();
286 if (signal(SIGINT, tmon_sig_handler) == SIG_ERR)
287 syslog(LOG_DEBUG, "Cannot handle SIGINT\n");
288 if (signal(SIGTERM, tmon_sig_handler) == SIG_ERR)
289 syslog(LOG_DEBUG, "Cannot handle SIGTERM\n");
290
291 if (probe_thermal_sysfs()) {
292 pthread_mutex_destroy(&input_lock);
293 closelog();
294 return -1;
295 }
296 initialize_curses();
297 setup_windows();
298 signal(SIGWINCH, resize_handler);
299 show_title_bar();
300 show_sensors_w();
301 show_cooling_device();
302 update_thermal_data();
303 show_data_w();
304 prepare_logging();
305 init_thermal_controller();
306
307 nodelay(stdscr, TRUE);
308 err = pthread_create(&event_tid, NULL, &handle_tui_events, NULL);
309 if (err != 0) {
310 printf("\ncan't create thread :[%s]", strerror(err));
311 tmon_cleanup();
312 exit(EXIT_FAILURE);
313 }
314
315
316
317
318 target_tz_index = zone_instance_to_index(target_thermal_zone);
319 if (target_tz_index < 0) {
320 target_thermal_zone = ptdata.tzi[0].instance;
321 syslog(LOG_ERR, "target zone is not found, default to %d\n",
322 target_thermal_zone);
323 }
324 while (1) {
325 sleep(ticktime);
326 show_title_bar();
327 show_sensors_w();
328 update_thermal_data();
329 if (!dialogue_on) {
330 show_data_w();
331 show_cooling_device();
332 }
333 time_elapsed += ticktime;
334 controller_handler(trec[0].temp[target_tz_index] / 1000, &yk);
335 trec[0].pid_out_pct = yk;
336 if (!dialogue_on)
337 show_control_w();
338 if (tmon_exit)
339 break;
340 }
341 tmon_cleanup();
342 return 0;
343}
344
345static void start_daemon_mode(void)
346{
347 daemon_mode = 1;
348
349 pid_t sid, pid = fork();
350
351 if (pid < 0)
352 exit(EXIT_FAILURE);
353 else if (pid > 0)
354
355 exit(EXIT_SUCCESS);
356
357
358 disable_tui();
359
360
361 umask(S_IWGRP | S_IWOTH);
362
363
364 sid = setsid();
365 if (sid < 0)
366 exit(EXIT_FAILURE);
367
368
369 if ((chdir("/")) < 0)
370 exit(EXIT_FAILURE);
371
372 sleep(10);
373
374 close(STDIN_FILENO);
375 close(STDOUT_FILENO);
376 close(STDERR_FILENO);
377}
378