Fix dbus methods for mptcp
[platform/core/connectivity/net-config.git] / src / network-dump.c
1 /*
2  * Network Configuration Module
3  *
4  * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <glib.h>
21
22 #include "log.h"
23
24 #include "util.h"
25 #include "netdbus.h"
26 #include "neterror.h"
27 #include "network-dump.h"
28 #include <unistd.h>
29
30 #define NETWORK_DUMP_SCRIPT  "/opt/var/lib/net-config/network_dump.sh"
31
32 #define TCP_DUMP_IN_BOOT_TIME "/opt/usr/data/tcp_dump_mode"
33
34 static gboolean tcpdump_running = FALSE;
35
36 static const char *get_current_time(void)
37 {
38         struct timeval mytime;
39         struct tm stTempTime;
40         static char time_string[128];
41
42
43         struct tm {
44                 int tm_sec; // Seconds
45                 int tm_min; // Minutes
46                 int tm_hour; // Hour (0--23)
47                 int tm_mday; // Day of month (1--31)
48                 int tm_mon; // Month (0--11)
49                 int tm_year; // Year (calendar year minus 1900)
50                 int tm_wday; // Weekday (0--6; Sunday = 0)
51                 int tm_yday; // Day of year (0--365)
52                 int tm_isdst; // 0 if daylight savings time is not in effect)
53         };
54
55         gettimeofday(&mytime, NULL);
56         localtime_r(&mytime.tv_sec, &stTempTime);
57         g_snprintf(time_string, 128, "%04d.%02d.%02d-%02d_%02d_%02d",
58                         stTempTime.tm_year + 1900, stTempTime.tm_mon + 1, stTempTime.tm_mday,
59                         stTempTime.tm_hour, stTempTime.tm_min, stTempTime.tm_sec);
60         time_string[127] = '\0';
61
62         return time_string;
63 }
64
65 static int _start_dump(gchar *dump_path)
66 {
67         int rv = 0;
68         gchar *path = NETWORK_DUMP_SCRIPT;
69         char *const args[] = { NETWORK_DUMP_SCRIPT, dump_path, NULL };
70         char *const envs[] = { NULL };
71
72         rv = netconfig_execute_file(path, args, envs);
73         if (rv < 0) {
74                 ERR("Fail to execute network log dump shell");
75                 return -EIO;
76         }
77         return 0;
78 }
79
80 static gboolean _stop_tcpdump(void)
81 {
82         int ret;
83         const char *path = KILLALL_EXEC_PATH;
84         char *const args[] = { KILLALL_EXEC_PATH, "tcpdump", NULL };
85         char *const envs[] = { NULL };
86
87         if (!tcpdump_running) {
88                 DBG("tcpdump is not running");
89                 return FALSE;
90         }
91
92         ret = netconfig_execute_file(path, args, envs);
93         if (ret < 0) {
94                 DBG("Failed to Kill tcpdump, ret : %d\n", ret);
95                 return FALSE;
96         }
97
98         tcpdump_running = FALSE;
99
100         return TRUE;
101 }
102
103 static gboolean _start_tcpdump(void)
104 {
105         int ret;
106         const char *start_path = TCPDUMP_EXEC_PATH;
107         char filename[128] = { 0, };
108         g_snprintf(filename, 128, "%s/tcpdump-%s.pcap", NETWORK_LOG_PATH, get_current_time());
109         char *const start_args[] = { TCPDUMP_EXEC_PATH,
110                         "-W", "5", "-C", "50", "-i", "any", "-s", "0", "-w", filename, NULL};
111
112         if (tcpdump_running) {
113                 DBG("kill tcpdump.");
114                 gboolean ret = _stop_tcpdump();
115                 if (!ret)
116                         DBG("Failed to kill tcpdump.. ret : %d", ret);
117         }
118
119         ret = netconfig_execute_file_no_wait(start_path, start_args);
120         if (ret < 0) {
121                 DBG("Failed to start tcpdump, ret : %d", ret);
122                 return FALSE;
123         }
124
125         tcpdump_running = TRUE;
126
127         return tcpdump_running;
128 }
129
130 static gboolean _get_dump_state(void)
131 {
132         int ret = access(TCP_DUMP_IN_BOOT_TIME, F_OK);
133         if (ret != 0) {
134                 ERR("Dumpstate Get Failed (%d).", ret);
135                 return FALSE;
136         }
137
138         return TRUE;
139 }
140
141 gboolean handle_start_tcpdump(Tcpdump *object,
142                 GDBusMethodInvocation *context)
143 {
144         gboolean ret = FALSE;
145
146         ret = _start_tcpdump();
147         if (!ret) {
148                 netconfig_error_dbus_method_return(context,
149                                 NETCONFIG_ERROR_INTERNAL, "StartTCPDumpFailed");
150                 return TRUE;
151         }
152
153         DBG("Successfully started tcpdump and running..");
154         tcpdump_complete_start_tcpdump(object, context);
155
156         return TRUE;
157 }
158
159 gboolean handle_stop_tcpdump(Tcpdump *object, GDBusMethodInvocation *context)
160 {
161         gboolean ret = FALSE;
162
163         if (!tcpdump_running) {
164                 DBG("tcpdump not running");
165                 tcpdump_complete_stop_tcpdump(object, context);
166                 return TRUE;
167         }
168
169         ret = _stop_tcpdump();
170         if (!ret) {
171                 netconfig_error_dbus_method_return(context,
172                                 NETCONFIG_ERROR_INTERNAL, "StopTCPDumpFailed");
173                 return TRUE;
174         }
175
176         DBG("Successfully stopped tcpdump");
177         tcpdump_complete_stop_tcpdump(object, context);
178
179         return TRUE;
180 }
181
182 gboolean handle_get_tcpdump_state(Tcpdump *object, GDBusMethodInvocation *context)
183 {
184         DBG("tcpdump %s running", tcpdump_running ? "is" : "is not");
185
186         tcpdump_complete_get_tcpdump_state(object, context, tcpdump_running);
187
188         return TRUE;
189 }
190
191 static void _send_dump_signal(const gchar *sig_name)
192 {
193         gboolean reply;
194         GVariant *params;
195         GDBusConnection *connection = NULL;
196         GError *error = NULL;
197
198         connection = netdbus_get_connection();
199         if (connection == NULL) {
200                 DBG("GDBusconnection is NULL");
201                 return;
202         }
203
204         params = g_variant_new("(i)", getpid());
205         reply = g_dbus_connection_emit_signal(connection,
206                         NULL,
207                         DUMP_SERVICE_OBJECT_PATH,
208                         DUMP_SERVICE_INTERFACE,
209                         sig_name,
210                         params,
211                         &error);
212         if (reply != TRUE) {
213                 if (error != NULL) {
214                         ERR("Failed to send signal [%s]", error->message);
215                         g_error_free(error);
216                 }
217                 return;
218         }
219 }
220
221 int netconfig_dump_log(const char *path)
222 {
223         gchar *dump_path = NULL;
224
225         if (!path) {
226                 ERR("path is NULL. Dump Fail");
227                 return -1;
228         }
229         ERR("Dump is started");
230         _send_dump_signal(DUMP_START_SIGNAL);
231
232         dump_path = g_strdup(path);
233         _start_dump(dump_path);
234         _stop_tcpdump();
235         g_free(dump_path);
236
237         _send_dump_signal(DUMP_FINISH_SIGNAL);
238         ERR("Dump is finished");
239         return 0;
240 }
241
242 void check_dump_state_and_start(void)
243 {
244         if (!_get_dump_state())
245                 return;
246
247         if (!_start_tcpdump()) {
248                 ERR("Failed to start tcpdump");
249                 return;
250         }
251         DBG("Successfully started tcpdump and running..");
252
253 }