element: Remove element.c
[framework/connectivity/connman.git] / src / main.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <signal.h>
32 #include <getopt.h>
33 #include <sys/stat.h>
34 #include <net/if.h>
35
36 #include <gdbus.h>
37
38 #ifdef HAVE_CAPNG
39 #include <cap-ng.h>
40 #endif
41
42 #include "connman.h"
43
44 static struct {
45         connman_bool_t bg_scan;
46 } connman_settings  = {
47         .bg_scan = TRUE,
48 };
49
50 static GKeyFile *load_config(const char *file)
51 {
52         GError *err = NULL;
53         GKeyFile *keyfile;
54
55         keyfile = g_key_file_new();
56
57         g_key_file_set_list_separator(keyfile, ',');
58
59         if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
60                 connman_error("Parsing %s failed: %s", file, err->message);
61                 g_error_free(err);
62                 g_key_file_free(keyfile);
63                 return NULL;
64         }
65
66         return keyfile;
67 }
68
69 static void parse_config(GKeyFile *config)
70 {
71         GError *error = NULL;
72         gboolean boolean;
73
74         if (config == NULL)
75                 return;
76
77         DBG("parsing main.conf");
78
79         boolean = g_key_file_get_boolean(config, "General",
80                                                 "BackgroundScanning", &error);
81         if (error == NULL)
82                 connman_settings.bg_scan = boolean;
83
84         g_clear_error(&error);
85 }
86
87 static GMainLoop *main_loop = NULL;
88
89 static void sig_term(int sig)
90 {
91         connman_info("Terminating");
92
93         g_main_loop_quit(main_loop);
94 }
95
96 static void disconnect_callback(DBusConnection *conn, void *user_data)
97 {
98         connman_error("D-Bus disconnect");
99
100         g_main_loop_quit(main_loop);
101 }
102
103 static gchar *option_debug = NULL;
104 static gchar *option_device = NULL;
105 static gchar *option_plugin = NULL;
106 static gchar *option_nodevice = NULL;
107 static gchar *option_noplugin = NULL;
108 static gchar *option_wifi = NULL;
109 static gboolean option_detach = TRUE;
110 static gboolean option_dnsproxy = TRUE;
111 static gboolean option_compat = FALSE;
112 static gboolean option_version = FALSE;
113
114 static gboolean parse_debug(const char *key, const char *value,
115                                         gpointer user_data, GError **error)
116 {
117         if (value)
118                 option_debug = g_strdup(value);
119         else
120                 option_debug = g_strdup("*");
121
122         return TRUE;
123 }
124
125 static GOptionEntry options[] = {
126         { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
127                                 G_OPTION_ARG_CALLBACK, parse_debug,
128                                 "Specify debug options to enable", "DEBUG" },
129         { "device", 'i', 0, G_OPTION_ARG_STRING, &option_device,
130                         "Specify networking device or interface", "DEV" },
131         { "nodevice", 'I', 0, G_OPTION_ARG_STRING, &option_nodevice,
132                         "Specify networking interface to ignore", "DEV" },
133         { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
134                                 "Specify plugins to load", "NAME,..." },
135         { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
136                                 "Specify plugins not to load", "NAME,..." },
137         { "wifi", 'W', 0, G_OPTION_ARG_STRING, &option_wifi,
138                                 "Specify driver for WiFi/Supplicant", "NAME" },
139         { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
140                                 G_OPTION_ARG_NONE, &option_detach,
141                                 "Don't fork daemon to background" },
142         { "nodnsproxy", 'r', G_OPTION_FLAG_REVERSE,
143                                 G_OPTION_ARG_NONE, &option_dnsproxy,
144                                 "Don't enable DNS Proxy" },
145         { "compat", 'c', 0, G_OPTION_ARG_NONE, &option_compat,
146                                 "(obsolete)" },
147         { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
148                                 "Show version information and exit" },
149         { NULL },
150 };
151
152 const char *connman_option_get_string(const char *key)
153 {
154         if (g_strcmp0(key, "wifi") == 0) {
155                 if (option_wifi == NULL)
156                         return "nl80211,wext";
157                 else
158                         return option_wifi;
159         }
160
161         return NULL;
162 }
163
164 connman_bool_t connman_setting_get_bool(const char *key)
165 {
166         if (g_str_equal(key, "BackgroundScanning") == TRUE)
167                 return connman_settings.bg_scan;
168
169         return FALSE;
170 }
171
172 int main(int argc, char *argv[])
173 {
174         GOptionContext *context;
175         GError *error = NULL;
176         DBusConnection *conn;
177         DBusError err;
178         struct sigaction sa;
179         GKeyFile *config;
180
181 #ifdef HAVE_CAPNG
182         /* Drop capabilities */
183 #endif
184
185 #ifdef NEED_THREADS
186         if (g_thread_supported() == FALSE)
187                 g_thread_init(NULL);
188 #endif
189
190         context = g_option_context_new(NULL);
191         g_option_context_add_main_entries(context, options, NULL);
192
193         if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
194                 if (error != NULL) {
195                         g_printerr("%s\n", error->message);
196                         g_error_free(error);
197                 } else
198                         g_printerr("An unknown error occurred\n");
199                 exit(1);
200         }
201
202         g_option_context_free(context);
203
204         if (option_version == TRUE) {
205                 printf("%s\n", VERSION);
206                 exit(0);
207         }
208
209         if (option_detach == TRUE) {
210                 if (daemon(0, 0)) {
211                         perror("Can't start daemon");
212                         exit(1);
213                 }
214         }
215
216         if (mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
217                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
218                 if (errno != EEXIST)
219                         perror("Failed to create state directory");
220         }
221
222         if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
223                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
224                 if (errno != EEXIST)
225                         perror("Failed to create storage directory");
226         }
227
228         if (mkdir(STORAGEDIR "/stats", S_IRUSR | S_IWUSR | S_IXUSR |
229                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
230                 if (errno != EEXIST)
231                         perror("Failed to create statistics directory");
232         }
233
234         umask(0077);
235
236         main_loop = g_main_loop_new(NULL, FALSE);
237
238 #ifdef NEED_THREADS
239         if (dbus_threads_init_default() == FALSE) {
240                 fprintf(stderr, "Can't init usage of threads\n");
241                 exit(1);
242         }
243 #endif
244
245         dbus_error_init(&err);
246
247         conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
248         if (conn == NULL) {
249                 if (dbus_error_is_set(&err) == TRUE) {
250                         fprintf(stderr, "%s\n", err.message);
251                         dbus_error_free(&err);
252                 } else
253                         fprintf(stderr, "Can't register with system bus\n");
254                 exit(1);
255         }
256
257         g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
258
259         __connman_log_init(option_debug, option_detach);
260
261         __connman_dbus_init(conn);
262
263         config = load_config(CONFIGDIR "/main.conf");
264
265         parse_config(config);
266
267         __connman_storage_init();
268         __connman_technology_init();
269         __connman_notifier_init();
270         __connman_location_init();
271         __connman_service_init();
272         __connman_provider_init();
273         __connman_network_init();
274         __connman_device_init(option_device, option_nodevice);
275
276         __connman_agent_init();
277         __connman_iptables_init();
278         __connman_tethering_init();
279         __connman_counter_init();
280         __connman_manager_init();
281         __connman_profile_init();
282         __connman_config_init();
283         __connman_stats_init();
284         __connman_clock_init();
285
286         __connman_resolver_init(option_dnsproxy);
287         __connman_ipconfig_init();
288         __connman_rtnl_init();
289         __connman_task_init();
290         __connman_proxy_init();
291         __connman_detect_init();
292         __connman_session_init();
293         __connman_timeserver_init();
294         __connman_connection_init();
295
296         __connman_plugin_init(option_plugin, option_noplugin);
297
298         __connman_storage_init_profile();
299
300         __connman_rtnl_start();
301         __connman_dhcp_init();
302         __connman_wpad_init();
303         __connman_wispr_init();
304         __connman_rfkill_init();
305
306         g_free(option_device);
307         g_free(option_plugin);
308         g_free(option_nodevice);
309         g_free(option_noplugin);
310
311         memset(&sa, 0, sizeof(sa));
312         sa.sa_handler = sig_term;
313         sigaction(SIGINT, &sa, NULL);
314         sigaction(SIGTERM, &sa, NULL);
315
316         g_main_loop_run(main_loop);
317
318         __connman_rfkill_cleanup();
319         __connman_wispr_cleanup();
320         __connman_wpad_cleanup();
321         __connman_dhcp_cleanup();
322         __connman_provider_cleanup();
323         __connman_plugin_cleanup();
324         __connman_connection_cleanup();
325         __connman_timeserver_cleanup();
326         __connman_session_cleanup();
327         __connman_detect_cleanup();
328         __connman_proxy_cleanup();
329         __connman_task_cleanup();
330         __connman_rtnl_cleanup();
331         __connman_ipconfig_cleanup();
332         __connman_resolver_cleanup();
333
334         __connman_clock_cleanup();
335         __connman_stats_cleanup();
336         __connman_config_cleanup();
337         __connman_profile_cleanup();
338         __connman_manager_cleanup();
339         __connman_counter_cleanup();
340         __connman_agent_cleanup();
341         __connman_tethering_cleanup();
342         __connman_iptables_cleanup();
343         __connman_device_cleanup();
344         __connman_network_cleanup();
345         __connman_service_cleanup();
346         __connman_location_cleanup();
347         __connman_notifier_cleanup();
348         __connman_technology_cleanup();
349         __connman_storage_cleanup();
350
351         __connman_dbus_cleanup();
352
353         __connman_log_cleanup();
354
355         dbus_connection_unref(conn);
356
357         g_main_loop_unref(main_loop);
358
359         if (config)
360                 g_key_file_free(config);
361
362         return 0;
363 }