Initialize smart traffic control iptables package
[platform/core/connectivity/stc-iptables.git] / src / stc-iptables-log.c
1 /*
2  *  Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License version 2 as
6  *  published by the Free Software Foundation.
7  *
8  *  This program is distributed in the hope that it will be useful,
9  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  *  GNU General Public License for more details.
12  *
13  *  You should have received a copy of the GNU General Public License
14  *  along with this program; if not, write to the Free Software
15  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
16  */
17
18 #include <glib.h>
19 #include <sys/time.h>
20 #include <stdio.h>
21 #include <stdarg.h>
22 #include <unistd.h>
23 #include <sys/stat.h>
24
25 #include "stc-iptables-log.h"
26
27 #define LOG_FILE_PATH   "/opt/usr/data/network/stc-iptables.log"
28 #define MAX_LOG_SIZE    1 * 1024 * 1024
29 #define MAX_LOG_COUNT   1
30
31 static FILE *log_file = NULL;
32
33 static void __stc_iptables_log_update_file_revision(int rev)
34 {
35         int next_log_rev = 0;
36         char *log_file = NULL;
37         char *next_log_file = NULL;
38
39         next_log_rev = rev + 1;
40
41         log_file = g_strdup_printf("%s.%d", LOG_FILE_PATH, rev);
42         next_log_file = g_strdup_printf("%s.%d", LOG_FILE_PATH, next_log_rev);
43
44         if (next_log_rev >= MAX_LOG_COUNT)
45                 remove(next_log_file);
46
47         if (access(next_log_file, F_OK) == 0)
48                 __stc_iptables_log_update_file_revision(next_log_rev);
49
50         if (rename(log_file, next_log_file) != 0)
51                 remove(log_file);
52
53         g_free(log_file);
54         g_free(next_log_file);
55 }
56
57 static void __stc_iptables_log_make_backup(void)
58 {
59         const int rev = 0;
60         char *backup = NULL;
61
62         backup = g_strdup_printf("%s.%d", LOG_FILE_PATH, rev);
63
64         if (access(backup, F_OK) == 0)
65                 __stc_iptables_log_update_file_revision(rev);
66
67         if (rename(LOG_FILE_PATH, backup) != 0)
68                 remove(LOG_FILE_PATH);
69
70         g_free(backup);         
71 }
72
73 static void __stc_iptables_log_get_local_time(char *strtime, const int size)
74 {
75         time_t buf;
76         struct tm *local_ptm;
77         struct tm result = {0, };
78
79         time(&buf);
80         buf = time(NULL);
81         local_ptm = localtime_r(&buf, &result);
82
83         if (local_ptm)
84                 strftime(strtime, size, "%m/%d %H:%M:%S", local_ptm);
85 }
86
87 void stc_iptables_log(const char *format, ...)
88 {
89         va_list ap;
90         int log_size = 0;
91         struct stat buf;
92         char str[256];
93         char strtime[40];
94
95         if (log_file == NULL)
96                 log_file = (FILE *)fopen(LOG_FILE_PATH, "a+");
97
98         if (log_file == NULL)
99                 return;
100
101         va_start(ap, format);
102
103         if (fstat(fileno(log_file), &buf) == 0)
104                 log_size = buf.st_size;
105
106         if (log_size >= MAX_LOG_SIZE) {
107                 fclose(log_file);
108                 log_file = NULL;
109
110                 __stc_iptables_log_make_backup();
111
112                 log_file = (FILE *)fopen(LOG_FILE_PATH, "a+");
113
114                 if (log_file == NULL) {
115                         va_end(ap);
116                         return;
117                 }
118         }
119
120         __stc_iptables_log_get_local_time(strtime, sizeof(strtime));
121
122         if (vsnprintf(str, sizeof(str), format, ap) > 0)
123                 fprintf(log_file, "%s %s", strtime, str);
124
125         va_end(ap);
126 }
127
128 gboolean stc_iptables_log_sync(gpointer data)
129 {
130         if (log_file) {
131                 fflush(log_file);
132                 fsync(log_file->_fileno);
133         }
134
135         return TRUE;
136 }
137
138 void stc_iptables_log_cleanup(void)
139 {
140         if (log_file == NULL)
141                 return;
142
143         fclose(log_file);
144         log_file = NULL;
145 }