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