-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
297 lines (242 loc) · 5.96 KB
/
main.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
/*
* Copyright (C) 2015, CompuLab ltd.
* Author: Andrey Gelman <[email protected]>
* License: GNU GPLv2 or later, at your option
*/
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <syslog.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sensors/sensors.h>
#include "common.h"
#include "thread-pool.h"
#include "registers.h"
#include "panel.h"
#include "sensors.h"
#include "domain-logic.h"
#include "stats.h"
#include "vga-tools.h"
#include "hdd-info.h"
#include "options.h"
ThreadPool *frontend_thread;
ThreadPool *backend_thread;
static InProcessingBitmap in_processing = {0};
static Options options;
static void main_thread(void *priv_context, void *shared_context);
static void signal_handler(int signo)
{
switch (signo)
{
case SIGALRM:
thread_pool_add_request(frontend_thread, main_thread, NULL);
break;
case SIGUSR1:
stat_show();
break;
case SIGUSR2:
/* nothing - interrupt sleep */
break;
}
}
static void install_sighandler(int signo)
{
struct sigaction sig = {{0}};
int err;
sig.sa_handler = signal_handler;
sigemptyset(&sig.sa_mask);
sig.sa_flags = SA_RESTART;
err = sigaction(signo, &sig, NULL);
if (err) {
sloge("Signal %d: could not install handler: %d", signo, err);
exit(EXIT_FAILURE);
}
}
enum {DTERM_INIT, DTERM_WAIT};
static void daemon_termination(int phase)
{
sigset_t signals;
int signo = 0;
sigemptyset(&signals);
sigaddset(&signals, SIGTERM);
switch (phase) {
case DTERM_INIT:
pthread_sigmask(SIG_BLOCK, &signals, NULL);
break;
case DTERM_WAIT:
while (signo != SIGTERM)
sigwait(&signals, &signo);
break;
}
}
static void daemonize(void)
{
int fd;
int err;
/*
* assert a single instance of the daemon
*/
fd = open(ATFP_DAEMON_LOCKFILE, O_CREAT, 0);
if (fd < 0) {
fprintf(stderr, "Could not open lock file: %d. \n", errno);
exit(1);
}
err = flock(fd, (LOCK_EX | LOCK_NB));
if (err < 0) {
if (errno == EWOULDBLOCK)
fprintf(stderr, "An instance of the daemon already runs. \n");
else
fprintf(stderr, "Could not acquire lock file: %d. \n", errno);
exit(1);
}
/*
* fork background daemon
*/
err = daemon(0, 0);
if (err < 0) {
fprintf(stderr, "Could not daemon-ize: %m \n");
exit(1);
}
umask(0022);
}
static void initialize(void)
{
int err;
/* set up logging */
openlog(ATFP_SYSLOG_IDENT, LOG_PID, LOG_USER);
setlogmask(LOG_UPTO(options.loglevel));
install_sighandler(SIGALRM);
install_sighandler(SIGUSR1);
install_sighandler(SIGUSR2);
daemon_termination(DTERM_INIT);
err = panel_open_i2c(options.i2c_bus, I2C_PANEL_INTERFACE_ADDR, options.i2c_delay);
if ( err )
exit(1);
err = panel_reset();
if ( err )
exit(1);
err = sensors_coretemp_init();
if ( err )
exit(1);
gpu_sensors_init();
frontend_thread = thread_pool_create(1, ATFP_FRONTEND_QUEUE_LEN, &in_processing);
backend_thread = thread_pool_create(ATFP_BACKEND_THREAD_NUM, ATFP_BACKEND_QUEUE_LEN, NULL);
}
static void cleanup(void)
{
thread_pool_join(frontend_thread);
thread_pool_destroy(backend_thread);
thread_pool_destroy(frontend_thread);
panel_close();
sensors_cleanup();
closelog();
}
static void show_info_and_exit(void)
{
char *p;
char *name_list;
DList *dl;
SMARTinfo *si;
name_list = vga_driver_name_list();
printf("Video drivers: %s \n", strtok(name_list, " "));
while ((p = strtok(NULL, " ")) != NULL)
printf(" %s \n", p);
printf("\nTemperature sensors: \n");
sensors_show(SENSORS_FEATURE_TEMP);
printf("\nHDD: \n");
hdd_get_temperature(&dl);
while ((si = dlist_pop_front(dl)) != NULL) {
printf("%10s: ", si->devname);
if (si->size_valid)
printf("%u [GB] \n", si->size_GB);
else
printf("-- \n");
delete_SMARTinfo(si);
}
exit(0);
}
#define UNSUPPORTED_REQ_MESSAGE(r) do { slogw("%s: "#r" request is not supported", __FUNCTION__); } while (0)
void in_processing_add_request(long request, InProcessingBitmap *processing)
{
processing->bitmap |= request;
}
void in_processing_remove_request(long request, InProcessingBitmap *processing)
{
processing->bitmap &= ~request;
}
long in_processing_get_bitmap(InProcessingBitmap *processing)
{
return processing->bitmap;
}
static void main_thread(void *priv_context, void *shared_context)
{
int i;
long request_bitmap;
long request;
InProcessingBitmap *processing = (InProcessingBitmap *)shared_context;
request_bitmap = panel_get_pending_requests();
/* (optionally) disable particular functions */
request_bitmap &= ~options.disable;
/* ignore requests currently being processed */
request_bitmap &= ~in_processing_get_bitmap(processing);
/* dispatch each request */
for (i = 0; i < (sizeof(request_bitmap) * 8); ++i) {
request = request_bitmap & (1L << i);
in_processing_add_request(request, processing);
switch (request) {
case 0:
/* no pending requests */
break;
case ATFP_MASK_PENDR0_HDDTR:
panel_update_hdd_temp();
break;
case ATFP_MASK_PENDR0_CPUFR:
panel_update_frequency();
break;
case ATFP_MASK_PENDR0_CPUTR:
panel_update_temperature();
break;
case ATFP_MASK_PENDR0_GPUTR:
panel_update_gpu_temp();
break;
default:
/* 'request' should not be added in the first place */
in_processing_remove_request(request, processing);
break;
}
}
/* program our next appearance */
alarm(options.poll_cycle);
}
int main(int argc, char *argv[])
{
options_process_or_abort(&options, argc, argv);
if (options.info)
show_info_and_exit();
if ( !options.i2c_bus_set ) {
options.i2c_bus = panel_lookup_i2c_bus();
if (options.i2c_bus < 0) {
fprintf(stderr, "Could not detect front panel I2C bus \n");
exit(1);
}
}
daemonize();
initialize();
slogn("AirTop Front-Panel Service -- start");
/*
* Main FP communication routine is armed
* upon SIGALRM reception.
*/
alarm(ATFP_MAIN_STARTUP_DELAY);
FP_store_daemon_postcode();
daemon_termination(DTERM_WAIT);
slogn("AirTop Front-Panel Service -- stop");
cleanup();
return 0;
}