Imported Upstream version 1.37
[platform/upstream/connman.git] / src / main.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2013  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 #include <sys/time.h>
38 #include <sys/resource.h>
39
40 #include <gdbus.h>
41
42 #include "connman.h"
43
44 #define CONF_ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]) - 1)
45
46 #define DEFAULT_INPUT_REQUEST_TIMEOUT (120 * 1000)
47 #define DEFAULT_BROWSER_LAUNCH_TIMEOUT (300 * 1000)
48
49 #define MAINFILE "main.conf"
50 #define CONFIGMAINFILE CONFIGDIR "/" MAINFILE
51
52 static char *default_auto_connect[] = {
53         "wifi",
54         "ethernet",
55         "cellular",
56         NULL
57 };
58
59 static char *default_favorite_techs[] = {
60         "ethernet",
61         NULL
62 };
63
64 static char *default_blacklist[] = {
65         "vmnet",
66         "vboxnet",
67         "virbr",
68         "ifb",
69         "ve-",
70         "vb-",
71         NULL
72 };
73
74 static struct {
75         bool bg_scan;
76         char **pref_timeservers;
77         unsigned int *auto_connect;
78         unsigned int *favorite_techs;
79         unsigned int *preferred_techs;
80         unsigned int *always_connected_techs;
81         char **fallback_nameservers;
82         unsigned int timeout_inputreq;
83         unsigned int timeout_browserlaunch;
84         char **blacklisted_interfaces;
85         bool allow_hostname_updates;
86         bool allow_domainname_updates;
87         bool single_tech;
88         char **tethering_technologies;
89         bool persistent_tethering_mode;
90         bool enable_6to4;
91         char *vendor_class_id;
92         bool enable_online_check;
93         bool auto_connect_roaming_services;
94         bool acd;
95         bool use_gateways_as_timeservers;
96 #if defined TIZEN_EXT
97         char **cellular_interfaces;
98         bool tizen_tv_extension;
99 #endif
100 } connman_settings  = {
101         .bg_scan = true,
102         .pref_timeservers = NULL,
103         .auto_connect = NULL,
104         .favorite_techs = NULL,
105         .preferred_techs = NULL,
106         .always_connected_techs = NULL,
107         .fallback_nameservers = NULL,
108         .timeout_inputreq = DEFAULT_INPUT_REQUEST_TIMEOUT,
109         .timeout_browserlaunch = DEFAULT_BROWSER_LAUNCH_TIMEOUT,
110         .blacklisted_interfaces = NULL,
111         .allow_hostname_updates = true,
112         .allow_domainname_updates = true,
113         .single_tech = false,
114         .tethering_technologies = NULL,
115         .persistent_tethering_mode = false,
116         .enable_6to4 = false,
117         .vendor_class_id = NULL,
118         .enable_online_check = true,
119         .auto_connect_roaming_services = false,
120         .acd = false,
121         .use_gateways_as_timeservers = false,
122 #if defined TIZEN_EXT
123         .cellular_interfaces = NULL,
124         .tizen_tv_extension = false,
125 #endif
126 };
127
128 #define CONF_BG_SCAN                    "BackgroundScanning"
129 #define CONF_PREF_TIMESERVERS           "FallbackTimeservers"
130 #define CONF_AUTO_CONNECT_TECHS         "DefaultAutoConnectTechnologies"
131 #define CONF_FAVORITE_TECHS             "DefaultFavoriteTechnologies"
132 #define CONF_ALWAYS_CONNECTED_TECHS     "AlwaysConnectedTechnologies"
133 #define CONF_PREFERRED_TECHS            "PreferredTechnologies"
134 #define CONF_FALLBACK_NAMESERVERS       "FallbackNameservers"
135 #define CONF_TIMEOUT_INPUTREQ           "InputRequestTimeout"
136 #define CONF_TIMEOUT_BROWSERLAUNCH      "BrowserLaunchTimeout"
137 #define CONF_BLACKLISTED_INTERFACES     "NetworkInterfaceBlacklist"
138 #define CONF_ALLOW_HOSTNAME_UPDATES     "AllowHostnameUpdates"
139 #define CONF_ALLOW_DOMAINNAME_UPDATES   "AllowDomainnameUpdates"
140 #define CONF_SINGLE_TECH                "SingleConnectedTechnology"
141 #define CONF_TETHERING_TECHNOLOGIES      "TetheringTechnologies"
142 #define CONF_PERSISTENT_TETHERING_MODE  "PersistentTetheringMode"
143 #define CONF_ENABLE_6TO4                "Enable6to4"
144 #define CONF_VENDOR_CLASS_ID            "VendorClassID"
145 #define CONF_ENABLE_ONLINE_CHECK        "EnableOnlineCheck"
146 #define CONF_AUTO_CONNECT_ROAMING_SERVICES "AutoConnectRoamingServices"
147 #define CONF_ACD                        "AddressConflictDetection"
148 #define CONF_USE_GATEWAYS_AS_TIMESERVERS "UseGatewaysAsTimeservers"
149 #if defined TIZEN_EXT
150 #define CONF_CELLULAR_INTERFACE         "NetworkCellularInterfaceList"
151 #define CONF_TIZEN_TV_EXT               "TizenTVExtension"
152 #endif
153
154 static const char *supported_options[] = {
155         CONF_BG_SCAN,
156         CONF_PREF_TIMESERVERS,
157         CONF_AUTO_CONNECT_TECHS,
158         CONF_ALWAYS_CONNECTED_TECHS,
159         CONF_PREFERRED_TECHS,
160         CONF_FALLBACK_NAMESERVERS,
161         CONF_TIMEOUT_INPUTREQ,
162         CONF_TIMEOUT_BROWSERLAUNCH,
163         CONF_BLACKLISTED_INTERFACES,
164         CONF_ALLOW_HOSTNAME_UPDATES,
165         CONF_ALLOW_DOMAINNAME_UPDATES,
166         CONF_SINGLE_TECH,
167         CONF_TETHERING_TECHNOLOGIES,
168         CONF_PERSISTENT_TETHERING_MODE,
169         CONF_ENABLE_6TO4,
170         CONF_VENDOR_CLASS_ID,
171         CONF_ENABLE_ONLINE_CHECK,
172         CONF_AUTO_CONNECT_ROAMING_SERVICES,
173         CONF_ACD,
174         CONF_USE_GATEWAYS_AS_TIMESERVERS,
175 #if defined TIZEN_EXT
176         CONF_CELLULAR_INTERFACE,
177         CONF_TIZEN_TV_EXT,
178 #endif
179         NULL
180 };
181
182 static GKeyFile *load_config(const char *file)
183 {
184         GError *err = NULL;
185         GKeyFile *keyfile;
186
187         keyfile = g_key_file_new();
188
189         g_key_file_set_list_separator(keyfile, ',');
190
191         if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
192                 if (err->code != G_FILE_ERROR_NOENT) {
193                         connman_error("Parsing %s failed: %s", file,
194                                                                 err->message);
195                 }
196
197                 g_error_free(err);
198                 g_key_file_free(keyfile);
199                 return NULL;
200         }
201
202         return keyfile;
203 }
204
205 static uint *parse_service_types(char **str_list, gsize len)
206 {
207         unsigned int *type_list;
208         int i, j;
209         enum connman_service_type type;
210
211         type_list = g_try_new0(unsigned int, len + 1);
212         if (!type_list)
213                 return NULL;
214
215         i = 0;
216         j = 0;
217         while (str_list[i]) {
218                 type = __connman_service_string2type(str_list[i]);
219
220                 if (type != CONNMAN_SERVICE_TYPE_UNKNOWN) {
221                         type_list[j] = type;
222                         j += 1;
223                 }
224                 i += 1;
225         }
226
227         type_list[j] = CONNMAN_SERVICE_TYPE_UNKNOWN;
228
229         return type_list;
230 }
231
232 static char **parse_fallback_nameservers(char **nameservers, gsize len)
233 {
234         char **servers;
235         int i, j;
236
237         servers = g_try_new0(char *, len + 1);
238         if (!servers)
239                 return NULL;
240
241         i = 0;
242         j = 0;
243         while (nameservers[i]) {
244                 if (connman_inet_check_ipaddress(nameservers[i]) > 0) {
245                         servers[j] = g_strdup(nameservers[i]);
246                         j += 1;
247                 }
248                 i += 1;
249         }
250
251         return servers;
252 }
253
254 static void check_config(GKeyFile *config)
255 {
256         char **keys;
257         int j;
258
259         if (!config)
260                 return;
261
262         keys = g_key_file_get_groups(config, NULL);
263
264         for (j = 0; keys && keys[j]; j++) {
265                 if (g_strcmp0(keys[j], "General") != 0)
266                         connman_warn("Unknown group %s in %s",
267                                                 keys[j], MAINFILE);
268         }
269
270         g_strfreev(keys);
271
272         keys = g_key_file_get_keys(config, "General", NULL, NULL);
273
274         for (j = 0; keys && keys[j]; j++) {
275                 bool found;
276                 int i;
277
278                 found = false;
279                 for (i = 0; supported_options[i]; i++) {
280                         if (g_strcmp0(keys[j], supported_options[i]) == 0) {
281                                 found = true;
282                                 break;
283                         }
284                 }
285                 if (!found && !supported_options[i])
286                         connman_warn("Unknown option %s in %s",
287                                                 keys[j], MAINFILE);
288         }
289
290         g_strfreev(keys);
291 }
292
293 #if defined TIZEN_EXT
294 static void check_Tizen_configuration(GKeyFile *config)
295 {
296         GError *error = NULL;
297         char **cellular_interfaces;
298         bool boolean;
299         gsize len;
300
301         cellular_interfaces = g_key_file_get_string_list(config, "General",
302                         CONF_CELLULAR_INTERFACE, &len, &error);
303
304         if (error == NULL)
305                 connman_settings.cellular_interfaces = cellular_interfaces;
306
307         g_clear_error(&error);
308
309         boolean = __connman_config_get_bool(config, "General",
310                         CONF_TIZEN_TV_EXT, &error);
311         if (!error)
312                 connman_settings.tizen_tv_extension = boolean;
313
314         g_clear_error(&error);
315 }
316
317 static void set_nofile_inc(void)
318 {
319         int err;
320         struct rlimit rlim;
321
322         rlim.rlim_cur = 8192;
323         rlim.rlim_max = 8192;
324
325         err = setrlimit(RLIMIT_NOFILE, &rlim);
326         if (err)
327                 DBG("fail to increase FILENO err(%d)", err);
328
329         return;
330 }
331 #endif
332
333 static void parse_config(GKeyFile *config)
334 {
335         GError *error = NULL;
336         bool boolean;
337         char **timeservers;
338         char **interfaces;
339         char **str_list;
340         char **tethering;
341         char *vendor_class_id;
342         gsize len;
343         int timeout;
344
345         if (!config) {
346                 connman_settings.auto_connect =
347                         parse_service_types(default_auto_connect, CONF_ARRAY_SIZE(default_auto_connect));
348                 connman_settings.favorite_techs =
349                         parse_service_types(default_favorite_techs, CONF_ARRAY_SIZE(default_favorite_techs));
350                 connman_settings.blacklisted_interfaces =
351                         g_strdupv(default_blacklist);
352                 return;
353         }
354
355         DBG("parsing %s", MAINFILE);
356
357         boolean = g_key_file_get_boolean(config, "General",
358                                                 CONF_BG_SCAN, &error);
359         if (!error)
360                 connman_settings.bg_scan = boolean;
361
362         g_clear_error(&error);
363
364         timeservers = __connman_config_get_string_list(config, "General",
365                                         CONF_PREF_TIMESERVERS, NULL, &error);
366         if (!error)
367                 connman_settings.pref_timeservers = timeservers;
368
369         g_clear_error(&error);
370
371         str_list = __connman_config_get_string_list(config, "General",
372                         CONF_AUTO_CONNECT_TECHS, &len, &error);
373
374         if (!error)
375                 connman_settings.auto_connect =
376                         parse_service_types(str_list, len);
377         else
378                 connman_settings.auto_connect =
379                         parse_service_types(default_auto_connect, CONF_ARRAY_SIZE(default_auto_connect));
380
381         g_clear_error(&error);
382
383         str_list = __connman_config_get_string_list(config, "General",
384                         CONF_FAVORITE_TECHS, &len, &error);
385
386         if (!error)
387                 connman_settings.favorite_techs =
388                         parse_service_types(str_list, len);
389         else
390                 connman_settings.favorite_techs =
391                         parse_service_types(default_favorite_techs, CONF_ARRAY_SIZE(default_favorite_techs));
392
393         g_strfreev(str_list);
394
395         g_clear_error(&error);
396
397         str_list = __connman_config_get_string_list(config, "General",
398                         CONF_PREFERRED_TECHS, &len, &error);
399
400         if (!error)
401                 connman_settings.preferred_techs =
402                         parse_service_types(str_list, len);
403
404         g_strfreev(str_list);
405
406         g_clear_error(&error);
407
408         str_list = __connman_config_get_string_list(config, "General",
409                         CONF_ALWAYS_CONNECTED_TECHS, &len, &error);
410
411         if (!error)
412                 connman_settings.always_connected_techs =
413                         parse_service_types(str_list, len);
414
415         g_strfreev(str_list);
416
417         g_clear_error(&error);
418
419         str_list = __connman_config_get_string_list(config, "General",
420                         CONF_FALLBACK_NAMESERVERS, &len, &error);
421
422         if (!error)
423                 connman_settings.fallback_nameservers =
424                         parse_fallback_nameservers(str_list, len);
425
426         g_strfreev(str_list);
427
428         g_clear_error(&error);
429
430         timeout = g_key_file_get_integer(config, "General",
431                         CONF_TIMEOUT_INPUTREQ, &error);
432         if (!error && timeout >= 0)
433                 connman_settings.timeout_inputreq = timeout * 1000;
434
435         g_clear_error(&error);
436
437         timeout = g_key_file_get_integer(config, "General",
438                         CONF_TIMEOUT_BROWSERLAUNCH, &error);
439         if (!error && timeout >= 0)
440                 connman_settings.timeout_browserlaunch = timeout * 1000;
441
442         g_clear_error(&error);
443
444         interfaces = __connman_config_get_string_list(config, "General",
445                         CONF_BLACKLISTED_INTERFACES, &len, &error);
446
447         if (!error)
448                 connman_settings.blacklisted_interfaces = interfaces;
449         else
450                 connman_settings.blacklisted_interfaces =
451                         g_strdupv(default_blacklist);
452
453         g_clear_error(&error);
454
455         boolean = __connman_config_get_bool(config, "General",
456                                         CONF_ALLOW_HOSTNAME_UPDATES,
457                                         &error);
458         if (!error)
459                 connman_settings.allow_hostname_updates = boolean;
460
461         g_clear_error(&error);
462
463         boolean = __connman_config_get_bool(config, "General",
464                                         CONF_ALLOW_DOMAINNAME_UPDATES,
465                                         &error);
466         if (!error)
467                 connman_settings.allow_domainname_updates = boolean;
468
469         g_clear_error(&error);
470
471         boolean = __connman_config_get_bool(config, "General",
472                         CONF_SINGLE_TECH, &error);
473         if (!error)
474                 connman_settings.single_tech = boolean;
475
476         g_clear_error(&error);
477
478         tethering = __connman_config_get_string_list(config, "General",
479                         CONF_TETHERING_TECHNOLOGIES, &len, &error);
480
481         if (!error)
482                 connman_settings.tethering_technologies = tethering;
483
484         g_clear_error(&error);
485
486         boolean = __connman_config_get_bool(config, "General",
487                                         CONF_PERSISTENT_TETHERING_MODE,
488                                         &error);
489         if (!error)
490                 connman_settings.persistent_tethering_mode = boolean;
491
492         g_clear_error(&error);
493
494         boolean = __connman_config_get_bool(config, "General",
495                                         CONF_ENABLE_6TO4, &error);
496         if (!error)
497                 connman_settings.enable_6to4 = boolean;
498
499         g_clear_error(&error);
500
501         vendor_class_id = __connman_config_get_string(config, "General",
502                                         CONF_VENDOR_CLASS_ID, &error);
503         if (!error)
504                 connman_settings.vendor_class_id = vendor_class_id;
505
506         g_clear_error(&error);
507
508         boolean = __connman_config_get_bool(config, "General",
509                                         CONF_ENABLE_ONLINE_CHECK, &error);
510         if (!error) {
511                 connman_settings.enable_online_check = boolean;
512                 if (!boolean)
513                         connman_info("Online check disabled by main config.");
514         }
515
516         g_clear_error(&error);
517
518         boolean = __connman_config_get_bool(config, "General",
519                                 CONF_AUTO_CONNECT_ROAMING_SERVICES, &error);
520         if (!error)
521                 connman_settings.auto_connect_roaming_services = boolean;
522
523         g_clear_error(&error);
524
525         boolean = __connman_config_get_bool(config, "General", CONF_ACD, &error);
526         if (!error)
527                 connman_settings.acd = boolean;
528
529         g_clear_error(&error);
530
531         boolean = __connman_config_get_bool(config, "General",
532                                 CONF_USE_GATEWAYS_AS_TIMESERVERS, &error);
533         if (!error)
534                 connman_settings.use_gateways_as_timeservers = boolean;
535
536         g_clear_error(&error);
537
538 #if defined TIZEN_EXT
539         check_Tizen_configuration(config);
540 #endif
541 }
542
543 static int config_init(const char *file)
544 {
545         GKeyFile *config;
546
547 #if defined TIZEN_EXT
548         set_nofile_inc();
549 #endif
550         config = load_config(file);
551         check_config(config);
552         parse_config(config);
553         if (config)
554                 g_key_file_free(config);
555
556         return 0;
557 }
558
559 static GMainLoop *main_loop = NULL;
560
561 static unsigned int __terminated = 0;
562
563 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
564                                                         gpointer user_data)
565 {
566         struct signalfd_siginfo si;
567         ssize_t result;
568         int fd;
569
570         if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
571                 return FALSE;
572
573         fd = g_io_channel_unix_get_fd(channel);
574
575         result = read(fd, &si, sizeof(si));
576         if (result != sizeof(si))
577                 return FALSE;
578
579         switch (si.ssi_signo) {
580         case SIGINT:
581         case SIGTERM:
582                 if (__terminated == 0) {
583                         connman_info("Terminating");
584                         g_main_loop_quit(main_loop);
585                 }
586
587                 __terminated = 1;
588                 break;
589         }
590
591         return TRUE;
592 }
593
594 static guint setup_signalfd(void)
595 {
596         GIOChannel *channel;
597         guint source;
598         sigset_t mask;
599         int fd;
600
601         sigemptyset(&mask);
602         sigaddset(&mask, SIGINT);
603         sigaddset(&mask, SIGTERM);
604
605         if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
606                 perror("Failed to set signal mask");
607                 return 0;
608         }
609
610         fd = signalfd(-1, &mask, 0);
611         if (fd < 0) {
612                 perror("Failed to create signal descriptor");
613                 return 0;
614         }
615
616         channel = g_io_channel_unix_new(fd);
617
618         g_io_channel_set_close_on_unref(channel, TRUE);
619         g_io_channel_set_encoding(channel, NULL, NULL);
620         g_io_channel_set_buffered(channel, FALSE);
621
622         source = g_io_add_watch(channel,
623                                 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
624                                 signal_handler, NULL);
625
626         g_io_channel_unref(channel);
627
628         return source;
629 }
630
631 static void disconnect_callback(DBusConnection *conn, void *user_data)
632 {
633         connman_error("D-Bus disconnect");
634
635         g_main_loop_quit(main_loop);
636 }
637
638 static gchar *option_config = NULL;
639 static gchar *option_debug = NULL;
640 static gchar *option_device = NULL;
641 static gchar *option_plugin = NULL;
642 static gchar *option_nodevice = NULL;
643 static gchar *option_noplugin = NULL;
644 static gchar *option_wifi = NULL;
645 static gboolean option_detach = TRUE;
646 static gboolean option_dnsproxy = TRUE;
647 static gboolean option_backtrace = TRUE;
648 static gboolean option_version = FALSE;
649
650 static bool parse_debug(const char *key, const char *value,
651                                         gpointer user_data, GError **error)
652 {
653         if (value) {
654                 if (option_debug) {
655                         char *prev = option_debug;
656
657                         option_debug = g_strconcat(prev, ",", value, NULL);
658                         g_free(prev);
659                 } else {
660                         option_debug = g_strdup(value);
661                 }
662         } else {
663                 g_free(option_debug);
664                 option_debug = g_strdup("*");
665         }
666
667         return true;
668 }
669
670 static bool parse_noplugin(const char *key, const char *value,
671                                         gpointer user_data, GError **error)
672 {
673         if (option_noplugin) {
674                 char *prev = option_noplugin;
675
676                 option_noplugin = g_strconcat(prev, ",", value, NULL);
677                 g_free(prev);
678         } else {
679                 option_noplugin = g_strdup(value);
680         }
681
682         return true;
683 }
684
685 static GOptionEntry options[] = {
686         { "config", 'c', 0, G_OPTION_ARG_STRING, &option_config,
687                                 "Load the specified configuration file "
688                                 "instead of " CONFIGMAINFILE, "FILE" },
689         { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
690                                 G_OPTION_ARG_CALLBACK, parse_debug,
691                                 "Specify debug options to enable", "DEBUG" },
692         { "device", 'i', 0, G_OPTION_ARG_STRING, &option_device,
693                         "Specify networking devices or interfaces", "DEV,..." },
694         { "nodevice", 'I', 0, G_OPTION_ARG_STRING, &option_nodevice,
695                         "Specify networking interfaces to ignore", "DEV,..." },
696         { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
697                                 "Specify plugins to load", "NAME,..." },
698         { "noplugin", 'P', 0, G_OPTION_ARG_CALLBACK, &parse_noplugin,
699                                 "Specify plugins not to load", "NAME,..." },
700         { "wifi", 'W', 0, G_OPTION_ARG_STRING, &option_wifi,
701                                 "Specify driver for WiFi/Supplicant", "NAME" },
702         { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
703                                 G_OPTION_ARG_NONE, &option_detach,
704                                 "Don't fork daemon to background" },
705         { "nodnsproxy", 'r', G_OPTION_FLAG_REVERSE,
706                                 G_OPTION_ARG_NONE, &option_dnsproxy,
707                                 "Don't support DNS resolving" },
708         { "nobacktrace", 0, G_OPTION_FLAG_REVERSE,
709                                 G_OPTION_ARG_NONE, &option_backtrace,
710                                 "Don't print out backtrace information" },
711         { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
712                                 "Show version information and exit" },
713         { NULL },
714 };
715
716 const char *connman_option_get_string(const char *key)
717 {
718         if (g_str_equal(key, CONF_VENDOR_CLASS_ID))
719                 return connman_settings.vendor_class_id;
720
721         if (g_strcmp0(key, "wifi") == 0) {
722                 if (!option_wifi)
723                         return "nl80211,wext";
724                 else
725                         return option_wifi;
726         }
727
728         return NULL;
729 }
730
731 bool connman_setting_get_bool(const char *key)
732 {
733         if (g_str_equal(key, CONF_BG_SCAN))
734                 return connman_settings.bg_scan;
735
736         if (g_str_equal(key, CONF_ALLOW_HOSTNAME_UPDATES))
737                 return connman_settings.allow_hostname_updates;
738
739         if (g_str_equal(key, CONF_ALLOW_DOMAINNAME_UPDATES))
740                 return connman_settings.allow_domainname_updates;
741
742         if (g_str_equal(key, CONF_SINGLE_TECH))
743                 return connman_settings.single_tech;
744
745         if (g_str_equal(key, CONF_PERSISTENT_TETHERING_MODE))
746                 return connman_settings.persistent_tethering_mode;
747
748         if (g_str_equal(key, CONF_ENABLE_6TO4))
749                 return connman_settings.enable_6to4;
750
751         if (g_str_equal(key, CONF_ENABLE_ONLINE_CHECK))
752                 return connman_settings.enable_online_check;
753
754         if (g_str_equal(key, CONF_AUTO_CONNECT_ROAMING_SERVICES))
755                 return connman_settings.auto_connect_roaming_services;
756
757         if (g_str_equal(key, CONF_ACD))
758                 return connman_settings.acd;
759
760         if (g_str_equal(key, CONF_USE_GATEWAYS_AS_TIMESERVERS))
761                 return connman_settings.use_gateways_as_timeservers;
762
763         return false;
764 }
765
766 char **connman_setting_get_string_list(const char *key)
767 {
768         if (g_str_equal(key, CONF_PREF_TIMESERVERS))
769                 return connman_settings.pref_timeservers;
770
771         if (g_str_equal(key, CONF_FALLBACK_NAMESERVERS))
772                 return connman_settings.fallback_nameservers;
773
774         if (g_str_equal(key, CONF_BLACKLISTED_INTERFACES))
775                 return connman_settings.blacklisted_interfaces;
776
777         if (g_str_equal(key, CONF_TETHERING_TECHNOLOGIES))
778                 return connman_settings.tethering_technologies;
779
780 #if defined TIZEN_EXT
781         if (g_str_equal(key, CONF_CELLULAR_INTERFACE))
782                 return connman_settings.cellular_interfaces;
783 #endif
784
785         return NULL;
786 }
787
788 unsigned int *connman_setting_get_uint_list(const char *key)
789 {
790         if (g_str_equal(key, CONF_AUTO_CONNECT_TECHS))
791                 return connman_settings.auto_connect;
792
793         if (g_str_equal(key, CONF_FAVORITE_TECHS))
794                 return connman_settings.favorite_techs;
795
796         if (g_str_equal(key, CONF_PREFERRED_TECHS))
797                 return connman_settings.preferred_techs;
798
799         if (g_str_equal(key, CONF_ALWAYS_CONNECTED_TECHS))
800                 return connman_settings.always_connected_techs;
801
802         return NULL;
803 }
804
805 unsigned int connman_timeout_input_request(void)
806 {
807         return connman_settings.timeout_inputreq;
808 }
809
810 unsigned int connman_timeout_browser_launch(void)
811 {
812         return connman_settings.timeout_browserlaunch;
813 }
814
815 int main(int argc, char *argv[])
816 {
817         GOptionContext *context;
818         GError *error = NULL;
819         DBusConnection *conn;
820         DBusError err;
821         guint signal;
822
823         context = g_option_context_new(NULL);
824         g_option_context_add_main_entries(context, options, NULL);
825
826         if (!g_option_context_parse(context, &argc, &argv, &error)) {
827                 if (error) {
828                         g_printerr("%s\n", error->message);
829                         g_error_free(error);
830                 } else
831                         g_printerr("An unknown error occurred\n");
832                 exit(1);
833         }
834
835         g_option_context_free(context);
836
837         if (option_version) {
838                 printf("%s\n", VERSION);
839                 exit(0);
840         }
841
842         if (option_detach) {
843                 if (daemon(0, 0)) {
844                         perror("Can't start daemon");
845                         exit(1);
846                 }
847         }
848
849         if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
850                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
851                 if (errno != EEXIST)
852                         perror("Failed to create storage directory");
853         }
854
855         umask(0077);
856
857         main_loop = g_main_loop_new(NULL, FALSE);
858
859         signal = setup_signalfd();
860
861         dbus_error_init(&err);
862
863         conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
864         if (!conn) {
865                 if (dbus_error_is_set(&err)) {
866                         fprintf(stderr, "%s\n", err.message);
867                         dbus_error_free(&err);
868                 } else
869                         fprintf(stderr, "Can't register with system bus\n");
870                 exit(1);
871         }
872
873         g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
874
875         __connman_log_init(argv[0], option_debug, option_detach,
876                         option_backtrace, "Connection Manager", VERSION);
877
878         __connman_dbus_init(conn);
879
880         if (!option_config)
881                 config_init(CONFIGMAINFILE);
882         else
883                 config_init(option_config);
884
885         __connman_util_init();
886         __connman_inotify_init();
887         __connman_technology_init();
888         __connman_notifier_init();
889         __connman_agent_init();
890         __connman_service_init();
891         __connman_peer_service_init();
892         __connman_peer_init();
893 #if defined TIZEN_EXT_WIFI_MESH
894         __connman_mesh_init();
895 #endif /* TIZEN_EXT_WIFI_MESH */
896         __connman_provider_init();
897         __connman_network_init();
898         __connman_config_init();
899         __connman_device_init(option_device, option_nodevice);
900
901         __connman_ippool_init();
902         __connman_firewall_init();
903         __connman_nat_init();
904         __connman_tethering_init();
905         __connman_counter_init();
906         __connman_manager_init();
907         __connman_stats_init();
908         __connman_clock_init();
909
910         __connman_ipconfig_init();
911         __connman_rtnl_init();
912         __connman_task_init();
913         __connman_proxy_init();
914         __connman_detect_init();
915         __connman_session_init();
916         __connman_timeserver_init();
917         __connman_connection_init();
918
919         __connman_plugin_init(option_plugin, option_noplugin);
920
921         __connman_resolver_init(option_dnsproxy);
922         __connman_rtnl_start();
923         __connman_dhcp_init();
924         __connman_dhcpv6_init();
925         __connman_wpad_init();
926         __connman_wispr_init();
927 #if !defined TIZEN_EXT
928         __connman_rfkill_init();
929         __connman_machine_init();
930 #endif
931
932         g_free(option_config);
933         g_free(option_device);
934         g_free(option_plugin);
935         g_free(option_nodevice);
936         g_free(option_noplugin);
937
938         g_main_loop_run(main_loop);
939
940         g_source_remove(signal);
941
942 #if !defined TIZEN_EXT
943         __connman_machine_cleanup();
944         __connman_rfkill_cleanup();
945 #endif
946         __connman_wispr_cleanup();
947         __connman_wpad_cleanup();
948         __connman_dhcpv6_cleanup();
949         __connman_session_cleanup();
950         __connman_plugin_cleanup();
951         __connman_provider_cleanup();
952         __connman_connection_cleanup();
953         __connman_timeserver_cleanup();
954         __connman_detect_cleanup();
955         __connman_proxy_cleanup();
956         __connman_task_cleanup();
957         __connman_rtnl_cleanup();
958         __connman_resolver_cleanup();
959
960         __connman_clock_cleanup();
961         __connman_stats_cleanup();
962         __connman_config_cleanup();
963         __connman_manager_cleanup();
964         __connman_counter_cleanup();
965         __connman_tethering_cleanup();
966         __connman_nat_cleanup();
967         __connman_firewall_cleanup();
968         __connman_peer_service_cleanup();
969         __connman_peer_cleanup();
970 #if defined TIZEN_EXT_WIFI_MESH
971         __connman_mesh_cleanup();
972 #endif /* TIZEN_EXT_WIFI_MESH */
973         __connman_ippool_cleanup();
974         __connman_device_cleanup();
975         __connman_network_cleanup();
976         __connman_dhcp_cleanup();
977         __connman_service_cleanup();
978         __connman_agent_cleanup();
979         __connman_ipconfig_cleanup();
980         __connman_notifier_cleanup();
981         __connman_technology_cleanup();
982         __connman_inotify_cleanup();
983
984         __connman_util_cleanup();
985         __connman_dbus_cleanup();
986
987         __connman_log_cleanup(option_backtrace);
988
989         dbus_connection_unref(conn);
990
991         g_main_loop_unref(main_loop);
992
993         if (connman_settings.pref_timeservers)
994                 g_strfreev(connman_settings.pref_timeservers);
995
996         g_free(connman_settings.auto_connect);
997         g_free(connman_settings.favorite_techs);
998         g_free(connman_settings.preferred_techs);
999         g_strfreev(connman_settings.fallback_nameservers);
1000         g_strfreev(connman_settings.blacklisted_interfaces);
1001         g_strfreev(connman_settings.tethering_technologies);
1002
1003         g_free(option_debug);
1004         g_free(option_wifi);
1005
1006         return 0;
1007 }