main: Simplify fallback nameserver parsing
[platform/upstream/connman.git] / src / main.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  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 #include <netdb.h>
37
38 #include <gdbus.h>
39
40 #include "connman.h"
41
42 #define DEFAULT_INPUT_REQUEST_TIMEOUT 120 * 1000
43 #define DEFAULT_BROWSER_LAUNCH_TIMEOUT 300 * 1000
44
45 static struct {
46         connman_bool_t bg_scan;
47         char **pref_timeservers;
48         unsigned int *auto_connect;
49         unsigned int *preferred_techs;
50         char **fallback_nameservers;
51         unsigned int timeout_inputreq;
52         unsigned int timeout_browserlaunch;
53 } connman_settings  = {
54         .bg_scan = TRUE,
55         .pref_timeservers = NULL,
56         .auto_connect = NULL,
57         .preferred_techs = NULL,
58         .fallback_nameservers = NULL,
59         .timeout_inputreq = DEFAULT_INPUT_REQUEST_TIMEOUT,
60         .timeout_browserlaunch = DEFAULT_BROWSER_LAUNCH_TIMEOUT,
61 };
62
63 static GKeyFile *load_config(const char *file)
64 {
65         GError *err = NULL;
66         GKeyFile *keyfile;
67
68         keyfile = g_key_file_new();
69
70         g_key_file_set_list_separator(keyfile, ',');
71
72         if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
73                 if (err->code != G_FILE_ERROR_NOENT) {
74                         connman_error("Parsing %s failed: %s", file,
75                                                                 err->message);
76                 }
77
78                 g_error_free(err);
79                 g_key_file_free(keyfile);
80                 return NULL;
81         }
82
83         return keyfile;
84 }
85
86 static uint *parse_service_types(char **str_list, gsize len)
87 {
88         unsigned int *type_list;
89         int i, j;
90         enum connman_service_type type;
91
92         type_list = g_try_new0(unsigned int, len + 1);
93         if (type_list == NULL)
94                 return NULL;
95
96         i = 0;
97         j = 0;
98         while (str_list[i] != NULL)
99         {
100                 type = __connman_service_string2type(str_list[i]);
101
102                 if (type != CONNMAN_SERVICE_TYPE_UNKNOWN) {
103                         type_list[j] = type;
104                         j += 1;
105                 }
106                 i += 1;
107         }
108
109         return type_list;
110 }
111
112 static char **parse_fallback_nameservers(char **nameservers, gsize len)
113 {
114         char **servers;
115         int i, j;
116
117         servers = g_try_new0(char *, len + 1);
118         if (servers == NULL)
119                 return NULL;
120
121         i = 0;
122         j = 0;
123         while (nameservers[i] != NULL) {
124                 if (connman_inet_check_ipaddress(nameservers[i]) > 0) {
125                         servers[j] = g_strdup(nameservers[i]);
126                         j += 1;
127                 }
128                 i += 1;
129         }
130
131         return servers;
132 }
133
134 static void parse_config(GKeyFile *config)
135 {
136         GError *error = NULL;
137         gboolean boolean;
138         char **timeservers;
139         char **str_list;
140         gsize len;
141         char *default_auto_connect[] = {
142                 "wifi",
143                 "ethernet",
144                 "cellular",
145                 NULL
146         };
147         int timeout;
148
149         if (config == NULL) {
150                 connman_settings.auto_connect =
151                         parse_service_types(default_auto_connect, 3);
152                 return;
153         }
154
155         DBG("parsing main.conf");
156
157         boolean = g_key_file_get_boolean(config, "General",
158                                                 "BackgroundScanning", &error);
159         if (error == NULL)
160                 connman_settings.bg_scan = boolean;
161
162         g_clear_error(&error);
163
164         timeservers = g_key_file_get_string_list(config, "General",
165                                                 "FallbackTimeservers", NULL, &error);
166         if (error == NULL)
167                 connman_settings.pref_timeservers = timeservers;
168
169         g_clear_error(&error);
170
171         str_list = g_key_file_get_string_list(config, "General",
172                         "DefaultAutoConnectTechnologies", &len, &error);
173
174         if (error == NULL)
175                 connman_settings.auto_connect =
176                         parse_service_types(str_list, len);
177         else
178                 connman_settings.auto_connect =
179                         parse_service_types(default_auto_connect, 3);
180
181         g_strfreev(str_list);
182
183         g_clear_error(&error);
184
185         str_list = g_key_file_get_string_list(config, "General",
186                         "PreferredTechnologies", &len, &error);
187
188         if (error == NULL)
189                 connman_settings.preferred_techs =
190                         parse_service_types(str_list, len);
191
192         g_strfreev(str_list);
193
194         g_clear_error(&error);
195
196         str_list = g_key_file_get_string_list(config, "General",
197                         "FallbackNameservers", &len, &error);
198
199         if (error == NULL)
200                 connman_settings.fallback_nameservers =
201                         parse_fallback_nameservers(str_list, len);
202
203         g_strfreev(str_list);
204
205         g_clear_error(&error);
206
207         timeout = g_key_file_get_integer(config, "General",
208                         "InputRequestTimeout", &error);
209         if (error == NULL && timeout >= 0)
210                 connman_settings.timeout_inputreq = timeout * 1000;
211
212         g_clear_error(&error);
213
214         timeout = g_key_file_get_integer(config, "General",
215                         "BrowserLaunchTimeout", &error);
216         if (error == NULL && timeout >= 0)
217                 connman_settings.timeout_browserlaunch = timeout * 1000;
218
219         g_clear_error(&error);
220 }
221
222 static GMainLoop *main_loop = NULL;
223
224 static unsigned int __terminated = 0;
225
226 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
227                                                         gpointer user_data)
228 {
229         struct signalfd_siginfo si;
230         ssize_t result;
231         int fd;
232
233         if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
234                 return FALSE;
235
236         fd = g_io_channel_unix_get_fd(channel);
237
238         result = read(fd, &si, sizeof(si));
239         if (result != sizeof(si))
240                 return FALSE;
241
242         switch (si.ssi_signo) {
243         case SIGINT:
244         case SIGTERM:
245                 if (__terminated == 0) {
246                         connman_info("Terminating");
247                         g_main_loop_quit(main_loop);
248                 }
249
250                 __terminated = 1;
251                 break;
252         }
253
254         return TRUE;
255 }
256
257 static guint setup_signalfd(void)
258 {
259         GIOChannel *channel;
260         guint source;
261         sigset_t mask;
262         int fd;
263
264         sigemptyset(&mask);
265         sigaddset(&mask, SIGINT);
266         sigaddset(&mask, SIGTERM);
267
268         if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
269                 perror("Failed to set signal mask");
270                 return 0;
271         }
272
273         fd = signalfd(-1, &mask, 0);
274         if (fd < 0) {
275                 perror("Failed to create signal descriptor");
276                 return 0;
277         }
278
279         channel = g_io_channel_unix_new(fd);
280
281         g_io_channel_set_close_on_unref(channel, TRUE);
282         g_io_channel_set_encoding(channel, NULL, NULL);
283         g_io_channel_set_buffered(channel, FALSE);
284
285         source = g_io_add_watch(channel,
286                                 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
287                                 signal_handler, NULL);
288
289         g_io_channel_unref(channel);
290
291         return source;
292 }
293
294 static void disconnect_callback(DBusConnection *conn, void *user_data)
295 {
296         connman_error("D-Bus disconnect");
297
298         g_main_loop_quit(main_loop);
299 }
300
301 static gchar *option_debug = NULL;
302 static gchar *option_device = NULL;
303 static gchar *option_plugin = NULL;
304 static gchar *option_nodevice = NULL;
305 static gchar *option_noplugin = NULL;
306 static gchar *option_wifi = NULL;
307 static gboolean option_detach = TRUE;
308 static gboolean option_dnsproxy = TRUE;
309 static gboolean option_compat = FALSE;
310 static gboolean option_version = FALSE;
311
312 static gboolean parse_debug(const char *key, const char *value,
313                                         gpointer user_data, GError **error)
314 {
315         if (value)
316                 option_debug = g_strdup(value);
317         else
318                 option_debug = g_strdup("*");
319
320         return TRUE;
321 }
322
323 static GOptionEntry options[] = {
324         { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
325                                 G_OPTION_ARG_CALLBACK, parse_debug,
326                                 "Specify debug options to enable", "DEBUG" },
327         { "device", 'i', 0, G_OPTION_ARG_STRING, &option_device,
328                         "Specify networking device or interface", "DEV" },
329         { "nodevice", 'I', 0, G_OPTION_ARG_STRING, &option_nodevice,
330                         "Specify networking interface to ignore", "DEV" },
331         { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
332                                 "Specify plugins to load", "NAME,..." },
333         { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
334                                 "Specify plugins not to load", "NAME,..." },
335         { "wifi", 'W', 0, G_OPTION_ARG_STRING, &option_wifi,
336                                 "Specify driver for WiFi/Supplicant", "NAME" },
337         { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
338                                 G_OPTION_ARG_NONE, &option_detach,
339                                 "Don't fork daemon to background" },
340         { "nodnsproxy", 'r', G_OPTION_FLAG_REVERSE,
341                                 G_OPTION_ARG_NONE, &option_dnsproxy,
342                                 "Don't enable DNS Proxy" },
343         { "compat", 'c', 0, G_OPTION_ARG_NONE, &option_compat,
344                                 "(obsolete)" },
345         { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
346                                 "Show version information and exit" },
347         { NULL },
348 };
349
350 const char *connman_option_get_string(const char *key)
351 {
352         if (g_strcmp0(key, "wifi") == 0) {
353                 if (option_wifi == NULL)
354                         return "nl80211,wext";
355                 else
356                         return option_wifi;
357         }
358
359         return NULL;
360 }
361
362 connman_bool_t connman_setting_get_bool(const char *key)
363 {
364         if (g_str_equal(key, "BackgroundScanning") == TRUE)
365                 return connman_settings.bg_scan;
366
367         return FALSE;
368 }
369
370 char **connman_setting_get_string_list(const char *key)
371 {
372         if (g_str_equal(key, "FallbackTimeservers") == TRUE)
373                 return connman_settings.pref_timeservers;
374
375         if (g_str_equal(key, "FallbackNameservers") == TRUE)
376                 return connman_settings.fallback_nameservers;
377
378         return NULL;
379 }
380
381 unsigned int *connman_setting_get_uint_list(const char *key)
382 {
383         if (g_str_equal(key, "DefaultAutoConnectTechnologies") == TRUE)
384                 return connman_settings.auto_connect;
385
386         if (g_str_equal(key, "PreferredTechnologies") == TRUE)
387                 return connman_settings.preferred_techs;
388
389         return NULL;
390 }
391
392 unsigned int connman_timeout_input_request(void) {
393         return connman_settings.timeout_inputreq;
394 }
395
396 unsigned int connman_timeout_browser_launch(void) {
397         return connman_settings.timeout_browserlaunch;
398 }
399
400 int main(int argc, char *argv[])
401 {
402         GOptionContext *context;
403         GError *error = NULL;
404         DBusConnection *conn;
405         DBusError err;
406         GKeyFile *config;
407         guint signal;
408
409 #ifdef NEED_THREADS
410         if (g_thread_supported() == FALSE)
411                 g_thread_init(NULL);
412 #endif
413
414         context = g_option_context_new(NULL);
415         g_option_context_add_main_entries(context, options, NULL);
416
417         if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
418                 if (error != NULL) {
419                         g_printerr("%s\n", error->message);
420                         g_error_free(error);
421                 } else
422                         g_printerr("An unknown error occurred\n");
423                 exit(1);
424         }
425
426         g_option_context_free(context);
427
428         if (option_version == TRUE) {
429                 printf("%s\n", VERSION);
430                 exit(0);
431         }
432
433         if (option_detach == TRUE) {
434                 if (daemon(0, 0)) {
435                         perror("Can't start daemon");
436                         exit(1);
437                 }
438         }
439
440         if (mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
441                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
442                 if (errno != EEXIST)
443                         perror("Failed to create state directory");
444         }
445
446         if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
447                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
448                 if (errno != EEXIST)
449                         perror("Failed to create storage directory");
450         }
451
452         umask(0077);
453
454         main_loop = g_main_loop_new(NULL, FALSE);
455
456 #ifdef NEED_THREADS
457         if (dbus_threads_init_default() == FALSE) {
458                 fprintf(stderr, "Can't init usage of threads\n");
459                 exit(1);
460         }
461 #endif
462
463         signal = setup_signalfd();
464
465         dbus_error_init(&err);
466
467         conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
468         if (conn == NULL) {
469                 if (dbus_error_is_set(&err) == TRUE) {
470                         fprintf(stderr, "%s\n", err.message);
471                         dbus_error_free(&err);
472                 } else
473                         fprintf(stderr, "Can't register with system bus\n");
474                 exit(1);
475         }
476
477         g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
478
479         __connman_log_init(argv[0], option_debug, option_detach);
480
481         __connman_dbus_init(conn);
482
483         config = load_config(CONFIGDIR "/main.conf");
484         parse_config(config);
485         if (config != NULL)
486                 g_key_file_free(config);
487
488         __connman_storage_migrate();
489         __connman_technology_init();
490         __connman_notifier_init();
491         __connman_service_init();
492         __connman_provider_init();
493         __connman_network_init();
494         __connman_device_init(option_device, option_nodevice);
495
496         __connman_agent_init();
497         __connman_ippool_init();
498         __connman_iptables_init();
499         __connman_nat_init();
500         __connman_tethering_init();
501         __connman_counter_init();
502         __connman_manager_init();
503         __connman_config_init();
504         __connman_stats_init();
505         __connman_clock_init();
506
507         __connman_resolver_init(option_dnsproxy);
508         __connman_ipconfig_init();
509         __connman_rtnl_init();
510         __connman_task_init();
511         __connman_proxy_init();
512         __connman_detect_init();
513         __connman_session_init();
514         __connman_timeserver_init();
515         __connman_connection_init();
516
517         __connman_plugin_init(option_plugin, option_noplugin);
518
519         __connman_rtnl_start();
520         __connman_dhcp_init();
521         __connman_dhcpv6_init();
522         __connman_wpad_init();
523         __connman_wispr_init();
524         __connman_rfkill_init();
525
526         g_free(option_device);
527         g_free(option_plugin);
528         g_free(option_nodevice);
529         g_free(option_noplugin);
530
531         g_main_loop_run(main_loop);
532
533         g_source_remove(signal);
534
535         __connman_rfkill_cleanup();
536         __connman_wispr_cleanup();
537         __connman_wpad_cleanup();
538         __connman_dhcpv6_cleanup();
539         __connman_dhcp_cleanup();
540         __connman_provider_cleanup();
541         __connman_plugin_cleanup();
542         __connman_connection_cleanup();
543         __connman_timeserver_cleanup();
544         __connman_session_cleanup();
545         __connman_detect_cleanup();
546         __connman_proxy_cleanup();
547         __connman_task_cleanup();
548         __connman_rtnl_cleanup();
549         __connman_resolver_cleanup();
550
551         __connman_clock_cleanup();
552         __connman_stats_cleanup();
553         __connman_config_cleanup();
554         __connman_manager_cleanup();
555         __connman_counter_cleanup();
556         __connman_agent_cleanup();
557         __connman_tethering_cleanup();
558         __connman_nat_cleanup();
559         __connman_iptables_cleanup();
560         __connman_ippool_cleanup();
561         __connman_device_cleanup();
562         __connman_network_cleanup();
563         __connman_service_cleanup();
564         __connman_ipconfig_cleanup();
565         __connman_notifier_cleanup();
566         __connman_technology_cleanup();
567
568         __connman_dbus_cleanup();
569
570         __connman_log_cleanup();
571
572         dbus_connection_unref(conn);
573
574         g_main_loop_unref(main_loop);
575
576         if (connman_settings.pref_timeservers != NULL)
577                 g_strfreev(connman_settings.pref_timeservers);
578
579         g_free(connman_settings.auto_connect);
580         g_free(connman_settings.preferred_techs);
581         g_strfreev(connman_settings.fallback_nameservers);
582
583         g_free(option_debug);
584
585         return 0;
586 }