main: Parse FallbackTimeservers list from main.conf
[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 <errno.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <signal.h>
32 #include <sys/signalfd.h>
33 #include <getopt.h>
34 #include <sys/stat.h>
35 #include <net/if.h>
36
37 #include <gdbus.h>
38
39 #ifdef HAVE_CAPNG
40 #include <cap-ng.h>
41 #endif
42
43 #include "connman.h"
44
45 static struct {
46         connman_bool_t bg_scan;
47         char **pref_timeservers;
48 } connman_settings  = {
49         .bg_scan = TRUE,
50         .pref_timeservers = NULL,
51 };
52
53 static GKeyFile *load_config(const char *file)
54 {
55         GError *err = NULL;
56         GKeyFile *keyfile;
57
58         keyfile = g_key_file_new();
59
60         g_key_file_set_list_separator(keyfile, ',');
61
62         if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
63                 if (err->code != G_FILE_ERROR_NOENT) {
64                         connman_error("Parsing %s failed: %s", file,
65                                                                 err->message);
66                 }
67
68                 g_error_free(err);
69                 g_key_file_free(keyfile);
70                 return NULL;
71         }
72
73         return keyfile;
74 }
75
76 static void parse_config(GKeyFile *config)
77 {
78         GError *error = NULL;
79         gboolean boolean;
80         char **timeservers;
81
82         if (config == NULL)
83                 return;
84
85         DBG("parsing main.conf");
86
87         boolean = g_key_file_get_boolean(config, "General",
88                                                 "BackgroundScanning", &error);
89         if (error == NULL)
90                 connman_settings.bg_scan = boolean;
91
92         g_clear_error(&error);
93
94         timeservers = g_key_file_get_string_list(config, "General",
95                                                 "FallbackTimeservers", NULL, &error);
96         if (error == NULL)
97                 connman_settings.pref_timeservers = timeservers;
98
99         g_clear_error(&error);
100 }
101
102 static GMainLoop *main_loop = NULL;
103
104 static unsigned int __terminated = 0;
105
106 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
107                                                         gpointer user_data)
108 {
109         struct signalfd_siginfo si;
110         ssize_t result;
111         int fd;
112
113         if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
114                 return FALSE;
115
116         fd = g_io_channel_unix_get_fd(channel);
117
118         result = read(fd, &si, sizeof(si));
119         if (result != sizeof(si))
120                 return FALSE;
121
122         switch (si.ssi_signo) {
123         case SIGINT:
124         case SIGTERM:
125                 if (__terminated == 0) {
126                         connman_info("Terminating");
127                         g_main_loop_quit(main_loop);
128                 }
129
130                 __terminated = 1;
131                 break;
132         }
133
134         return TRUE;
135 }
136
137 static guint setup_signalfd(void)
138 {
139         GIOChannel *channel;
140         guint source;
141         sigset_t mask;
142         int fd;
143
144         sigemptyset(&mask);
145         sigaddset(&mask, SIGINT);
146         sigaddset(&mask, SIGTERM);
147
148         if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
149                 perror("Failed to set signal mask");
150                 return 0;
151         }
152
153         fd = signalfd(-1, &mask, 0);
154         if (fd < 0) {
155                 perror("Failed to create signal descriptor");
156                 return 0;
157         }
158
159         channel = g_io_channel_unix_new(fd);
160
161         g_io_channel_set_close_on_unref(channel, TRUE);
162         g_io_channel_set_encoding(channel, NULL, NULL);
163         g_io_channel_set_buffered(channel, FALSE);
164
165         source = g_io_add_watch(channel,
166                                 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
167                                 signal_handler, NULL);
168
169         g_io_channel_unref(channel);
170
171         return source;
172 }
173
174 static void disconnect_callback(DBusConnection *conn, void *user_data)
175 {
176         connman_error("D-Bus disconnect");
177
178         g_main_loop_quit(main_loop);
179 }
180
181 static gchar *option_debug = NULL;
182 static gchar *option_device = NULL;
183 static gchar *option_plugin = NULL;
184 static gchar *option_nodevice = NULL;
185 static gchar *option_noplugin = NULL;
186 static gchar *option_wifi = NULL;
187 static gboolean option_detach = TRUE;
188 static gboolean option_dnsproxy = TRUE;
189 static gboolean option_compat = FALSE;
190 static gboolean option_version = FALSE;
191
192 static gboolean parse_debug(const char *key, const char *value,
193                                         gpointer user_data, GError **error)
194 {
195         if (value)
196                 option_debug = g_strdup(value);
197         else
198                 option_debug = g_strdup("*");
199
200         return TRUE;
201 }
202
203 static GOptionEntry options[] = {
204         { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
205                                 G_OPTION_ARG_CALLBACK, parse_debug,
206                                 "Specify debug options to enable", "DEBUG" },
207         { "device", 'i', 0, G_OPTION_ARG_STRING, &option_device,
208                         "Specify networking device or interface", "DEV" },
209         { "nodevice", 'I', 0, G_OPTION_ARG_STRING, &option_nodevice,
210                         "Specify networking interface to ignore", "DEV" },
211         { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
212                                 "Specify plugins to load", "NAME,..." },
213         { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
214                                 "Specify plugins not to load", "NAME,..." },
215         { "wifi", 'W', 0, G_OPTION_ARG_STRING, &option_wifi,
216                                 "Specify driver for WiFi/Supplicant", "NAME" },
217         { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
218                                 G_OPTION_ARG_NONE, &option_detach,
219                                 "Don't fork daemon to background" },
220         { "nodnsproxy", 'r', G_OPTION_FLAG_REVERSE,
221                                 G_OPTION_ARG_NONE, &option_dnsproxy,
222                                 "Don't enable DNS Proxy" },
223         { "compat", 'c', 0, G_OPTION_ARG_NONE, &option_compat,
224                                 "(obsolete)" },
225         { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
226                                 "Show version information and exit" },
227         { NULL },
228 };
229
230 const char *connman_option_get_string(const char *key)
231 {
232         if (g_strcmp0(key, "wifi") == 0) {
233                 if (option_wifi == NULL)
234                         return "nl80211,wext";
235                 else
236                         return option_wifi;
237         }
238
239         return NULL;
240 }
241
242 connman_bool_t connman_setting_get_bool(const char *key)
243 {
244         if (g_str_equal(key, "BackgroundScanning") == TRUE)
245                 return connman_settings.bg_scan;
246
247         return FALSE;
248 }
249
250 char **connman_setting_get_string_list(const char *key)
251 {
252         if (g_str_equal(key, "FallbackTimeservers") == TRUE)
253                 return connman_settings.pref_timeservers;
254
255         return NULL;
256 }
257
258 int main(int argc, char *argv[])
259 {
260         GOptionContext *context;
261         GError *error = NULL;
262         DBusConnection *conn;
263         DBusError err;
264         GKeyFile *config;
265         guint signal;
266
267 #ifdef HAVE_CAPNG
268         /* Drop capabilities */
269 #endif
270
271 #ifdef NEED_THREADS
272         if (g_thread_supported() == FALSE)
273                 g_thread_init(NULL);
274 #endif
275
276         context = g_option_context_new(NULL);
277         g_option_context_add_main_entries(context, options, NULL);
278
279         if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
280                 if (error != NULL) {
281                         g_printerr("%s\n", error->message);
282                         g_error_free(error);
283                 } else
284                         g_printerr("An unknown error occurred\n");
285                 exit(1);
286         }
287
288         g_option_context_free(context);
289
290         if (option_version == TRUE) {
291                 printf("%s\n", VERSION);
292                 exit(0);
293         }
294
295         if (option_detach == TRUE) {
296                 if (daemon(0, 0)) {
297                         perror("Can't start daemon");
298                         exit(1);
299                 }
300         }
301
302         if (mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
303                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
304                 if (errno != EEXIST)
305                         perror("Failed to create state directory");
306         }
307
308         if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
309                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
310                 if (errno != EEXIST)
311                         perror("Failed to create storage directory");
312         }
313
314         umask(0077);
315
316         main_loop = g_main_loop_new(NULL, FALSE);
317
318 #ifdef NEED_THREADS
319         if (dbus_threads_init_default() == FALSE) {
320                 fprintf(stderr, "Can't init usage of threads\n");
321                 exit(1);
322         }
323 #endif
324
325         signal = setup_signalfd();
326
327         dbus_error_init(&err);
328
329         conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
330         if (conn == NULL) {
331                 if (dbus_error_is_set(&err) == TRUE) {
332                         fprintf(stderr, "%s\n", err.message);
333                         dbus_error_free(&err);
334                 } else
335                         fprintf(stderr, "Can't register with system bus\n");
336                 exit(1);
337         }
338
339         g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
340
341         __connman_log_init(argv[0], option_debug, option_detach);
342
343         __connman_dbus_init(conn);
344
345         config = load_config(CONFIGDIR "/main.conf");
346
347         parse_config(config);
348
349         __connman_storage_migrate();
350         __connman_technology_init();
351         __connman_notifier_init();
352         __connman_service_init();
353         __connman_provider_init();
354         __connman_network_init();
355         __connman_device_init(option_device, option_nodevice);
356
357         __connman_agent_init();
358         __connman_ippool_init();
359         __connman_iptables_init();
360         __connman_nat_init();
361         __connman_tethering_init();
362         __connman_counter_init();
363         __connman_manager_init();
364         __connman_config_init();
365         __connman_stats_init();
366         __connman_clock_init();
367
368         __connman_resolver_init(option_dnsproxy);
369         __connman_ipconfig_init();
370         __connman_rtnl_init();
371         __connman_task_init();
372         __connman_proxy_init();
373         __connman_detect_init();
374         __connman_session_init();
375         __connman_timeserver_init();
376         __connman_connection_init();
377
378         __connman_plugin_init(option_plugin, option_noplugin);
379
380         __connman_rtnl_start();
381         __connman_dhcp_init();
382         __connman_dhcpv6_init();
383         __connman_wpad_init();
384         __connman_wispr_init();
385         __connman_rfkill_init();
386
387         g_free(option_device);
388         g_free(option_plugin);
389         g_free(option_nodevice);
390         g_free(option_noplugin);
391
392         g_main_loop_run(main_loop);
393
394         g_source_remove(signal);
395
396         __connman_rfkill_cleanup();
397         __connman_wispr_cleanup();
398         __connman_wpad_cleanup();
399         __connman_dhcpv6_cleanup();
400         __connman_dhcp_cleanup();
401         __connman_provider_cleanup();
402         __connman_plugin_cleanup();
403         __connman_connection_cleanup();
404         __connman_timeserver_cleanup();
405         __connman_session_cleanup();
406         __connman_detect_cleanup();
407         __connman_proxy_cleanup();
408         __connman_task_cleanup();
409         __connman_rtnl_cleanup();
410         __connman_resolver_cleanup();
411
412         __connman_clock_cleanup();
413         __connman_stats_cleanup();
414         __connman_config_cleanup();
415         __connman_manager_cleanup();
416         __connman_counter_cleanup();
417         __connman_agent_cleanup();
418         __connman_tethering_cleanup();
419         __connman_nat_cleanup();
420         __connman_iptables_cleanup();
421         __connman_ippool_cleanup();
422         __connman_device_cleanup();
423         __connman_network_cleanup();
424         __connman_service_cleanup();
425         __connman_ipconfig_cleanup();
426         __connman_notifier_cleanup();
427         __connman_technology_cleanup();
428
429         __connman_dbus_cleanup();
430
431         __connman_log_cleanup();
432
433         dbus_connection_unref(conn);
434
435         g_main_loop_unref(main_loop);
436
437         if (config)
438                 g_key_file_free(config);
439
440         if (connman_settings.pref_timeservers != NULL)
441                 g_strfreev(connman_settings.pref_timeservers);
442
443         g_free(option_debug);
444
445         return 0;
446 }