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;
87 bool use_gateway_timeserver;
89 } connman_settings = {
91 .pref_timeservers = 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,
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"
135 static const char *supported_options[] = {
137 CONF_PREF_TIMESERVERS,
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,
147 CONF_TETHERING_TECHNOLOGIES,
148 CONF_PERSISTENT_TETHERING_MODE,
150 CONF_ENABLE_ONLINE_CHECK,
151 #if defined TIZEN_EXT
152 CONF_CELLULAR_INTERFACE,
154 CONF_USE_GATEWAY_TIMESERVER,
159 static GKeyFile *load_config(const char *file)
164 keyfile = g_key_file_new();
166 g_key_file_set_list_separator(keyfile, ',');
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,
175 g_key_file_free(keyfile);
182 static uint *parse_service_types(char **str_list, gsize len)
184 unsigned int *type_list;
186 enum connman_service_type type;
188 type_list = g_try_new0(unsigned int, len + 1);
194 while (str_list[i]) {
195 type = __connman_service_string2type(str_list[i]);
197 if (type != CONNMAN_SERVICE_TYPE_UNKNOWN) {
204 type_list[j] = CONNMAN_SERVICE_TYPE_UNKNOWN;
209 static char **parse_fallback_nameservers(char **nameservers, gsize len)
214 servers = g_try_new0(char *, len + 1);
220 while (nameservers[i]) {
221 if (connman_inet_check_ipaddress(nameservers[i]) > 0) {
222 servers[j] = g_strdup(nameservers[i]);
231 static void check_config(GKeyFile *config)
239 keys = g_key_file_get_groups(config, NULL);
241 for (j = 0; keys && keys[j]; j++) {
242 if (g_strcmp0(keys[j], "General") != 0)
243 connman_warn("Unknown group %s in %s",
249 keys = g_key_file_get_keys(config, "General", NULL, NULL);
251 for (j = 0; keys && keys[j]; j++) {
256 for (i = 0; supported_options[i]; i++) {
257 if (g_strcmp0(keys[j], supported_options[i]) == 0) {
262 if (!found && !supported_options[i])
263 connman_warn("Unknown option %s in %s",
270 #if defined TIZEN_EXT
271 static void check_Tizen_configuration(GKeyFile *config)
273 GError *error = NULL;
274 char **cellular_interfaces;
278 cellular_interfaces = g_key_file_get_string_list(config, "General",
279 CONF_CELLULAR_INTERFACE, &len, &error);
282 connman_settings.cellular_interfaces = cellular_interfaces;
284 g_clear_error(&error);
286 boolean = __connman_config_get_bool(config, "General",
287 CONF_TIZEN_TV_EXT, &error);
289 connman_settings.tizen_tv_extension = boolean;
291 g_clear_error(&error);
293 boolean = __connman_config_get_bool(config, "General",
294 CONF_USE_GATEWAY_TIMESERVER, &error);
296 connman_settings.use_gateway_timeserver = boolean;
298 g_clear_error(&error);
301 static void set_nofile_inc(void)
306 rlim.rlim_cur = 8192;
307 rlim.rlim_max = 8192;
309 err = setrlimit(RLIMIT_NOFILE, &rlim);
311 DBG("fail to increase FILENO err(%d)", err);
317 static void parse_config(GKeyFile *config)
319 GError *error = NULL;
325 char *vendor_class_id;
330 connman_settings.auto_connect =
331 parse_service_types(default_auto_connect, 3);
332 connman_settings.blacklisted_interfaces =
333 g_strdupv(default_blacklist);
337 DBG("parsing %s", MAINFILE);
339 boolean = g_key_file_get_boolean(config, "General",
340 CONF_BG_SCAN, &error);
342 connman_settings.bg_scan = boolean;
344 g_clear_error(&error);
346 timeservers = __connman_config_get_string_list(config, "General",
347 CONF_PREF_TIMESERVERS, NULL, &error);
349 connman_settings.pref_timeservers = timeservers;
351 g_clear_error(&error);
353 str_list = __connman_config_get_string_list(config, "General",
354 CONF_AUTO_CONNECT, &len, &error);
357 connman_settings.auto_connect =
358 parse_service_types(str_list, len);
360 connman_settings.auto_connect =
361 parse_service_types(default_auto_connect, 3);
363 g_strfreev(str_list);
365 g_clear_error(&error);
367 str_list = __connman_config_get_string_list(config, "General",
368 CONF_PREFERRED_TECHS, &len, &error);
371 connman_settings.preferred_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_ALWAYS_CONNECTED_TECHS, &len, &error);
382 connman_settings.always_connected_techs =
383 parse_service_types(str_list, len);
385 g_strfreev(str_list);
387 g_clear_error(&error);
389 str_list = __connman_config_get_string_list(config, "General",
390 CONF_FALLBACK_NAMESERVERS, &len, &error);
393 connman_settings.fallback_nameservers =
394 parse_fallback_nameservers(str_list, len);
396 g_strfreev(str_list);
398 g_clear_error(&error);
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;
405 g_clear_error(&error);
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;
412 g_clear_error(&error);
414 interfaces = __connman_config_get_string_list(config, "General",
415 CONF_BLACKLISTED_INTERFACES, &len, &error);
418 connman_settings.blacklisted_interfaces = interfaces;
420 connman_settings.blacklisted_interfaces =
421 g_strdupv(default_blacklist);
423 g_clear_error(&error);
425 boolean = __connman_config_get_bool(config, "General",
426 CONF_ALLOW_HOSTNAME_UPDATES,
429 connman_settings.allow_hostname_updates = boolean;
431 g_clear_error(&error);
433 boolean = __connman_config_get_bool(config, "General",
434 CONF_SINGLE_TECH, &error);
436 connman_settings.single_tech = boolean;
438 g_clear_error(&error);
440 tethering = __connman_config_get_string_list(config, "General",
441 CONF_TETHERING_TECHNOLOGIES, &len, &error);
444 connman_settings.tethering_technologies = tethering;
446 g_clear_error(&error);
448 boolean = __connman_config_get_bool(config, "General",
449 CONF_PERSISTENT_TETHERING_MODE,
452 connman_settings.persistent_tethering_mode = boolean;
454 g_clear_error(&error);
456 boolean = __connman_config_get_bool(config, "General",
457 CONF_ENABLE_6TO4, &error);
459 connman_settings.enable_6to4 = boolean;
461 g_clear_error(&error);
463 vendor_class_id = __connman_config_get_string(config, "General",
464 CONF_VENDOR_CLASS_ID, &error);
466 connman_settings.vendor_class_id = vendor_class_id;
468 g_clear_error(&error);
470 boolean = __connman_config_get_bool(config, "General",
471 CONF_ENABLE_ONLINE_CHECK, &error);
473 connman_settings.enable_online_check = boolean;
475 connman_info("Online check disabled by main config.");
478 g_clear_error(&error);
480 #if defined TIZEN_EXT
481 check_Tizen_configuration(config);
485 static int config_init(const char *file)
489 #if defined TIZEN_EXT
492 config = load_config(file);
493 check_config(config);
494 parse_config(config);
496 g_key_file_free(config);
501 static GMainLoop *main_loop = NULL;
503 static unsigned int __terminated = 0;
505 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
508 struct signalfd_siginfo si;
512 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
515 fd = g_io_channel_unix_get_fd(channel);
517 result = read(fd, &si, sizeof(si));
518 if (result != sizeof(si))
521 switch (si.ssi_signo) {
524 if (__terminated == 0) {
525 connman_info("Terminating");
526 g_main_loop_quit(main_loop);
536 static guint setup_signalfd(void)
544 sigaddset(&mask, SIGINT);
545 sigaddset(&mask, SIGTERM);
547 if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
548 perror("Failed to set signal mask");
552 fd = signalfd(-1, &mask, 0);
554 perror("Failed to create signal descriptor");
558 channel = g_io_channel_unix_new(fd);
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);
564 source = g_io_add_watch(channel,
565 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
566 signal_handler, NULL);
568 g_io_channel_unref(channel);
573 static void disconnect_callback(DBusConnection *conn, void *user_data)
575 connman_error("D-Bus disconnect");
577 g_main_loop_quit(main_loop);
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;
592 static bool parse_debug(const char *key, const char *value,
593 gpointer user_data, GError **error)
597 char *prev = option_debug;
599 option_debug = g_strconcat(prev, ",", value, NULL);
602 option_debug = g_strdup(value);
605 g_free(option_debug);
606 option_debug = g_strdup("*");
612 static bool parse_noplugin(const char *key, const char *value,
613 gpointer user_data, GError **error)
615 if (option_noplugin) {
616 char *prev = option_noplugin;
618 option_noplugin = g_strconcat(prev, ",", value, NULL);
621 option_noplugin = g_strdup(value);
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" },
658 const char *connman_option_get_string(const char *key)
660 if (g_str_equal(key, CONF_VENDOR_CLASS_ID))
661 return connman_settings.vendor_class_id;
663 if (g_strcmp0(key, "wifi") == 0) {
665 return "nl80211,wext";
673 bool connman_setting_get_bool(const char *key)
675 if (g_str_equal(key, CONF_BG_SCAN))
676 return connman_settings.bg_scan;
678 if (g_str_equal(key, CONF_ALLOW_HOSTNAME_UPDATES))
679 return connman_settings.allow_hostname_updates;
681 if (g_str_equal(key, CONF_SINGLE_TECH))
682 return connman_settings.single_tech;
684 if (g_str_equal(key, CONF_PERSISTENT_TETHERING_MODE))
685 return connman_settings.persistent_tethering_mode;
687 if (g_str_equal(key, CONF_ENABLE_6TO4))
688 return connman_settings.enable_6to4;
690 if (g_str_equal(key, CONF_ENABLE_ONLINE_CHECK))
691 return connman_settings.enable_online_check;
693 #if defined TIZEN_EXT
694 if (g_str_equal(key, CONF_USE_GATEWAY_TIMESERVER))
695 return connman_settings.use_gateway_timeserver;
701 char **connman_setting_get_string_list(const char *key)
703 if (g_str_equal(key, CONF_PREF_TIMESERVERS))
704 return connman_settings.pref_timeservers;
706 if (g_str_equal(key, CONF_FALLBACK_NAMESERVERS))
707 return connman_settings.fallback_nameservers;
709 if (g_str_equal(key, CONF_BLACKLISTED_INTERFACES))
710 return connman_settings.blacklisted_interfaces;
712 if (g_str_equal(key, CONF_TETHERING_TECHNOLOGIES))
713 return connman_settings.tethering_technologies;
715 #if defined TIZEN_EXT
716 if (g_str_equal(key, CONF_CELLULAR_INTERFACE))
717 return connman_settings.cellular_interfaces;
723 unsigned int *connman_setting_get_uint_list(const char *key)
725 if (g_str_equal(key, CONF_AUTO_CONNECT))
726 return connman_settings.auto_connect;
728 if (g_str_equal(key, CONF_PREFERRED_TECHS))
729 return connman_settings.preferred_techs;
731 if (g_str_equal(key, CONF_ALWAYS_CONNECTED_TECHS))
732 return connman_settings.always_connected_techs;
737 unsigned int connman_timeout_input_request(void)
739 return connman_settings.timeout_inputreq;
742 unsigned int connman_timeout_browser_launch(void)
744 return connman_settings.timeout_browserlaunch;
747 int main(int argc, char *argv[])
749 GOptionContext *context;
750 GError *error = NULL;
751 DBusConnection *conn;
755 context = g_option_context_new(NULL);
756 g_option_context_add_main_entries(context, options, NULL);
758 if (!g_option_context_parse(context, &argc, &argv, &error)) {
760 g_printerr("%s\n", error->message);
763 g_printerr("An unknown error occurred\n");
767 g_option_context_free(context);
769 if (option_version) {
770 printf("%s\n", VERSION);
776 perror("Can't start daemon");
781 if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
782 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
784 perror("Failed to create storage directory");
789 main_loop = g_main_loop_new(NULL, FALSE);
791 signal = setup_signalfd();
793 dbus_error_init(&err);
795 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
797 if (dbus_error_is_set(&err)) {
798 fprintf(stderr, "%s\n", err.message);
799 dbus_error_free(&err);
801 fprintf(stderr, "Can't register with system bus\n");
805 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
807 __connman_log_init(argv[0], option_debug, option_detach,
808 option_backtrace, "Connection Manager", VERSION);
810 __connman_dbus_init(conn);
813 config_init(CONFIGMAINFILE);
815 config_init(option_config);
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);
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();
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();
851 __connman_plugin_init(option_plugin, option_noplugin);
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();
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);
870 g_main_loop_run(main_loop);
872 g_source_remove(signal);
874 #if !defined TIZEN_EXT
875 __connman_machine_cleanup();
876 __connman_rfkill_cleanup();
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();
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();
916 __connman_util_cleanup();
917 __connman_dbus_cleanup();
919 __connman_log_cleanup(option_backtrace);
921 dbus_connection_unref(conn);
923 g_main_loop_unref(main_loop);
925 if (connman_settings.pref_timeservers)
926 g_strfreev(connman_settings.pref_timeservers);
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);
934 g_free(option_debug);