Merge "Disabled in container environment" into tizen
[platform/core/connectivity/net-config.git] / src / main.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 <stdio.h>
21 #include <errno.h>
22 #include <unistd.h>
23 #include <sys/stat.h>
24 #include <system_info.h>
25
26 #include "log.h"
27 #include "util.h"
28 #include "wifi.h"
29 #include "setting.h"
30 #include "netdbus.h"
31 #include "emulator.h"
32 #include "neterror.h"
33 #include "wifi-agent.h"
34 #include "wifi-power.h"
35 #include "vpnsvc.h"
36 #include "mptcp.h"
37 #include "network-clock.h"
38 #include "network-dpm.h"
39 #include "network-state.h"
40 #include "network-monitor.h"
41 #include "signal-handler.h"
42 #include "network-statistics.h"
43 #include "network-dump.h"
44 #include "ethernet.h"
45 #include "wifi-firmware.h"
46
47 static GMainLoop *main_loop = NULL;
48
49 /*Poll the ethernet Cable Plug-in /Plug-out status at every 1000 ms*/
50 #define ETH_POLLING_TIME       1000
51 #define CONTAINER_FILE "/run/systemd/container"
52
53 /* Callback to Poll the Ethernet Status*/
54 gboolean __net_ethernet_cable_status_polling_callback(gpointer data)
55 {
56         netconfig_ethernet_cable_plugin_status_check();
57         return TRUE;
58 }
59
60 void _got_name_cb(void)
61 {
62         wifi_object_create_and_init();
63         state_object_create_and_init();
64         statistics_object_create_and_init();
65         vpnsvc_create_and_init();
66         mptcp_object_create_and_init();
67         ethernet_object_create_and_init();
68
69         register_gdbus_signal();
70         connman_register_agent();
71
72         if (TIZEN_TV)
73                 __netconfig_set_ether_macaddr();
74 }
75
76 static void _objects_deinit(void)
77 {
78         cleanup_gdbus();
79         wifi_object_deinit();
80         state_object_deinit();
81         statistics_object_deinit();
82         vpnsvc_destroy_deinit();
83         mptcp_object_deinit();
84         ethernet_object_deinit();
85 }
86
87 int main(int argc, char *argv[])
88 {
89         int ret;
90         int check_ethernet_monitor_timer = 0;
91         char *ifname = NULL;
92         char *wifi_def_mac = NULL;
93
94         umask(0077);
95
96         DBG("Network Configuration service");
97         if (daemon(0, 0) != 0)
98                 DBG("Cannot start daemon");
99
100         if (mkdir(WIFI_STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
101                         S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
102                 if (errno != EEXIST)
103                         ERR("Failed to create Wi-Fi directory");
104         }
105
106         if (mkdir(WIFI_CERT_STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
107                         S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
108                 if (errno != EEXIST)
109                         ERR("Failed to create cert directory");
110         }
111
112 #if !GLIB_CHECK_VERSION(2, 36, 0)
113         g_type_init();
114 #endif
115
116         main_loop = g_main_loop_new(NULL, FALSE);
117         if (main_loop == NULL) {
118                 ERR("Couldn't create GMainLoop\n");
119                 log_cleanup();
120                 return 0;
121         }
122
123         if (access(CONTAINER_FILE, F_OK) == 0) {
124                 g_main_loop_run(main_loop);
125                 log_cleanup();
126                 return 0;
127         }
128
129         /* Backward compatibility:
130          * Execute only when the old style hal architecture is not running
131          */
132         if (!wifi_check_systemd_service()) {
133                 ifname = netconfig_get_default_ifname_from_file();
134
135                 if (ifname && netconfig_wifi_firmware_get_mac(ifname, &wifi_def_mac) == 0)
136                         wifi_set_default_mac(wifi_def_mac);
137
138                 g_free(ifname);
139         }
140
141         ret = setup_gdbus(_got_name_cb);
142         if (ret > 0) {
143                 ERR("_netconfig_setup_gdbus is failed\n");
144                 return 0;
145         }
146
147         netconfig_setting_init();
148
149         netconfig_error_init();
150
151         if (TIZEN_NTP_ENABLE)
152                 netconfig_clock_init();
153
154         /* If its environment uses Emulator, network configuration is set by emulator default */
155         emulator_test_and_start();
156
157         /* For device policy manager */
158         netconfig_dpm_init();
159
160         /* Start tcpdump if dump state is on */
161 #if defined TIZEN_DEBUG_ENABLE
162         check_dump_state_and_start();
163 #endif
164
165         /* Backward compatibility:
166          * Execute only when the old style hal architecture is running
167          */
168         if (TIZEN_WLAN_BOARD_SPRD && wifi_check_systemd_service())
169                 wifi_firmware_download();
170
171         /*In case no emulator, set the ETH0 Mac address*/
172         if (TIZEN_TV && emulator_is_emulated() == FALSE)
173                 __netconfig_set_ether_macaddr();
174
175         if (netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_ETHERNET)) {
176                 /* Register the callback to check the ethernet Plug-in /Plug-out Status */
177                 check_ethernet_monitor_timer = g_timeout_add(ETH_POLLING_TIME,
178                                 __net_ethernet_cable_status_polling_callback,
179                                 &check_ethernet_monitor_timer);
180         }
181
182         netconfig_plugin_init();
183
184         g_main_loop_run(main_loop);
185
186         netconfig_plugin_deinit();
187
188         _objects_deinit();
189
190         log_cleanup();
191
192         deregister_gdbus_signal();
193
194         netconfig_dpm_deinit();
195
196         netconfig_setting_deinit();
197
198         if (TIZEN_NTP_ENABLE)
199                 netconfig_clock_deinit();
200
201
202         /*remove the Timer*/
203         if (check_ethernet_monitor_timer > 0)
204                 g_source_remove(check_ethernet_monitor_timer);
205
206         wifi_state_notifier_cleanup();
207
208         /* Unregistering the agent */
209         connman_unregister_agent();
210
211         return 0;
212 }