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[] = {
67 char **pref_timeservers;
68 unsigned int *auto_connect;
69 unsigned int *preferred_techs;
70 char **fallback_nameservers;
71 unsigned int timeout_inputreq;
72 unsigned int timeout_browserlaunch;
73 char **blacklisted_interfaces;
74 bool allow_hostname_updates;
76 char **tethering_technologies;
77 bool persistent_tethering_mode;
80 char **cellular_interfaces;
81 bool tizen_tv_extension;
83 } connman_settings = {
85 .pref_timeservers = NULL,
87 .preferred_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,
98 .cellular_interfaces = NULL,
99 .tizen_tv_extension = false,
103 #define CONF_BG_SCAN "BackgroundScanning"
104 #define CONF_PREF_TIMESERVERS "FallbackTimeservers"
105 #define CONF_AUTO_CONNECT "DefaultAutoConnectTechnologies"
106 #define CONF_PREFERRED_TECHS "PreferredTechnologies"
107 #define CONF_FALLBACK_NAMESERVERS "FallbackNameservers"
108 #define CONF_TIMEOUT_INPUTREQ "InputRequestTimeout"
109 #define CONF_TIMEOUT_BROWSERLAUNCH "BrowserLaunchTimeout"
110 #define CONF_BLACKLISTED_INTERFACES "NetworkInterfaceBlacklist"
111 #define CONF_ALLOW_HOSTNAME_UPDATES "AllowHostnameUpdates"
112 #define CONF_SINGLE_TECH "SingleConnectedTechnology"
113 #define CONF_TETHERING_TECHNOLOGIES "TetheringTechnologies"
114 #define CONF_PERSISTENT_TETHERING_MODE "PersistentTetheringMode"
115 #define CONF_ENABLE_6TO4 "Enable6to4"
116 #if defined TIZEN_EXT
117 #define CONF_CELLULAR_INTERFACE "NetworkCellularInterfaceList"
118 #define CONF_TIZEN_TV_EXT "TizenTVExtension"
121 static const char *supported_options[] = {
123 CONF_PREF_TIMESERVERS,
125 CONF_PREFERRED_TECHS,
126 CONF_FALLBACK_NAMESERVERS,
127 CONF_TIMEOUT_INPUTREQ,
128 CONF_TIMEOUT_BROWSERLAUNCH,
129 CONF_BLACKLISTED_INTERFACES,
130 CONF_ALLOW_HOSTNAME_UPDATES,
132 CONF_TETHERING_TECHNOLOGIES,
133 CONF_PERSISTENT_TETHERING_MODE,
135 #if defined TIZEN_EXT
136 CONF_CELLULAR_INTERFACE,
142 static GKeyFile *load_config(const char *file)
147 keyfile = g_key_file_new();
149 g_key_file_set_list_separator(keyfile, ',');
151 if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
152 if (err->code != G_FILE_ERROR_NOENT) {
153 connman_error("Parsing %s failed: %s", file,
158 g_key_file_free(keyfile);
165 static uint *parse_service_types(char **str_list, gsize len)
167 unsigned int *type_list;
169 enum connman_service_type type;
171 type_list = g_try_new0(unsigned int, len + 1);
177 while (str_list[i]) {
178 type = __connman_service_string2type(str_list[i]);
180 if (type != CONNMAN_SERVICE_TYPE_UNKNOWN) {
187 type_list[j] = CONNMAN_SERVICE_TYPE_UNKNOWN;
192 static char **parse_fallback_nameservers(char **nameservers, gsize len)
197 servers = g_try_new0(char *, len + 1);
203 while (nameservers[i]) {
204 if (connman_inet_check_ipaddress(nameservers[i]) > 0) {
205 servers[j] = g_strdup(nameservers[i]);
214 static void check_config(GKeyFile *config)
222 keys = g_key_file_get_groups(config, NULL);
224 for (j = 0; keys && keys[j]; j++) {
225 if (g_strcmp0(keys[j], "General") != 0)
226 connman_warn("Unknown group %s in %s",
232 keys = g_key_file_get_keys(config, "General", NULL, NULL);
234 for (j = 0; keys && keys[j]; j++) {
239 for (i = 0; supported_options[i]; i++) {
240 if (g_strcmp0(keys[j], supported_options[i]) == 0) {
245 if (!found && !supported_options[i])
246 connman_warn("Unknown option %s in %s",
253 #if defined TIZEN_EXT
254 static void check_Tizen_configuration(GKeyFile *config)
256 GError *error = NULL;
257 char **cellular_interfaces;
261 cellular_interfaces = g_key_file_get_string_list(config, "General",
262 CONF_CELLULAR_INTERFACE, &len, &error);
265 connman_settings.cellular_interfaces = cellular_interfaces;
267 g_clear_error(&error);
269 boolean = __connman_config_get_bool(config, "General",
270 CONF_TIZEN_TV_EXT, &error);
272 connman_settings.tizen_tv_extension = boolean;
274 g_clear_error(&error);
277 static void set_nofile_inc(void)
282 rlim.rlim_cur = 8192;
283 rlim.rlim_max = 8192;
285 err = setrlimit(RLIMIT_NOFILE, &rlim);
287 DBG("fail to increase FILENO err(%d)", err);
293 static void parse_config(GKeyFile *config)
295 GError *error = NULL;
305 connman_settings.auto_connect =
306 parse_service_types(default_auto_connect, 3);
307 connman_settings.blacklisted_interfaces =
308 g_strdupv(default_blacklist);
312 DBG("parsing %s", MAINFILE);
314 boolean = g_key_file_get_boolean(config, "General",
315 CONF_BG_SCAN, &error);
317 connman_settings.bg_scan = boolean;
319 g_clear_error(&error);
321 timeservers = __connman_config_get_string_list(config, "General",
322 CONF_PREF_TIMESERVERS, NULL, &error);
324 connman_settings.pref_timeservers = timeservers;
326 g_clear_error(&error);
328 str_list = __connman_config_get_string_list(config, "General",
329 CONF_AUTO_CONNECT, &len, &error);
332 connman_settings.auto_connect =
333 parse_service_types(str_list, len);
335 connman_settings.auto_connect =
336 parse_service_types(default_auto_connect, 3);
338 g_strfreev(str_list);
340 g_clear_error(&error);
342 str_list = __connman_config_get_string_list(config, "General",
343 CONF_PREFERRED_TECHS, &len, &error);
346 connman_settings.preferred_techs =
347 parse_service_types(str_list, len);
349 g_strfreev(str_list);
351 g_clear_error(&error);
353 str_list = __connman_config_get_string_list(config, "General",
354 CONF_FALLBACK_NAMESERVERS, &len, &error);
357 connman_settings.fallback_nameservers =
358 parse_fallback_nameservers(str_list, len);
360 g_strfreev(str_list);
362 g_clear_error(&error);
364 timeout = g_key_file_get_integer(config, "General",
365 CONF_TIMEOUT_INPUTREQ, &error);
366 if (!error && timeout >= 0)
367 connman_settings.timeout_inputreq = timeout * 1000;
369 g_clear_error(&error);
371 timeout = g_key_file_get_integer(config, "General",
372 CONF_TIMEOUT_BROWSERLAUNCH, &error);
373 if (!error && timeout >= 0)
374 connman_settings.timeout_browserlaunch = timeout * 1000;
376 g_clear_error(&error);
378 interfaces = __connman_config_get_string_list(config, "General",
379 CONF_BLACKLISTED_INTERFACES, &len, &error);
382 connman_settings.blacklisted_interfaces = interfaces;
384 connman_settings.blacklisted_interfaces =
385 g_strdupv(default_blacklist);
387 g_clear_error(&error);
389 boolean = __connman_config_get_bool(config, "General",
390 CONF_ALLOW_HOSTNAME_UPDATES,
393 connman_settings.allow_hostname_updates = boolean;
395 g_clear_error(&error);
397 boolean = __connman_config_get_bool(config, "General",
398 CONF_SINGLE_TECH, &error);
400 connman_settings.single_tech = boolean;
402 g_clear_error(&error);
404 tethering = __connman_config_get_string_list(config, "General",
405 CONF_TETHERING_TECHNOLOGIES, &len, &error);
408 connman_settings.tethering_technologies = tethering;
410 g_clear_error(&error);
412 boolean = __connman_config_get_bool(config, "General",
413 CONF_PERSISTENT_TETHERING_MODE,
416 connman_settings.persistent_tethering_mode = boolean;
418 g_clear_error(&error);
420 boolean = __connman_config_get_bool(config, "General",
421 CONF_ENABLE_6TO4, &error);
423 connman_settings.enable_6to4 = boolean;
425 g_clear_error(&error);
427 #if defined TIZEN_EXT
428 check_Tizen_configuration(config);
432 static int config_init(const char *file)
436 #if defined TIZEN_EXT
439 config = load_config(file);
440 check_config(config);
441 parse_config(config);
443 g_key_file_free(config);
448 static GMainLoop *main_loop = NULL;
450 static unsigned int __terminated = 0;
452 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
455 struct signalfd_siginfo si;
459 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
462 fd = g_io_channel_unix_get_fd(channel);
464 result = read(fd, &si, sizeof(si));
465 if (result != sizeof(si))
468 switch (si.ssi_signo) {
471 if (__terminated == 0) {
472 connman_info("Terminating");
473 g_main_loop_quit(main_loop);
483 static guint setup_signalfd(void)
491 sigaddset(&mask, SIGINT);
492 sigaddset(&mask, SIGTERM);
494 if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
495 perror("Failed to set signal mask");
499 fd = signalfd(-1, &mask, 0);
501 perror("Failed to create signal descriptor");
505 channel = g_io_channel_unix_new(fd);
507 g_io_channel_set_close_on_unref(channel, TRUE);
508 g_io_channel_set_encoding(channel, NULL, NULL);
509 g_io_channel_set_buffered(channel, FALSE);
511 source = g_io_add_watch(channel,
512 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
513 signal_handler, NULL);
515 g_io_channel_unref(channel);
520 static void disconnect_callback(DBusConnection *conn, void *user_data)
522 connman_error("D-Bus disconnect");
524 g_main_loop_quit(main_loop);
527 static gchar *option_config = NULL;
528 static gchar *option_debug = NULL;
529 static gchar *option_device = NULL;
530 static gchar *option_plugin = NULL;
531 static gchar *option_nodevice = NULL;
532 static gchar *option_noplugin = NULL;
533 static gchar *option_wifi = NULL;
534 static gboolean option_detach = TRUE;
535 static gboolean option_dnsproxy = TRUE;
536 static gboolean option_backtrace = TRUE;
537 static gboolean option_version = FALSE;
539 static bool parse_debug(const char *key, const char *value,
540 gpointer user_data, GError **error)
543 option_debug = g_strdup(value);
545 option_debug = g_strdup("*");
550 static GOptionEntry options[] = {
551 { "config", 'c', 0, G_OPTION_ARG_STRING, &option_config,
552 "Load the specified configuration file "
553 "instead of " CONFIGMAINFILE, "FILE" },
554 { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
555 G_OPTION_ARG_CALLBACK, parse_debug,
556 "Specify debug options to enable", "DEBUG" },
557 { "device", 'i', 0, G_OPTION_ARG_STRING, &option_device,
558 "Specify networking device or interface", "DEV" },
559 { "nodevice", 'I', 0, G_OPTION_ARG_STRING, &option_nodevice,
560 "Specify networking interface to ignore", "DEV" },
561 { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
562 "Specify plugins to load", "NAME,..." },
563 { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
564 "Specify plugins not to load", "NAME,..." },
565 { "wifi", 'W', 0, G_OPTION_ARG_STRING, &option_wifi,
566 "Specify driver for WiFi/Supplicant", "NAME" },
567 { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
568 G_OPTION_ARG_NONE, &option_detach,
569 "Don't fork daemon to background" },
570 { "nodnsproxy", 'r', G_OPTION_FLAG_REVERSE,
571 G_OPTION_ARG_NONE, &option_dnsproxy,
572 "Don't enable DNS Proxy" },
573 { "nobacktrace", 0, G_OPTION_FLAG_REVERSE,
574 G_OPTION_ARG_NONE, &option_backtrace,
575 "Don't print out backtrace information" },
576 { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
577 "Show version information and exit" },
581 const char *connman_option_get_string(const char *key)
583 if (g_strcmp0(key, "wifi") == 0) {
585 return "nl80211,wext";
593 bool connman_setting_get_bool(const char *key)
595 if (g_str_equal(key, CONF_BG_SCAN))
596 return connman_settings.bg_scan;
598 if (g_str_equal(key, CONF_ALLOW_HOSTNAME_UPDATES))
599 return connman_settings.allow_hostname_updates;
601 if (g_str_equal(key, CONF_SINGLE_TECH))
602 return connman_settings.single_tech;
604 if (g_str_equal(key, CONF_PERSISTENT_TETHERING_MODE))
605 return connman_settings.persistent_tethering_mode;
607 if (g_str_equal(key, CONF_ENABLE_6TO4))
608 return connman_settings.enable_6to4;
613 char **connman_setting_get_string_list(const char *key)
615 if (g_str_equal(key, CONF_PREF_TIMESERVERS))
616 return connman_settings.pref_timeservers;
618 if (g_str_equal(key, CONF_FALLBACK_NAMESERVERS))
619 return connman_settings.fallback_nameservers;
621 if (g_str_equal(key, CONF_BLACKLISTED_INTERFACES))
622 return connman_settings.blacklisted_interfaces;
624 if (g_str_equal(key, CONF_TETHERING_TECHNOLOGIES))
625 return connman_settings.tethering_technologies;
627 #if defined TIZEN_EXT
628 if (g_str_equal(key, CONF_CELLULAR_INTERFACE))
629 return connman_settings.cellular_interfaces;
635 unsigned int *connman_setting_get_uint_list(const char *key)
637 if (g_str_equal(key, CONF_AUTO_CONNECT))
638 return connman_settings.auto_connect;
640 if (g_str_equal(key, CONF_PREFERRED_TECHS))
641 return connman_settings.preferred_techs;
646 unsigned int connman_timeout_input_request(void)
648 return connman_settings.timeout_inputreq;
651 unsigned int connman_timeout_browser_launch(void)
653 return connman_settings.timeout_browserlaunch;
656 int main(int argc, char *argv[])
658 GOptionContext *context;
659 GError *error = NULL;
660 DBusConnection *conn;
664 context = g_option_context_new(NULL);
665 g_option_context_add_main_entries(context, options, NULL);
667 if (!g_option_context_parse(context, &argc, &argv, &error)) {
669 g_printerr("%s\n", error->message);
672 g_printerr("An unknown error occurred\n");
676 g_option_context_free(context);
678 if (option_version) {
679 printf("%s\n", VERSION);
685 perror("Can't start daemon");
690 if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
691 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
693 perror("Failed to create storage directory");
698 main_loop = g_main_loop_new(NULL, FALSE);
700 signal = setup_signalfd();
702 dbus_error_init(&err);
704 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
706 if (dbus_error_is_set(&err)) {
707 fprintf(stderr, "%s\n", err.message);
708 dbus_error_free(&err);
710 fprintf(stderr, "Can't register with system bus\n");
714 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
716 __connman_log_init(argv[0], option_debug, option_detach,
717 option_backtrace, "Connection Manager", VERSION);
719 __connman_dbus_init(conn);
722 config_init(CONFIGMAINFILE);
724 config_init(option_config);
726 __connman_util_init();
727 __connman_inotify_init();
728 __connman_technology_init();
729 __connman_notifier_init();
730 __connman_agent_init();
731 __connman_service_init();
732 __connman_peer_service_init();
733 __connman_peer_init();
734 __connman_provider_init();
735 __connman_network_init();
736 __connman_config_init();
737 __connman_device_init(option_device, option_nodevice);
739 __connman_ippool_init();
740 __connman_iptables_init();
741 __connman_firewall_init();
742 __connman_nat_init();
743 __connman_tethering_init();
744 __connman_counter_init();
745 __connman_manager_init();
746 __connman_stats_init();
747 __connman_clock_init();
749 __connman_resolver_init(option_dnsproxy);
750 __connman_ipconfig_init();
751 __connman_rtnl_init();
752 __connman_task_init();
753 __connman_proxy_init();
754 __connman_detect_init();
755 __connman_session_init();
756 __connman_timeserver_init();
757 __connman_connection_init();
759 __connman_plugin_init(option_plugin, option_noplugin);
761 __connman_rtnl_start();
762 __connman_dhcp_init();
763 __connman_dhcpv6_init();
764 __connman_wpad_init();
765 __connman_wispr_init();
766 #if !defined TIZEN_EXT
767 __connman_rfkill_init();
768 __connman_machine_init();
771 g_free(option_config);
772 g_free(option_device);
773 g_free(option_plugin);
774 g_free(option_nodevice);
775 g_free(option_noplugin);
777 g_main_loop_run(main_loop);
779 g_source_remove(signal);
781 #if !defined TIZEN_EXT
782 __connman_machine_cleanup();
783 __connman_rfkill_cleanup();
785 __connman_wispr_cleanup();
786 __connman_wpad_cleanup();
787 __connman_dhcpv6_cleanup();
788 __connman_session_cleanup();
789 __connman_plugin_cleanup();
790 __connman_provider_cleanup();
791 __connman_connection_cleanup();
792 __connman_timeserver_cleanup();
793 __connman_detect_cleanup();
794 __connman_proxy_cleanup();
795 __connman_task_cleanup();
796 __connman_rtnl_cleanup();
797 __connman_resolver_cleanup();
799 __connman_clock_cleanup();
800 __connman_stats_cleanup();
801 __connman_config_cleanup();
802 __connman_manager_cleanup();
803 __connman_counter_cleanup();
804 __connman_tethering_cleanup();
805 __connman_nat_cleanup();
806 __connman_firewall_cleanup();
807 __connman_iptables_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);