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