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