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>
33 #include <sys/types.h>
34 #include <sys/resource.h>
44 #define DEFAULT_INPUT_REQUEST_TIMEOUT (120 * 1000)
45 #define DEFAULT_BROWSER_LAUNCH_TIMEOUT (300 * 1000)
47 #define MAINFILE "main.conf"
48 #define CONFIGMAINFILE CONFIGDIR "/" MAINFILE
50 static char *default_auto_connect[] = {
57 static char *default_blacklist[] = {
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;
79 char **tethering_technologies;
80 bool persistent_tethering_mode;
82 char *vendor_class_id;
83 bool enable_online_check;
85 char **cellular_interfaces;
86 bool tizen_tv_extension;
88 } connman_settings = {
90 .pref_timeservers = 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,
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,
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"
132 static const char *supported_options[] = {
134 CONF_PREF_TIMESERVERS,
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,
144 CONF_TETHERING_TECHNOLOGIES,
145 CONF_PERSISTENT_TETHERING_MODE,
147 CONF_ENABLE_ONLINE_CHECK,
148 #if defined TIZEN_EXT
149 CONF_CELLULAR_INTERFACE,
155 static GKeyFile *load_config(const char *file)
160 keyfile = g_key_file_new();
162 g_key_file_set_list_separator(keyfile, ',');
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,
171 g_key_file_free(keyfile);
178 static uint *parse_service_types(char **str_list, gsize len)
180 unsigned int *type_list;
182 enum connman_service_type type;
184 type_list = g_try_new0(unsigned int, len + 1);
190 while (str_list[i]) {
191 type = __connman_service_string2type(str_list[i]);
193 if (type != CONNMAN_SERVICE_TYPE_UNKNOWN) {
200 type_list[j] = CONNMAN_SERVICE_TYPE_UNKNOWN;
205 static char **parse_fallback_nameservers(char **nameservers, gsize len)
210 servers = g_try_new0(char *, len + 1);
216 while (nameservers[i]) {
217 if (connman_inet_check_ipaddress(nameservers[i]) > 0) {
218 servers[j] = g_strdup(nameservers[i]);
227 static void check_config(GKeyFile *config)
235 keys = g_key_file_get_groups(config, NULL);
237 for (j = 0; keys && keys[j]; j++) {
238 if (g_strcmp0(keys[j], "General") != 0)
239 connman_warn("Unknown group %s in %s",
245 keys = g_key_file_get_keys(config, "General", NULL, NULL);
247 for (j = 0; keys && keys[j]; j++) {
252 for (i = 0; supported_options[i]; i++) {
253 if (g_strcmp0(keys[j], supported_options[i]) == 0) {
258 if (!found && !supported_options[i])
259 connman_warn("Unknown option %s in %s",
266 #if defined TIZEN_EXT
267 static void check_Tizen_configuration(GKeyFile *config)
269 GError *error = NULL;
270 char **cellular_interfaces;
274 cellular_interfaces = g_key_file_get_string_list(config, "General",
275 CONF_CELLULAR_INTERFACE, &len, &error);
278 connman_settings.cellular_interfaces = cellular_interfaces;
280 g_clear_error(&error);
282 boolean = __connman_config_get_bool(config, "General",
283 CONF_TIZEN_TV_EXT, &error);
285 connman_settings.tizen_tv_extension = boolean;
287 g_clear_error(&error);
290 static void set_nofile_inc(void)
295 rlim.rlim_cur = 8192;
296 rlim.rlim_max = 8192;
298 err = setrlimit(RLIMIT_NOFILE, &rlim);
300 DBG("fail to increase FILENO err(%d)", err);
306 static void parse_config(GKeyFile *config)
308 GError *error = NULL;
314 char *vendor_class_id;
319 connman_settings.auto_connect =
320 parse_service_types(default_auto_connect, 3);
321 connman_settings.blacklisted_interfaces =
322 g_strdupv(default_blacklist);
326 DBG("parsing %s", MAINFILE);
328 boolean = g_key_file_get_boolean(config, "General",
329 CONF_BG_SCAN, &error);
331 connman_settings.bg_scan = boolean;
333 g_clear_error(&error);
335 timeservers = __connman_config_get_string_list(config, "General",
336 CONF_PREF_TIMESERVERS, NULL, &error);
338 connman_settings.pref_timeservers = timeservers;
340 g_clear_error(&error);
342 str_list = __connman_config_get_string_list(config, "General",
343 CONF_AUTO_CONNECT, &len, &error);
346 connman_settings.auto_connect =
347 parse_service_types(str_list, len);
349 connman_settings.auto_connect =
350 parse_service_types(default_auto_connect, 3);
352 g_strfreev(str_list);
354 g_clear_error(&error);
356 str_list = __connman_config_get_string_list(config, "General",
357 CONF_PREFERRED_TECHS, &len, &error);
360 connman_settings.preferred_techs =
361 parse_service_types(str_list, len);
363 g_strfreev(str_list);
365 g_clear_error(&error);
367 str_list = __connman_config_get_string_list(config, "General",
368 CONF_ALWAYS_CONNECTED_TECHS, &len, &error);
371 connman_settings.always_connected_techs =
372 parse_service_types(str_list, len);
374 g_strfreev(str_list);
376 g_clear_error(&error);
378 str_list = __connman_config_get_string_list(config, "General",
379 CONF_FALLBACK_NAMESERVERS, &len, &error);
382 connman_settings.fallback_nameservers =
383 parse_fallback_nameservers(str_list, len);
385 g_strfreev(str_list);
387 g_clear_error(&error);
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;
394 g_clear_error(&error);
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;
401 g_clear_error(&error);
403 interfaces = __connman_config_get_string_list(config, "General",
404 CONF_BLACKLISTED_INTERFACES, &len, &error);
407 connman_settings.blacklisted_interfaces = interfaces;
409 connman_settings.blacklisted_interfaces =
410 g_strdupv(default_blacklist);
412 g_clear_error(&error);
414 boolean = __connman_config_get_bool(config, "General",
415 CONF_ALLOW_HOSTNAME_UPDATES,
418 connman_settings.allow_hostname_updates = boolean;
420 g_clear_error(&error);
422 boolean = __connman_config_get_bool(config, "General",
423 CONF_SINGLE_TECH, &error);
425 connman_settings.single_tech = boolean;
427 g_clear_error(&error);
429 tethering = __connman_config_get_string_list(config, "General",
430 CONF_TETHERING_TECHNOLOGIES, &len, &error);
433 connman_settings.tethering_technologies = tethering;
435 g_clear_error(&error);
437 boolean = __connman_config_get_bool(config, "General",
438 CONF_PERSISTENT_TETHERING_MODE,
441 connman_settings.persistent_tethering_mode = boolean;
443 g_clear_error(&error);
445 boolean = __connman_config_get_bool(config, "General",
446 CONF_ENABLE_6TO4, &error);
448 connman_settings.enable_6to4 = boolean;
450 g_clear_error(&error);
452 vendor_class_id = __connman_config_get_string(config, "General",
453 CONF_VENDOR_CLASS_ID, &error);
455 connman_settings.vendor_class_id = vendor_class_id;
457 g_clear_error(&error);
459 boolean = __connman_config_get_bool(config, "General",
460 CONF_ENABLE_ONLINE_CHECK, &error);
462 connman_settings.enable_online_check = boolean;
464 connman_info("Online check disabled by main config.");
467 g_clear_error(&error);
469 #if defined TIZEN_EXT
470 check_Tizen_configuration(config);
474 static int config_init(const char *file)
478 #if defined TIZEN_EXT
481 config = load_config(file);
482 check_config(config);
483 parse_config(config);
485 g_key_file_free(config);
490 static GMainLoop *main_loop = NULL;
492 static unsigned int __terminated = 0;
494 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
497 struct signalfd_siginfo si;
501 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
504 fd = g_io_channel_unix_get_fd(channel);
506 result = read(fd, &si, sizeof(si));
507 if (result != sizeof(si))
510 switch (si.ssi_signo) {
513 if (__terminated == 0) {
514 connman_info("Terminating");
515 g_main_loop_quit(main_loop);
525 static guint setup_signalfd(void)
533 sigaddset(&mask, SIGINT);
534 sigaddset(&mask, SIGTERM);
536 if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
537 perror("Failed to set signal mask");
541 fd = signalfd(-1, &mask, 0);
543 perror("Failed to create signal descriptor");
547 channel = g_io_channel_unix_new(fd);
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);
553 source = g_io_add_watch(channel,
554 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
555 signal_handler, NULL);
557 g_io_channel_unref(channel);
562 static void disconnect_callback(DBusConnection *conn, void *user_data)
564 connman_error("D-Bus disconnect");
566 g_main_loop_quit(main_loop);
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;
581 static bool parse_debug(const char *key, const char *value,
582 gpointer user_data, GError **error)
586 char *prev = option_debug;
588 option_debug = g_strconcat(prev, ",", value, NULL);
591 option_debug = g_strdup(value);
594 g_free(option_debug);
595 option_debug = g_strdup("*");
601 static bool parse_noplugin(const char *key, const char *value,
602 gpointer user_data, GError **error)
604 if (option_noplugin) {
605 char *prev = option_noplugin;
607 option_noplugin = g_strconcat(prev, ",", value, NULL);
610 option_noplugin = g_strdup(value);
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" },
647 const char *connman_option_get_string(const char *key)
649 if (g_str_equal(key, CONF_VENDOR_CLASS_ID))
650 return connman_settings.vendor_class_id;
652 if (g_strcmp0(key, "wifi") == 0) {
654 return "nl80211,wext";
662 bool connman_setting_get_bool(const char *key)
664 if (g_str_equal(key, CONF_BG_SCAN))
665 return connman_settings.bg_scan;
667 if (g_str_equal(key, CONF_ALLOW_HOSTNAME_UPDATES))
668 return connman_settings.allow_hostname_updates;
670 if (g_str_equal(key, CONF_SINGLE_TECH))
671 return connman_settings.single_tech;
673 if (g_str_equal(key, CONF_PERSISTENT_TETHERING_MODE))
674 return connman_settings.persistent_tethering_mode;
676 if (g_str_equal(key, CONF_ENABLE_6TO4))
677 return connman_settings.enable_6to4;
679 if (g_str_equal(key, CONF_ENABLE_ONLINE_CHECK))
680 return connman_settings.enable_online_check;
685 char **connman_setting_get_string_list(const char *key)
687 if (g_str_equal(key, CONF_PREF_TIMESERVERS))
688 return connman_settings.pref_timeservers;
690 if (g_str_equal(key, CONF_FALLBACK_NAMESERVERS))
691 return connman_settings.fallback_nameservers;
693 if (g_str_equal(key, CONF_BLACKLISTED_INTERFACES))
694 return connman_settings.blacklisted_interfaces;
696 if (g_str_equal(key, CONF_TETHERING_TECHNOLOGIES))
697 return connman_settings.tethering_technologies;
699 #if defined TIZEN_EXT
700 if (g_str_equal(key, CONF_CELLULAR_INTERFACE))
701 return connman_settings.cellular_interfaces;
707 unsigned int *connman_setting_get_uint_list(const char *key)
709 if (g_str_equal(key, CONF_AUTO_CONNECT))
710 return connman_settings.auto_connect;
712 if (g_str_equal(key, CONF_PREFERRED_TECHS))
713 return connman_settings.preferred_techs;
715 if (g_str_equal(key, CONF_ALWAYS_CONNECTED_TECHS))
716 return connman_settings.always_connected_techs;
721 unsigned int connman_timeout_input_request(void)
723 return connman_settings.timeout_inputreq;
726 unsigned int connman_timeout_browser_launch(void)
728 return connman_settings.timeout_browserlaunch;
731 int main(int argc, char *argv[])
733 GOptionContext *context;
734 GError *error = NULL;
735 DBusConnection *conn;
739 context = g_option_context_new(NULL);
740 g_option_context_add_main_entries(context, options, NULL);
742 if (!g_option_context_parse(context, &argc, &argv, &error)) {
744 g_printerr("%s\n", error->message);
747 g_printerr("An unknown error occurred\n");
751 g_option_context_free(context);
753 if (option_version) {
754 printf("%s\n", VERSION);
760 perror("Can't start daemon");
765 if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
766 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
768 perror("Failed to create storage directory");
773 main_loop = g_main_loop_new(NULL, FALSE);
775 signal = setup_signalfd();
777 dbus_error_init(&err);
779 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
781 if (dbus_error_is_set(&err)) {
782 fprintf(stderr, "%s\n", err.message);
783 dbus_error_free(&err);
785 fprintf(stderr, "Can't register with system bus\n");
789 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
791 __connman_log_init(argv[0], option_debug, option_detach,
792 option_backtrace, "Connection Manager", VERSION);
794 __connman_dbus_init(conn);
797 config_init(CONFIGMAINFILE);
799 config_init(option_config);
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);
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();
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();
835 __connman_plugin_init(option_plugin, option_noplugin);
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();
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);
854 g_main_loop_run(main_loop);
856 g_source_remove(signal);
858 #if !defined TIZEN_EXT
859 __connman_machine_cleanup();
860 __connman_rfkill_cleanup();
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();
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();
900 __connman_util_cleanup();
901 __connman_dbus_cleanup();
903 __connman_log_cleanup(option_backtrace);
905 dbus_connection_unref(conn);
907 g_main_loop_unref(main_loop);
909 if (connman_settings.pref_timeservers)
910 g_strfreev(connman_settings.pref_timeservers);
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);
918 g_free(option_debug);