2 * Network Configuration Module
4 * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
27 #include "network-dump.h"
30 #define NETWORK_DUMP_SCRIPT "/opt/var/lib/net-config/network_dump.sh"
32 #define TCP_DUMP_IN_BOOT_TIME "/opt/usr/data/tcp_dump_mode"
34 static gboolean tcpdump_running = FALSE;
36 static const char *get_current_time(void)
38 struct timeval mytime;
40 static char time_string[128];
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)
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';
65 static int _start_dump(gchar *dump_path)
68 gchar *path = NETWORK_DUMP_SCRIPT;
69 char *const args[] = { NETWORK_DUMP_SCRIPT, dump_path, NULL };
70 char *const envs[] = { NULL };
72 rv = netconfig_execute_file(path, args, envs);
74 ERR("Fail to execute network log dump shell");
80 static gboolean _stop_tcpdump(void)
83 const char *path = KILLALL_EXEC_PATH;
84 char *const args[] = { KILLALL_EXEC_PATH, "tcpdump", NULL };
85 char *const envs[] = { NULL };
87 if (!tcpdump_running) {
88 DBG("tcpdump is not running");
92 ret = netconfig_execute_file(path, args, envs);
94 DBG("Failed to Kill tcpdump, ret : %d\n", ret);
98 tcpdump_running = FALSE;
103 static gboolean _start_tcpdump(void)
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};
112 if (tcpdump_running) {
113 DBG("kill tcpdump.");
114 gboolean ret = _stop_tcpdump();
116 DBG("Failed to kill tcpdump.. ret : %d", ret);
119 ret = netconfig_execute_file_no_wait(start_path, start_args);
121 DBG("Failed to start tcpdump, ret : %d", ret);
125 tcpdump_running = TRUE;
127 return tcpdump_running;
130 static gboolean _get_dump_state(void)
132 int ret = access(TCP_DUMP_IN_BOOT_TIME, F_OK);
134 ERR("Dumpstate Get Failed (%d).", ret);
141 gboolean handle_start_tcpdump(Tcpdump *object,
142 GDBusMethodInvocation *context)
144 gboolean ret = FALSE;
146 ret = _start_tcpdump();
148 netconfig_error_dbus_method_return(context,
149 NETCONFIG_ERROR_INTERNAL, "StartTCPDumpFailed");
153 DBG("Successfully started tcpdump and running..");
154 tcpdump_complete_start_tcpdump(object, context);
159 gboolean handle_stop_tcpdump(Tcpdump *object, GDBusMethodInvocation *context)
161 gboolean ret = FALSE;
163 if (!tcpdump_running) {
164 DBG("tcpdump not running");
165 tcpdump_complete_stop_tcpdump(object, context);
169 ret = _stop_tcpdump();
171 netconfig_error_dbus_method_return(context,
172 NETCONFIG_ERROR_INTERNAL, "StopTCPDumpFailed");
176 DBG("Successfully stopped tcpdump");
177 tcpdump_complete_stop_tcpdump(object, context);
182 gboolean handle_get_tcpdump_state(Tcpdump *object, GDBusMethodInvocation *context)
184 DBG("tcpdump %s running", tcpdump_running ? "is" : "is not");
186 tcpdump_complete_get_tcpdump_state(object, context, tcpdump_running);
191 static void _send_dump_signal(const gchar *sig_name)
195 GDBusConnection *connection = NULL;
196 GError *error = NULL;
198 connection = netdbus_get_connection();
199 if (connection == NULL) {
200 DBG("GDBusconnection is NULL");
204 params = g_variant_new("(i)", getpid());
205 reply = g_dbus_connection_emit_signal(connection,
207 DUMP_SERVICE_OBJECT_PATH,
208 DUMP_SERVICE_INTERFACE,
214 ERR("Failed to send signal [%s]", error->message);
221 int netconfig_dump_log(const char *path)
223 gchar *dump_path = NULL;
226 ERR("path is NULL. Dump Fail");
229 ERR("Dump is started");
230 _send_dump_signal(DUMP_START_SIGNAL);
232 dump_path = g_strdup(path);
233 _start_dump(dump_path);
237 _send_dump_signal(DUMP_FINISH_SIGNAL);
238 ERR("Dump is finished");
242 void check_dump_state_and_start(void)
244 if (!_get_dump_state())
247 if (!_start_tcpdump()) {
248 ERR("Failed to start tcpdump");
251 DBG("Successfully started tcpdump and running..");