-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmxhtspd.c
348 lines (291 loc) · 7.1 KB
/
mxhtspd.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
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/select.h>
#include <sys/time.h>
#include <signal.h>
#include <string.h>
#include <stdarg.h>
#include <syslog.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include "mxhtsp.h"
#include "mxhtspd.h"
/* Jared, the control should be moved the global variable for accessing safely in another sub-function */
/* default control setting */
static daemon_ctrl control = {
.time_btn_long = TIME_BTN_LONG_PRESSED,
.time_chk_part = TIME_PARTITION_CHECK,
.force_chk_part = 0,
.log_facility = -1,
.daemonize = 0,
.verbose = 0,
.exit = 0
};
static daemon_ctrl *ctrl; /* A pointer for accessing control */
static const char *dir = "/usr/sbin/";
static void msleep(unsigned long msec)
{
struct timeval time;
time.tv_sec = msec / 1000;
time.tv_usec = (msec % 1000) * 1000;
select(1, NULL, NULL, NULL, &time);
}
static void mprintf(const char *fmt, ...)
{
char buf[BUFSIZ];
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
if (ctrl->log_facility >= 0)
syslog(LOG_NOTICE, "%s", buf);
else
printf("mxhtspd: %s", buf);
}
static void execute_script(char *cmd, int param)
{
char name[50];
d("%s %d\n", cmd, param);
sprintf(name, "%s%s %d &", dir, cmd, param);
system(name);
}
static void handle_disk_plugged(int fd, int n, htsp_info *info)
{
int ret;
ret = mxhtsp_is_disk_plugged(fd, n);
if ((ret == 1) && (info[n-1].disk_plugged == 0)) {
mprintf("Disk %d is plugged\n", n);
if (ctrl->verbose)
mprintf("call action-disk-plugged %d\n", n);
execute_script("action-disk-plugged", n);
ctrl->force_chk_part = 1;
info[n-1].first_insert = 1;
mxhtsp_set_led(fd, n, 1);
info[n-1].led_on = 1;
sleep (1);
} else if ((ret == 0) && (info[n-1].disk_plugged == 1)) {
mprintf("Disk %d is unplugged\n", n);
if (ctrl->verbose)
mprintf("call action-disk-plugged %d\n", n);
/*
* Note: partition should be umounted before unplugged, and
* it's advicable to umount all partition in action-disk-unplugged script.
* Here is just a prevention for operation mistake
*/
execute_script("action-disk-unplugged", n);
ctrl->force_chk_part = 1;
mxhtsp_set_led(fd, n, 0);
info[n-1].led_on = 0;
}
info[n-1].disk_plugged = ret;
}
static int handle_button_pressed(int fd, int n, htsp_info *info)
{
int now_press;
double time_used;
double time_threshold = (double)ctrl->time_btn_long * 1000.0;
clock_t now;
/* int *ta = &info[n-1].btn_time_accum; */
int *btn_event_start = &info[n-1].btn_event_start;
int *btn_last_pressed = &info[n-1].btn_last_pressed;
now_press = mxhtsp_is_button_pressed(fd, n);
if (now_press < 0) {
mprintf("detect button pressed error!");
return -1;
}
if (now_press == 1 && (*btn_last_pressed == 0)) {
/* record pressed time */
info[n-1].press_time = clock();
*btn_event_start = 1;
*btn_last_pressed = 1;
} else if (now_press == 0) {
if (*btn_last_pressed == 1) {
now = clock();
time_used = ((double) (now - info[n-1].press_time));
mprintf("[btn=%d, pressed time elapsed = %f]\n", n, time_used);
/* check elapsed time over threshold */
if (time_used > time_threshold) {
if (*btn_event_start && info[n-1].first_insert == 0) {
mprintf("Button %d is pressed\n", n);
if (mxhtsp_is_disk_busy(fd, n) == 1) { /* busy */
mprintf("Disk %d is busy.\n", n);
if (ctrl->verbose)
mprintf("call action-btn-pressed %d\n", n+2);
execute_script("action-btn-pressed", n+2);
*btn_event_start = 0;
} else {
mprintf("Unmount the disk %d and unplug it.\n", n);
if (ctrl->verbose)
mprintf("call action-btn-pressed %d\n", n);
execute_script("action-btn-pressed", n);
*btn_event_start = 0;
}
}
} else {
/* handling re-scanning SATA device */
if (*btn_event_start && info[n-1].first_insert == 0) {
mprintf("Button %d is pressed - Scanning Disk %d\n", n, n);
if (ctrl->verbose)
mprintf("call mxhtspd-handle-disk-plugged %d\n", n);
execute_script("mxhtspd-handle-disk-plugged", n);
*btn_event_start = 0;
}
}
}
*btn_last_pressed = 0;
info[n-1].first_insert = 0;
}
return 0;
}
void sig_handler(int signo)
{
ctrl->exit = 1;
}
static void usage(void)
{
printf("Usage: mxhtspd [options] \n");
/* printf("\t-i interval in seconds to check partition usage (defulat 60 secs) \n"); */
printf("\t-l facility_num log daemon's message with LOCAL[facility_num]\n");
printf("\t-v run in verbose mode \n");
printf("\t-h print usage\n");
exit(1);
}
static void parseopt(int argc, char **argv)
{
char opt;
int num;
int facility[8] = {LOG_LOCAL0, LOG_LOCAL1, LOG_LOCAL2, LOG_LOCAL3,
LOG_LOCAL4, LOG_LOCAL5, LOG_LOCAL6, LOG_LOCAL7};
while ((opt = getopt(argc, argv, "t:i:l:dvh")) != -1) {
switch (opt) {
case 't':
num = atoi(optarg);
if (num <= 0) {
mprintf("can't set -t %d", ctrl->time_btn_long);
exit(1);
}
ctrl->time_btn_long = num;
break;
case 'i':
num = atoi(optarg);
if (num <= 0) {
mprintf("can't set -i %d", ctrl->time_chk_part);
exit(1);
}
ctrl->time_chk_part = num;
break;
case 'l':
num = atoi(optarg);
if ((num > 7) && (num < 0)) {
mprintf("set a wrong log facility %d\n", num);
exit(1);
}
openlog("mxhtspd", LOG_CONS | LOG_PID | LOG_NDELAY, facility[num]);
syslog(LOG_NOTICE, "Starting mxthspd daemon...");
d("start with local %d\n", num);
ctrl->log_facility = num;
break;
case 'd':
ctrl->daemonize = 1;
break;
case 'v':
ctrl->verbose = 1;
break;
case 'h':
usage();
break;
case '?':
usage();
}
}
}
static int create_pid_file(void)
{
FILE *fp;
pid_t pid = getpid();
fp = fopen(PID_FILE, "w");
if (!fp) {
mprintf("can't open pid file\n");
return -1;
}
if (fprintf(fp, "%d\n", (int)pid) <= 0) {
return -1;
}
fclose(fp);
return 0;
}
void remove_pid_file(void)
{
if (unlink(PID_FILE))
mprintf("can't remove pid file");
}
static void daemonize(void)
{
pid_t pid, sid;
/* already a daemon */
if (getppid() == 1)
return;
pid = fork();
/* error */
if (pid < 0) {
exit(1);
}
/* fork off parent */
if (pid > 0) {
exit(0);
}
/* now execute as child process */
umask(0);
/* get a unique session id */
sid = setsid();
if (sid < 0) {
exit(1);
}
/* change work directory to /, prevent to lock a directory */
if ((chdir("/")) < 0) {
exit(1);
}
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
}
int main(int argc, char *argv[])
{
int fd;
int i;
htsp_info info[2] = {};
ctrl = &control;
parseopt(argc, argv);
/* daemonzie */
if (ctrl->daemonize)
daemonize();
fd = mxhtsp_open();
if (fd < 0) {
mprintf("can't open device\n");
exit(1);
}
/* signal handler */
signal(SIGTERM, sig_handler);
signal(SIGINT, sig_handler);
signal(SIGKILL, sig_handler);
create_pid_file();
while (1) {
if (ctrl->exit)
break;
for (i = 1; i <= 2; i++) {
handle_disk_plugged(fd, i, info);
handle_button_pressed(fd, i, info);
}
msleep(TIME_SLEEP_MS);
}
mprintf("Stopping...\n");
remove_pid_file();
closelog();
mxhtsp_set_led(fd, 1, 0);
mxhtsp_set_led(fd, 2, 0);
close(fd);
return 0;
}