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