main: Initial configuration file support
[platform/upstream/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 <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <signal.h>
31 #include <getopt.h>
32 #include <sys/stat.h>
33 #include <net/if.h>
34
35 #include <gdbus.h>
36
37 #ifdef HAVE_CAPNG
38 #include <cap-ng.h>
39 #endif
40
41 #include "connman.h"
42
43 struct connman_conf {
44         connman_bool_t bg_scan;
45 };
46
47 static struct connman_conf main_conf = {
48         .bg_scan = TRUE,
49 };
50
51 static GKeyFile *load_config(const char *file)
52 {
53         GError *err = NULL;
54         GKeyFile *keyfile;
55
56         keyfile = g_key_file_new();
57
58         g_key_file_set_list_separator(keyfile, ',');
59
60         if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
61                 connman_error("Parsing %s failed: %s", file, err->message);
62                 g_error_free(err);
63                 g_key_file_free(keyfile);
64                 return NULL;
65         }
66
67         return keyfile;
68 }
69
70 static void parse_config(GKeyFile *config)
71 {
72         GError *error = NULL;
73         gboolean boolean;
74
75         if (config == NULL)
76                 return;
77
78         DBG("parsing main.conf");
79
80         boolean = g_key_file_get_boolean(config, "General",
81                                                 "BackgroundScanning", &error);
82         if (error == NULL)
83                 main_conf.bg_scan = boolean;
84
85         g_clear_error(&error);
86 }
87
88 static GMainLoop *main_loop = NULL;
89
90 static void sig_term(int sig)
91 {
92         connman_info("Terminating");
93
94         g_main_loop_quit(main_loop);
95 }
96
97 static void disconnect_callback(DBusConnection *conn, void *user_data)
98 {
99         connman_error("D-Bus disconnect");
100
101         g_main_loop_quit(main_loop);
102 }
103
104 static gchar *option_debug = NULL;
105 static gchar *option_device = NULL;
106 static gchar *option_plugin = NULL;
107 static gchar *option_nodevice = NULL;
108 static gchar *option_noplugin = NULL;
109 static gchar *option_wifi = NULL;
110 static gboolean option_detach = TRUE;
111 static gboolean option_dnsproxy = TRUE;
112 static gboolean option_compat = FALSE;
113 static gboolean option_version = FALSE;
114
115 static gboolean parse_debug(const char *key, const char *value,
116                                         gpointer user_data, GError **error)
117 {
118         if (value)
119                 option_debug = g_strdup(value);
120         else
121                 option_debug = g_strdup("*");
122
123         return TRUE;
124 }
125
126 static GOptionEntry options[] = {
127         { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
128                                 G_OPTION_ARG_CALLBACK, parse_debug,
129                                 "Specify debug options to enable", "DEBUG" },
130         { "device", 'i', 0, G_OPTION_ARG_STRING, &option_device,
131                         "Specify networking device or interface", "DEV" },
132         { "nodevice", 'I', 0, G_OPTION_ARG_STRING, &option_nodevice,
133                         "Specify networking interface to ignore", "DEV" },
134         { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
135                                 "Specify plugins to load", "NAME,..." },
136         { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
137                                 "Specify plugins not to load", "NAME,..." },
138         { "wifi", 'W', 0, G_OPTION_ARG_STRING, &option_wifi,
139                                 "Specify driver for WiFi/Supplicant", "NAME" },
140         { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
141                                 G_OPTION_ARG_NONE, &option_detach,
142                                 "Don't fork daemon to background" },
143         { "nodnsproxy", 'r', G_OPTION_FLAG_REVERSE,
144                                 G_OPTION_ARG_NONE, &option_dnsproxy,
145                                 "Don't enable DNS Proxy" },
146         { "compat", 'c', 0, G_OPTION_ARG_NONE, &option_compat,
147                                 "(obsolete)" },
148         { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
149                                 "Show version information and exit" },
150         { NULL },
151 };
152
153 const char *connman_option_get_string(const char *key)
154 {
155         if (g_strcmp0(key, "wifi") == 0) {
156                 if (option_wifi == NULL)
157                         return "nl80211,wext";
158                 else
159                         return option_wifi;
160         }
161
162         return NULL;
163 }
164
165 connman_bool_t connman_configuration_get_bool(const char *key)
166 {
167         if (g_str_equal(key, "BackgroundScanning") == TRUE)
168                 return main_conf.bg_scan;
169
170         return FALSE;
171 }
172
173 int main(int argc, char *argv[])
174 {
175         GOptionContext *context;
176         GError *error = NULL;
177         DBusConnection *conn;
178         DBusError err;
179         struct sigaction sa;
180         mode_t old_umask;
181         GKeyFile *config;
182
183 #ifdef HAVE_CAPNG
184         /* Drop capabilities */
185 #endif
186
187 #ifdef NEED_THREADS
188         if (g_thread_supported() == FALSE)
189                 g_thread_init(NULL);
190 #endif
191
192         context = g_option_context_new(NULL);
193         g_option_context_add_main_entries(context, options, NULL);
194
195         if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
196                 if (error != NULL) {
197                         g_printerr("%s\n", error->message);
198                         g_error_free(error);
199                 } else
200                         g_printerr("An unknown error occurred\n");
201                 exit(1);
202         }
203
204         g_option_context_free(context);
205
206         if (option_version == TRUE) {
207                 printf("%s\n", VERSION);
208                 exit(0);
209         }
210
211         if (option_detach == TRUE) {
212                 if (daemon(0, 0)) {
213                         perror("Can't start daemon");
214                         exit(1);
215                 }
216         }
217
218         if (mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
219                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
220                 if (errno != EEXIST)
221                         perror("Failed to create state directory");
222         }
223
224         if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
225                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
226                 if (errno != EEXIST)
227                         perror("Failed to create storage directory");
228         }
229
230         if (mkdir(STORAGEDIR "/stats", S_IRUSR | S_IWUSR | S_IXUSR |
231                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
232                 if (errno != EEXIST)
233                         perror("Failed to create statistics directory");
234         }
235
236         old_umask = umask(077);
237
238         main_loop = g_main_loop_new(NULL, FALSE);
239
240 #ifdef NEED_THREADS
241         if (dbus_threads_init_default() == FALSE) {
242                 fprintf(stderr, "Can't init usage of threads\n");
243                 exit(1);
244         }
245 #endif
246
247         dbus_error_init(&err);
248
249         conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
250         if (conn == NULL) {
251                 if (dbus_error_is_set(&err) == TRUE) {
252                         fprintf(stderr, "%s\n", err.message);
253                         dbus_error_free(&err);
254                 } else
255                         fprintf(stderr, "Can't register with system bus\n");
256                 exit(1);
257         }
258
259         g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
260
261         __connman_log_init(option_debug, option_detach);
262
263         __connman_dbus_init(conn);
264
265         config = load_config(CONFIGDIR "/main.conf");
266
267         parse_config(config);
268
269         __connman_storage_init();
270         __connman_element_init(option_device, option_nodevice);
271
272         __connman_agent_init();
273         __connman_iptables_init();
274         __connman_tethering_init();
275         __connman_counter_init();
276         __connman_manager_init();
277         __connman_profile_init();
278         __connman_config_init();
279         __connman_stats_init();
280
281         __connman_resolver_init(option_dnsproxy);
282         __connman_ipconfig_init();
283         __connman_rtnl_init();
284         __connman_task_init();
285         __connman_proxy_init();
286         __connman_detect_init();
287         __connman_session_init();
288         __connman_timeserver_init();
289         __connman_connection_init();
290
291         __connman_plugin_init(option_plugin, option_noplugin);
292
293         __connman_element_start();
294
295         g_free(option_device);
296         g_free(option_plugin);
297         g_free(option_nodevice);
298         g_free(option_noplugin);
299
300         memset(&sa, 0, sizeof(sa));
301         sa.sa_handler = sig_term;
302         sigaction(SIGINT, &sa, NULL);
303         sigaction(SIGTERM, &sa, NULL);
304
305         g_main_loop_run(main_loop);
306
307         __connman_element_stop();
308
309         __connman_plugin_cleanup();
310
311         __connman_connection_cleanup();
312         __connman_timeserver_cleanup();
313         __connman_session_cleanup();
314         __connman_detect_cleanup();
315         __connman_proxy_cleanup();
316         __connman_task_cleanup();
317         __connman_rtnl_cleanup();
318         __connman_ipconfig_cleanup();
319         __connman_resolver_cleanup();
320
321         __connman_stats_cleanup();
322         __connman_config_cleanup();
323         __connman_profile_cleanup();
324         __connman_manager_cleanup();
325         __connman_counter_cleanup();
326         __connman_agent_cleanup();
327         __connman_tethering_cleanup();
328         __connman_iptables_cleanup();
329
330         __connman_element_cleanup();
331         __connman_storage_cleanup();
332
333         __connman_dbus_cleanup();
334
335         __connman_log_cleanup();
336
337         dbus_connection_unref(conn);
338
339         g_main_loop_unref(main_loop);
340
341         if (config)
342                 g_key_file_free(config);
343
344         return 0;
345 }