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