1/*
2	when traffic analyzer is enabled, bwdpi_db_10 will start to save database each minute, after 10 mins, it will be killed.
3*/
4#include <rc.h>
5#include <bwdpi.h>
6
7static int sig_cur = -1;
8static int count = 0;
9
10static void save_database_10()
11{
12	count++;
13
14	int debug = nvram_get_int("sqlite_debug");
15	if(debug) dbg("[bwdpi_db_10] count=%d\n", count);
16
17	if(count >= 10){
18		remove("/var/run/bwdpi_db_10.pid");
19		exit(0);
20	}
21	else{
22		hm_traffic_analyzer_save();
23	}
24}
25
26static void catch_sig(int sig)
27{
28	int debug = nvram_get_int("sqlite_debug");
29	sig_cur = sig;
30
31	if(sig == SIGALRM)
32	{
33		if(debug) dbg("[bwdpi_db_10] check alive...\n");
34		save_database_10();
35	}
36	else if (sig == SIGTERM)
37	{
38		if(debug) dbg("[bwdpi_db_10] killed!!\n");
39		remove("/var/run/bwdpi_db_10.pid");
40		exit(0);
41	}
42}
43
44int bwdpi_db_10_main(int argc, char **argv)
45{
46	int debug = nvram_get_int("sqlite_debug");
47
48	FILE *fp;
49	sigset_t sigs_to_catch;
50
51	/* write pid */
52	if ((fp = fopen("/var/run/bwdpi_db_10.pid", "w")) != NULL)
53	{
54		fprintf(fp, "%d", getpid());
55		fclose(fp);
56	}
57
58	/* set the signal handler */
59	sigemptyset(&sigs_to_catch);
60	sigaddset(&sigs_to_catch, SIGTERM);
61	sigaddset(&sigs_to_catch, SIGALRM);
62	sigprocmask(SIG_UNBLOCK, &sigs_to_catch, NULL);
63
64	signal(SIGTERM, catch_sig);
65	signal(SIGALRM, catch_sig);
66
67	while(1)
68	{
69		if(debug) dbg("[bwdpi_db_10] set alarm, count=%d\n", count);
70		if(count <= 6) alarm(10);
71		else if(count > 6) alarm(60);
72		pause();
73	}
74
75	return 0;
76}
77