Fixed memory leaks
[platform/core/connectivity/net-config.git] / src / main.c
old mode 100644 (file)
new mode 100755 (executable)
index 1f9fcfc..6c4c648
@@ -1,7 +1,7 @@
 /*
  * Network Configuration Module
  *
- * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd. All rights reserved.
+ * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  *
  */
 
-#include <systemd/sd-daemon.h>
-#include <getopt.h>
-#include <unistd.h>
-#include <signal.h>
+#include <stdio.h>
 #include <errno.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <system_info.h>
 
 #include "log.h"
-#include "wifi.h"
 #include "util.h"
-#include "emulator.h"
+#include "wifi.h"
 #include "netdbus.h"
+#include "emulator.h"
+#include "neterror.h"
+#include "wifi-agent.h"
+#include "wifi-power.h"
+#include "vpnsvc.h"
+#include "mptcp.h"
 #include "network-clock.h"
+#include "network-dpm.h"
 #include "network-state.h"
-#include "network-statistics.h"
+#include "network-monitor.h"
 #include "signal-handler.h"
-#include "wifi-agent.h"
+#include "network-statistics.h"
+#include "network-dump.h"
 
 static GMainLoop *main_loop = NULL;
 
-static int no_fork = FALSE;
+/*Poll the ethernet Cable Plug-in /Plug-out status at every 1000 ms*/
+#define ETH_POLLING_TIME       1000
 
-int netconfig_test_input_parameters(int argc, char* argv[])
+/* Callback to Poll the Ethernet Status*/
+gboolean __net_ethernet_cable_status_polling_callback(gpointer data)
 {
-        struct option tab[] = {
-                { "nofork", no_argument, 0, 0 },
-                { NULL, 0, NULL, 0 }
-        };
-        int idx = 0;
+       netconfig_ethernet_cable_plugin_status_check();
+       return TRUE;
+}
 
-        while (getopt_long(argc, argv, "", tab, &idx) >= 0) {
+void _got_name_cb(void)
+{
+       wifi_object_create_and_init();
+       state_object_create_and_init();
+       statistics_object_create_and_init();
+       vpnsvc_create_and_init();
+       mptcp_object_create_and_init();
 
-               if (idx == 0)
-                       no_fork = TRUE;
-               idx = 0;
-       }
-       return 0;
+       register_gdbus_signal();
+       connman_register_agent();
+
+       if (TIZEN_TV)
+               __netconfig_set_ether_macaddr();
+}
+
+static void _objects_deinit(void)
+{
+       cleanup_gdbus();
+       wifi_object_deinit();
+       state_object_deinit();
+       statistics_object_deinit();
+       vpnsvc_destroy_deinit();
+       mptcp_object_deinit();
 }
 
-int main(int argc, charargv[])
+int main(int argc, char *argv[])
 {
-       DBusGConnection *connection;
+       int ret;
+       int check_ethernet_monitor_timer = 0;
 
-       DBG("Network Configuration Module");
+       umask(0077);
 
-       /*
-        * Call parameters veryfication
-        */
-       netconfig_test_input_parameters(argc, argv);
+       DBG("Network Configuration service");
+       if (daemon(0, 0) != 0)
+               DBG("Cannot start daemon");
 
-       if (!no_fork) {
-               if (daemon(0, 0) != 0)
-                       DBG("Cannot start daemon");
+       if (mkdir(WIFI_STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
+                       S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
+               if (errno != EEXIST)
+                       ERR("Failed to create Wi-Fi directory");
        }
 
-       netconfig_set_wifi_mac_address();
+       if (mkdir(WIFI_CERT_STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
+                       S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
+               if (errno != EEXIST)
+                       ERR("Failed to create cert directory");
+       }
 
+#if !GLIB_CHECK_VERSION(2, 36, 0)
        g_type_init();
+#endif
 
        main_loop = g_main_loop_new(NULL, FALSE);
+       if (main_loop == NULL) {
+               ERR("Couldn't create GMainLoop\n");
+               return 0;
+       }
 
-       connection = netconfig_setup_dbus();
-       if (connection == NULL)
-               return -1;
+       ret = setup_gdbus(_got_name_cb);
+       if (ret > 0) {
+               ERR("_netconfig_setup_gdbus is failed\n");
+               return 0;
+       }
 
-       if (netconfig_network_state_create_and_init(connection) == NULL)
-               return -1;
+       netconfig_error_init();
 
-       netconfig_register_signal();
+       if (TIZEN_NTP_ENABLE)
+               netconfig_clock_init();
 
-       /* Registering the agent for exchanging security credentials */
-       netconfig_agent_register();
+       /* If its environment uses Emulator, network configuration is set by emulator default */
+       emulator_test_and_start();
 
-       if (netconfig_wifi_create_and_init(connection) == NULL)
-               return -1;
+       /* For device policy manager */
+       netconfig_dpm_init();
 
-       if (netconfig_network_statistics_create_and_init(connection) == NULL)
-               return -1;
+       /* Start tcpdump if dump state is on */
+#if defined TIZEN_DEBUG_ENABLE
+       check_dump_state_and_start();
+#endif
 
-       /* If its environment uses Emulator, network configuration is set by emulator default */
-       netconfig_emulator_test_and_start();
+       if (TIZEN_WLAN_BOARD_SPRD)
+               wifi_firmware_download();
+
+       /*In case no emulator, set the ETH0 Mac address*/
+       if (TIZEN_TV && emulator_is_emulated() == FALSE)
+               __netconfig_set_ether_macaddr();
+
+       if (netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_ETHERNET)) {
+               /* Register the callback to check the ethernet Plug-in /Plug-out Status */
+               check_ethernet_monitor_timer = g_timeout_add(ETH_POLLING_TIME,
+                               __net_ethernet_cable_status_polling_callback,
+                               &check_ethernet_monitor_timer);
+       }
 
-       // Notyfication to systemd
-       sd_notify(0, "READY=1");
+       netconfig_plugin_init();
 
        g_main_loop_run(main_loop);
 
-       netconfig_deregister_signal();
-       netconfig_wifi_state_notifier_cleanup();
+       netconfig_plugin_deinit();
+
+       _objects_deinit();
+
+       log_cleanup();
+
+       deregister_gdbus_signal();
+
+       netconfig_dpm_deinit();
+
+       if (TIZEN_NTP_ENABLE)
+               netconfig_clock_deinit();
+
+
+       /*remove the Timer*/
+       if (check_ethernet_monitor_timer > 0)
+               g_source_remove(check_ethernet_monitor_timer);
+
+       wifi_state_notifier_cleanup();
 
        /* Unregistering the agent */
-       netconfig_agent_unregister();
+       connman_unregister_agent();
 
        return 0;
 }