Initialize Tizen 2.3
[framework/connectivity/net-config.git] / mobile / src / main.c
1 /*
2  * Network Configuration Module
3  *
4  * Copyright (c) 2012-2013 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 <systemd/sd-daemon.h>
21 #include <getopt.h>
22 #include <unistd.h>
23 #include <signal.h>
24 #include <errno.h>
25
26 #include "log.h"
27 #include "wifi.h"
28 #include "util.h"
29 #include "emulator.h"
30 #include "netdbus.h"
31 #include "network-clock.h"
32 #include "network-state.h"
33 #include "network-statistics.h"
34 #include "signal-handler.h"
35 #include "wifi-agent.h"
36
37 static GMainLoop *main_loop = NULL;
38
39 static int no_fork = FALSE;
40
41 void netconfig_signal_handler_SIGTERM(int signum)
42 {
43         g_main_loop_quit(main_loop);
44 }
45
46 int netconfig_register_signal_handler_SIGTERM(void)
47 {
48         struct sigaction sigset;
49
50         sigemptyset(&sigset.sa_mask);
51         sigaddset( &sigset.sa_mask, SIGTERM );
52         sigset.sa_flags = 0;
53         sigset.sa_handler = netconfig_signal_handler_SIGTERM;
54
55         if (sigaction( SIGTERM, &sigset, NULL) < 0) {
56                 ERR("Sigaction for SIGTERM failed [%s]", strerror( errno ));
57                 return -1;
58         }
59
60         INFO( "Handler for SIGTERM ok" );
61         return 0;
62 }
63
64 int netconfig_test_input_parameters(int argc, char* argv[])
65 {
66         struct option tab[] = {
67                 { "nofork", no_argument, 0, 0 },
68                 { NULL, 0, NULL, 0 }
69         };
70         int idx = 0;
71
72         while (getopt_long(argc, argv, "", tab, &idx) >= 0) {
73
74                 if (idx == 0)
75                         no_fork = TRUE;
76                 idx = 0;
77         }
78         return 0;
79 }
80
81 int main(int argc, char* argv[])
82 {
83         DBusGConnection *connection;
84
85         DBG("Network Configuration Module");
86
87         /*
88          * Call parameters veryfication
89          */
90         netconfig_test_input_parameters(argc, argv);
91
92         if (!no_fork) {
93                 if (daemon(0, 0) != 0)
94                         DBG("Cannot start daemon");
95         }
96
97         netconfig_set_wifi_mac_address();
98
99         g_type_init();
100
101         main_loop = g_main_loop_new(NULL, FALSE);
102
103         connection = netconfig_setup_dbus();
104         if (connection == NULL)
105                 return -1;
106
107         if (netconfig_network_state_create_and_init(connection) == NULL)
108                 return -1;
109
110         netconfig_register_signal();
111
112         /* Registering the agent for exchanging security credentials */
113         netconfig_agent_register();
114
115         if (netconfig_wifi_create_and_init(connection) == NULL)
116                 return -1;
117
118         if (netconfig_network_statistics_create_and_init(connection) == NULL)
119                 return -1;
120
121         /* Register SIGCHLD signal handler function */
122         if (netconfig_register_signal_handler_SIGTERM() != 0)
123                 return -1;
124
125         /* If its environment uses Emulator, network configuration is set by emulator default */
126         netconfig_emulator_test_and_start();
127
128         // Notyfication to systemd
129         sd_notify(0, "READY=1");
130
131         g_main_loop_run(main_loop);
132
133         netconfig_deregister_signal();
134         netconfig_wifi_state_notifier_cleanup();
135
136         /* Unregistering the agent */
137         netconfig_agent_unregister();
138
139         return 0;
140 }