5 * Copyright (C) 2007-2013 Intel Corporation. All rights reserved.
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.
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.
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
32 #include <sys/signalfd.h>
42 #define DEFAULT_INPUT_REQUEST_TIMEOUT (120 * 1000)
43 #define DEFAULT_BROWSER_LAUNCH_TIMEOUT (300 * 1000)
45 #define MAINFILE "main.conf"
46 #define CONFIGMAINFILE CONFIGDIR "/" MAINFILE
48 static char *default_auto_connect[] = {
55 static char *default_blacklist[] = {
67 char **pref_timeservers;
68 unsigned int *auto_connect;
69 unsigned int *preferred_techs;
70 unsigned int *always_connected_techs;
71 char **fallback_nameservers;
72 unsigned int timeout_inputreq;
73 unsigned int timeout_browserlaunch;
74 char **blacklisted_interfaces;
75 bool allow_hostname_updates;
77 char **tethering_technologies;
78 bool persistent_tethering_mode;
80 char *vendor_class_id;
81 bool enable_online_check;
82 } connman_settings = {
84 .pref_timeservers = NULL,
86 .preferred_techs = NULL,
87 .always_connected_techs = NULL,
88 .fallback_nameservers = NULL,
89 .timeout_inputreq = DEFAULT_INPUT_REQUEST_TIMEOUT,
90 .timeout_browserlaunch = DEFAULT_BROWSER_LAUNCH_TIMEOUT,
91 .blacklisted_interfaces = NULL,
92 .allow_hostname_updates = true,
94 .tethering_technologies = NULL,
95 .persistent_tethering_mode = false,
97 .vendor_class_id = NULL,
98 .enable_online_check = true,
101 #define CONF_BG_SCAN "BackgroundScanning"
102 #define CONF_PREF_TIMESERVERS "FallbackTimeservers"
103 #define CONF_AUTO_CONNECT "DefaultAutoConnectTechnologies"
104 #define CONF_ALWAYS_CONNECTED_TECHS "AlwaysConnectedTechnologies"
105 #define CONF_PREFERRED_TECHS "PreferredTechnologies"
106 #define CONF_FALLBACK_NAMESERVERS "FallbackNameservers"
107 #define CONF_TIMEOUT_INPUTREQ "InputRequestTimeout"
108 #define CONF_TIMEOUT_BROWSERLAUNCH "BrowserLaunchTimeout"
109 #define CONF_BLACKLISTED_INTERFACES "NetworkInterfaceBlacklist"
110 #define CONF_ALLOW_HOSTNAME_UPDATES "AllowHostnameUpdates"
111 #define CONF_SINGLE_TECH "SingleConnectedTechnology"
112 #define CONF_TETHERING_TECHNOLOGIES "TetheringTechnologies"
113 #define CONF_PERSISTENT_TETHERING_MODE "PersistentTetheringMode"
114 #define CONF_ENABLE_6TO4 "Enable6to4"
115 #define CONF_VENDOR_CLASS_ID "VendorClassID"
116 #define CONF_ENABLE_ONLINE_CHECK "EnableOnlineCheck"
118 static const char *supported_options[] = {
120 CONF_PREF_TIMESERVERS,
122 CONF_ALWAYS_CONNECTED_TECHS,
123 CONF_PREFERRED_TECHS,
124 CONF_FALLBACK_NAMESERVERS,
125 CONF_TIMEOUT_INPUTREQ,
126 CONF_TIMEOUT_BROWSERLAUNCH,
127 CONF_BLACKLISTED_INTERFACES,
128 CONF_ALLOW_HOSTNAME_UPDATES,
130 CONF_TETHERING_TECHNOLOGIES,
131 CONF_PERSISTENT_TETHERING_MODE,
133 CONF_VENDOR_CLASS_ID,
134 CONF_ENABLE_ONLINE_CHECK,
138 static GKeyFile *load_config(const char *file)
143 keyfile = g_key_file_new();
145 g_key_file_set_list_separator(keyfile, ',');
147 if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
148 if (err->code != G_FILE_ERROR_NOENT) {
149 connman_error("Parsing %s failed: %s", file,
154 g_key_file_free(keyfile);
161 static uint *parse_service_types(char **str_list, gsize len)
163 unsigned int *type_list;
165 enum connman_service_type type;
167 type_list = g_try_new0(unsigned int, len + 1);
173 while (str_list[i]) {
174 type = __connman_service_string2type(str_list[i]);
176 if (type != CONNMAN_SERVICE_TYPE_UNKNOWN) {
183 type_list[j] = CONNMAN_SERVICE_TYPE_UNKNOWN;
188 static char **parse_fallback_nameservers(char **nameservers, gsize len)
193 servers = g_try_new0(char *, len + 1);
199 while (nameservers[i]) {
200 if (connman_inet_check_ipaddress(nameservers[i]) > 0) {
201 servers[j] = g_strdup(nameservers[i]);
210 static void check_config(GKeyFile *config)
218 keys = g_key_file_get_groups(config, NULL);
220 for (j = 0; keys && keys[j]; j++) {
221 if (g_strcmp0(keys[j], "General") != 0)
222 connman_warn("Unknown group %s in %s",
228 keys = g_key_file_get_keys(config, "General", NULL, NULL);
230 for (j = 0; keys && keys[j]; j++) {
235 for (i = 0; supported_options[i]; i++) {
236 if (g_strcmp0(keys[j], supported_options[i]) == 0) {
241 if (!found && !supported_options[i])
242 connman_warn("Unknown option %s in %s",
249 static void parse_config(GKeyFile *config)
251 GError *error = NULL;
257 char *vendor_class_id;
262 connman_settings.auto_connect =
263 parse_service_types(default_auto_connect, 3);
264 connman_settings.blacklisted_interfaces =
265 g_strdupv(default_blacklist);
269 DBG("parsing %s", MAINFILE);
271 boolean = g_key_file_get_boolean(config, "General",
272 CONF_BG_SCAN, &error);
274 connman_settings.bg_scan = boolean;
276 g_clear_error(&error);
278 timeservers = __connman_config_get_string_list(config, "General",
279 CONF_PREF_TIMESERVERS, NULL, &error);
281 connman_settings.pref_timeservers = timeservers;
283 g_clear_error(&error);
285 str_list = __connman_config_get_string_list(config, "General",
286 CONF_AUTO_CONNECT, &len, &error);
289 connman_settings.auto_connect =
290 parse_service_types(str_list, len);
292 connman_settings.auto_connect =
293 parse_service_types(default_auto_connect, 3);
295 g_strfreev(str_list);
297 g_clear_error(&error);
299 str_list = __connman_config_get_string_list(config, "General",
300 CONF_PREFERRED_TECHS, &len, &error);
303 connman_settings.preferred_techs =
304 parse_service_types(str_list, len);
306 g_strfreev(str_list);
308 g_clear_error(&error);
310 str_list = __connman_config_get_string_list(config, "General",
311 CONF_ALWAYS_CONNECTED_TECHS, &len, &error);
314 connman_settings.always_connected_techs =
315 parse_service_types(str_list, len);
317 g_strfreev(str_list);
319 g_clear_error(&error);
321 str_list = __connman_config_get_string_list(config, "General",
322 CONF_FALLBACK_NAMESERVERS, &len, &error);
325 connman_settings.fallback_nameservers =
326 parse_fallback_nameservers(str_list, len);
328 g_strfreev(str_list);
330 g_clear_error(&error);
332 timeout = g_key_file_get_integer(config, "General",
333 CONF_TIMEOUT_INPUTREQ, &error);
334 if (!error && timeout >= 0)
335 connman_settings.timeout_inputreq = timeout * 1000;
337 g_clear_error(&error);
339 timeout = g_key_file_get_integer(config, "General",
340 CONF_TIMEOUT_BROWSERLAUNCH, &error);
341 if (!error && timeout >= 0)
342 connman_settings.timeout_browserlaunch = timeout * 1000;
344 g_clear_error(&error);
346 interfaces = __connman_config_get_string_list(config, "General",
347 CONF_BLACKLISTED_INTERFACES, &len, &error);
350 connman_settings.blacklisted_interfaces = interfaces;
352 connman_settings.blacklisted_interfaces =
353 g_strdupv(default_blacklist);
355 g_clear_error(&error);
357 boolean = __connman_config_get_bool(config, "General",
358 CONF_ALLOW_HOSTNAME_UPDATES,
361 connman_settings.allow_hostname_updates = boolean;
363 g_clear_error(&error);
365 boolean = __connman_config_get_bool(config, "General",
366 CONF_SINGLE_TECH, &error);
368 connman_settings.single_tech = boolean;
370 g_clear_error(&error);
372 tethering = __connman_config_get_string_list(config, "General",
373 CONF_TETHERING_TECHNOLOGIES, &len, &error);
376 connman_settings.tethering_technologies = tethering;
378 g_clear_error(&error);
380 boolean = __connman_config_get_bool(config, "General",
381 CONF_PERSISTENT_TETHERING_MODE,
384 connman_settings.persistent_tethering_mode = boolean;
386 g_clear_error(&error);
388 boolean = __connman_config_get_bool(config, "General",
389 CONF_ENABLE_6TO4, &error);
391 connman_settings.enable_6to4 = boolean;
393 g_clear_error(&error);
395 vendor_class_id = __connman_config_get_string(config, "General",
396 CONF_VENDOR_CLASS_ID, &error);
398 connman_settings.vendor_class_id = vendor_class_id;
400 g_clear_error(&error);
402 boolean = __connman_config_get_bool(config, "General",
403 CONF_ENABLE_ONLINE_CHECK, &error);
405 connman_settings.enable_online_check = boolean;
407 connman_info("Online check disabled by main config.");
410 g_clear_error(&error);
413 static int config_init(const char *file)
417 config = load_config(file);
418 check_config(config);
419 parse_config(config);
421 g_key_file_free(config);
426 static GMainLoop *main_loop = NULL;
428 static unsigned int __terminated = 0;
430 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
433 struct signalfd_siginfo si;
437 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
440 fd = g_io_channel_unix_get_fd(channel);
442 result = read(fd, &si, sizeof(si));
443 if (result != sizeof(si))
446 switch (si.ssi_signo) {
449 if (__terminated == 0) {
450 connman_info("Terminating");
451 g_main_loop_quit(main_loop);
461 static guint setup_signalfd(void)
469 sigaddset(&mask, SIGINT);
470 sigaddset(&mask, SIGTERM);
472 if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
473 perror("Failed to set signal mask");
477 fd = signalfd(-1, &mask, 0);
479 perror("Failed to create signal descriptor");
483 channel = g_io_channel_unix_new(fd);
485 g_io_channel_set_close_on_unref(channel, TRUE);
486 g_io_channel_set_encoding(channel, NULL, NULL);
487 g_io_channel_set_buffered(channel, FALSE);
489 source = g_io_add_watch(channel,
490 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
491 signal_handler, NULL);
493 g_io_channel_unref(channel);
498 static void disconnect_callback(DBusConnection *conn, void *user_data)
500 connman_error("D-Bus disconnect");
502 g_main_loop_quit(main_loop);
505 static gchar *option_config = NULL;
506 static gchar *option_debug = NULL;
507 static gchar *option_device = NULL;
508 static gchar *option_plugin = NULL;
509 static gchar *option_nodevice = NULL;
510 static gchar *option_noplugin = NULL;
511 static gchar *option_wifi = NULL;
512 static gboolean option_detach = TRUE;
513 static gboolean option_dnsproxy = TRUE;
514 static gboolean option_backtrace = TRUE;
515 static gboolean option_version = FALSE;
517 static bool parse_debug(const char *key, const char *value,
518 gpointer user_data, GError **error)
522 char *prev = option_debug;
524 option_debug = g_strconcat(prev, ",", value, NULL);
527 option_debug = g_strdup(value);
530 g_free(option_debug);
531 option_debug = g_strdup("*");
537 static bool parse_noplugin(const char *key, const char *value,
538 gpointer user_data, GError **error)
540 if (option_noplugin) {
541 char *prev = option_noplugin;
543 option_noplugin = g_strconcat(prev, ",", value, NULL);
546 option_noplugin = g_strdup(value);
552 static GOptionEntry options[] = {
553 { "config", 'c', 0, G_OPTION_ARG_STRING, &option_config,
554 "Load the specified configuration file "
555 "instead of " CONFIGMAINFILE, "FILE" },
556 { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
557 G_OPTION_ARG_CALLBACK, parse_debug,
558 "Specify debug options to enable", "DEBUG" },
559 { "device", 'i', 0, G_OPTION_ARG_STRING, &option_device,
560 "Specify networking device or interface", "DEV" },
561 { "nodevice", 'I', 0, G_OPTION_ARG_STRING, &option_nodevice,
562 "Specify networking interface to ignore", "DEV" },
563 { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
564 "Specify plugins to load", "NAME,..." },
565 { "noplugin", 'P', 0, G_OPTION_ARG_CALLBACK, &parse_noplugin,
566 "Specify plugins not to load", "NAME,..." },
567 { "wifi", 'W', 0, G_OPTION_ARG_STRING, &option_wifi,
568 "Specify driver for WiFi/Supplicant", "NAME" },
569 { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
570 G_OPTION_ARG_NONE, &option_detach,
571 "Don't fork daemon to background" },
572 { "nodnsproxy", 'r', G_OPTION_FLAG_REVERSE,
573 G_OPTION_ARG_NONE, &option_dnsproxy,
574 "Don't enable DNS Proxy" },
575 { "nobacktrace", 0, G_OPTION_FLAG_REVERSE,
576 G_OPTION_ARG_NONE, &option_backtrace,
577 "Don't print out backtrace information" },
578 { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
579 "Show version information and exit" },
583 const char *connman_option_get_string(const char *key)
585 if (g_str_equal(key, CONF_VENDOR_CLASS_ID))
586 return connman_settings.vendor_class_id;
588 if (g_strcmp0(key, "wifi") == 0) {
590 return "nl80211,wext";
598 bool connman_setting_get_bool(const char *key)
600 if (g_str_equal(key, CONF_BG_SCAN))
601 return connman_settings.bg_scan;
603 if (g_str_equal(key, CONF_ALLOW_HOSTNAME_UPDATES))
604 return connman_settings.allow_hostname_updates;
606 if (g_str_equal(key, CONF_SINGLE_TECH))
607 return connman_settings.single_tech;
609 if (g_str_equal(key, CONF_PERSISTENT_TETHERING_MODE))
610 return connman_settings.persistent_tethering_mode;
612 if (g_str_equal(key, CONF_ENABLE_6TO4))
613 return connman_settings.enable_6to4;
615 if (g_str_equal(key, CONF_ENABLE_ONLINE_CHECK))
616 return connman_settings.enable_online_check;
621 char **connman_setting_get_string_list(const char *key)
623 if (g_str_equal(key, CONF_PREF_TIMESERVERS))
624 return connman_settings.pref_timeservers;
626 if (g_str_equal(key, CONF_FALLBACK_NAMESERVERS))
627 return connman_settings.fallback_nameservers;
629 if (g_str_equal(key, CONF_BLACKLISTED_INTERFACES))
630 return connman_settings.blacklisted_interfaces;
632 if (g_str_equal(key, CONF_TETHERING_TECHNOLOGIES))
633 return connman_settings.tethering_technologies;
638 unsigned int *connman_setting_get_uint_list(const char *key)
640 if (g_str_equal(key, CONF_AUTO_CONNECT))
641 return connman_settings.auto_connect;
643 if (g_str_equal(key, CONF_PREFERRED_TECHS))
644 return connman_settings.preferred_techs;
646 if (g_str_equal(key, CONF_ALWAYS_CONNECTED_TECHS))
647 return connman_settings.always_connected_techs;
652 unsigned int connman_timeout_input_request(void)
654 return connman_settings.timeout_inputreq;
657 unsigned int connman_timeout_browser_launch(void)
659 return connman_settings.timeout_browserlaunch;
662 int main(int argc, char *argv[])
664 GOptionContext *context;
665 GError *error = NULL;
666 DBusConnection *conn;
670 context = g_option_context_new(NULL);
671 g_option_context_add_main_entries(context, options, NULL);
673 if (!g_option_context_parse(context, &argc, &argv, &error)) {
675 g_printerr("%s\n", error->message);
678 g_printerr("An unknown error occurred\n");
682 g_option_context_free(context);
684 if (option_version) {
685 printf("%s\n", VERSION);
691 perror("Can't start daemon");
696 if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
697 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
699 perror("Failed to create storage directory");
704 main_loop = g_main_loop_new(NULL, FALSE);
706 signal = setup_signalfd();
708 dbus_error_init(&err);
710 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
712 if (dbus_error_is_set(&err)) {
713 fprintf(stderr, "%s\n", err.message);
714 dbus_error_free(&err);
716 fprintf(stderr, "Can't register with system bus\n");
720 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
722 __connman_log_init(argv[0], option_debug, option_detach,
723 option_backtrace, "Connection Manager", VERSION);
725 __connman_dbus_init(conn);
728 config_init(CONFIGMAINFILE);
730 config_init(option_config);
732 __connman_util_init();
733 __connman_inotify_init();
734 __connman_technology_init();
735 __connman_notifier_init();
736 __connman_agent_init();
737 __connman_service_init();
738 __connman_peer_service_init();
739 __connman_peer_init();
740 __connman_provider_init();
741 __connman_network_init();
742 __connman_config_init();
743 __connman_device_init(option_device, option_nodevice);
745 __connman_ippool_init();
746 __connman_firewall_init();
747 __connman_nat_init();
748 __connman_tethering_init();
749 __connman_counter_init();
750 __connman_manager_init();
751 __connman_stats_init();
752 __connman_clock_init();
754 __connman_ipconfig_init();
755 __connman_rtnl_init();
756 __connman_task_init();
757 __connman_proxy_init();
758 __connman_detect_init();
759 __connman_session_init();
760 __connman_timeserver_init();
761 __connman_connection_init();
763 __connman_plugin_init(option_plugin, option_noplugin);
765 __connman_resolver_init(option_dnsproxy);
766 __connman_rtnl_start();
767 __connman_dhcp_init();
768 __connman_dhcpv6_init();
769 __connman_wpad_init();
770 __connman_wispr_init();
771 __connman_rfkill_init();
772 __connman_machine_init();
774 g_free(option_config);
775 g_free(option_device);
776 g_free(option_plugin);
777 g_free(option_nodevice);
778 g_free(option_noplugin);
780 g_main_loop_run(main_loop);
782 g_source_remove(signal);
784 __connman_machine_cleanup();
785 __connman_rfkill_cleanup();
786 __connman_wispr_cleanup();
787 __connman_wpad_cleanup();
788 __connman_dhcpv6_cleanup();
789 __connman_session_cleanup();
790 __connman_plugin_cleanup();
791 __connman_provider_cleanup();
792 __connman_connection_cleanup();
793 __connman_timeserver_cleanup();
794 __connman_detect_cleanup();
795 __connman_proxy_cleanup();
796 __connman_task_cleanup();
797 __connman_rtnl_cleanup();
798 __connman_resolver_cleanup();
800 __connman_clock_cleanup();
801 __connman_stats_cleanup();
802 __connman_config_cleanup();
803 __connman_manager_cleanup();
804 __connman_counter_cleanup();
805 __connman_tethering_cleanup();
806 __connman_nat_cleanup();
807 __connman_firewall_cleanup();
808 __connman_peer_service_cleanup();
809 __connman_peer_cleanup();
810 __connman_ippool_cleanup();
811 __connman_device_cleanup();
812 __connman_network_cleanup();
813 __connman_dhcp_cleanup();
814 __connman_service_cleanup();
815 __connman_agent_cleanup();
816 __connman_ipconfig_cleanup();
817 __connman_notifier_cleanup();
818 __connman_technology_cleanup();
819 __connman_inotify_cleanup();
821 __connman_util_cleanup();
822 __connman_dbus_cleanup();
824 __connman_log_cleanup(option_backtrace);
826 dbus_connection_unref(conn);
828 g_main_loop_unref(main_loop);
830 if (connman_settings.pref_timeservers)
831 g_strfreev(connman_settings.pref_timeservers);
833 g_free(connman_settings.auto_connect);
834 g_free(connman_settings.preferred_techs);
835 g_strfreev(connman_settings.fallback_nameservers);
836 g_strfreev(connman_settings.blacklisted_interfaces);
837 g_strfreev(connman_settings.tethering_technologies);
839 g_free(option_debug);