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;
82 } connman_settings = {
84 .pref_timeservers = NULL,
86 .preferred_techs = NULL,
87 .fallback_nameservers = NULL,
88 .timeout_inputreq = DEFAULT_INPUT_REQUEST_TIMEOUT,
89 .timeout_browserlaunch = DEFAULT_BROWSER_LAUNCH_TIMEOUT,
90 .blacklisted_interfaces = NULL,
91 .allow_hostname_updates = true,
93 .tethering_technologies = NULL,
94 .persistent_tethering_mode = false,
97 .cellular_interfaces = NULL,
101 #define CONF_BG_SCAN "BackgroundScanning"
102 #define CONF_PREF_TIMESERVERS "FallbackTimeservers"
103 #define CONF_AUTO_CONNECT "DefaultAutoConnectTechnologies"
104 #define CONF_PREFERRED_TECHS "PreferredTechnologies"
105 #define CONF_FALLBACK_NAMESERVERS "FallbackNameservers"
106 #define CONF_TIMEOUT_INPUTREQ "InputRequestTimeout"
107 #define CONF_TIMEOUT_BROWSERLAUNCH "BrowserLaunchTimeout"
108 #define CONF_BLACKLISTED_INTERFACES "NetworkInterfaceBlacklist"
109 #define CONF_ALLOW_HOSTNAME_UPDATES "AllowHostnameUpdates"
110 #define CONF_SINGLE_TECH "SingleConnectedTechnology"
111 #define CONF_TETHERING_TECHNOLOGIES "TetheringTechnologies"
112 #define CONF_PERSISTENT_TETHERING_MODE "PersistentTetheringMode"
113 #define CONF_ENABLE_6TO4 "Enable6to4"
114 #if defined TIZEN_EXT
115 #define CONF_CELLULAR_INTERFACE "NetworkCellularInterfaceList"
118 static const char *supported_options[] = {
120 CONF_PREF_TIMESERVERS,
122 CONF_PREFERRED_TECHS,
123 CONF_FALLBACK_NAMESERVERS,
124 CONF_TIMEOUT_INPUTREQ,
125 CONF_TIMEOUT_BROWSERLAUNCH,
126 CONF_BLACKLISTED_INTERFACES,
127 CONF_ALLOW_HOSTNAME_UPDATES,
129 CONF_TETHERING_TECHNOLOGIES,
130 CONF_PERSISTENT_TETHERING_MODE,
132 #if defined TIZEN_EXT
133 CONF_CELLULAR_INTERFACE,
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 #if defined TIZEN_EXT
250 static void check_Tizen_configuration(GKeyFile *config)
252 GError *error = NULL;
253 char **cellular_interfaces;
256 cellular_interfaces = g_key_file_get_string_list(config, "General",
257 CONF_CELLULAR_INTERFACE, &len, &error);
260 connman_settings.cellular_interfaces = cellular_interfaces;
262 g_clear_error(&error);
265 static void set_nofile_inc(void)
270 rlim.rlim_cur = 8192;
271 rlim.rlim_max = 8192;
273 err = setrlimit(RLIMIT_NOFILE, &rlim);
275 DBG("fail to increase FILENO err(%d)", err);
281 static void parse_config(GKeyFile *config)
283 GError *error = NULL;
293 connman_settings.auto_connect =
294 parse_service_types(default_auto_connect, 3);
295 connman_settings.blacklisted_interfaces =
296 g_strdupv(default_blacklist);
300 DBG("parsing %s", MAINFILE);
302 boolean = g_key_file_get_boolean(config, "General",
303 CONF_BG_SCAN, &error);
305 connman_settings.bg_scan = boolean;
307 g_clear_error(&error);
309 timeservers = __connman_config_get_string_list(config, "General",
310 CONF_PREF_TIMESERVERS, NULL, &error);
312 connman_settings.pref_timeservers = timeservers;
314 g_clear_error(&error);
316 str_list = __connman_config_get_string_list(config, "General",
317 CONF_AUTO_CONNECT, &len, &error);
320 connman_settings.auto_connect =
321 parse_service_types(str_list, len);
323 connman_settings.auto_connect =
324 parse_service_types(default_auto_connect, 3);
326 g_strfreev(str_list);
328 g_clear_error(&error);
330 str_list = __connman_config_get_string_list(config, "General",
331 CONF_PREFERRED_TECHS, &len, &error);
334 connman_settings.preferred_techs =
335 parse_service_types(str_list, len);
337 g_strfreev(str_list);
339 g_clear_error(&error);
341 str_list = __connman_config_get_string_list(config, "General",
342 CONF_FALLBACK_NAMESERVERS, &len, &error);
345 connman_settings.fallback_nameservers =
346 parse_fallback_nameservers(str_list, len);
348 g_strfreev(str_list);
350 g_clear_error(&error);
352 timeout = g_key_file_get_integer(config, "General",
353 CONF_TIMEOUT_INPUTREQ, &error);
354 if (!error && timeout >= 0)
355 connman_settings.timeout_inputreq = timeout * 1000;
357 g_clear_error(&error);
359 timeout = g_key_file_get_integer(config, "General",
360 CONF_TIMEOUT_BROWSERLAUNCH, &error);
361 if (!error && timeout >= 0)
362 connman_settings.timeout_browserlaunch = timeout * 1000;
364 g_clear_error(&error);
366 interfaces = __connman_config_get_string_list(config, "General",
367 CONF_BLACKLISTED_INTERFACES, &len, &error);
370 connman_settings.blacklisted_interfaces = interfaces;
372 connman_settings.blacklisted_interfaces =
373 g_strdupv(default_blacklist);
375 g_clear_error(&error);
377 boolean = __connman_config_get_bool(config, "General",
378 CONF_ALLOW_HOSTNAME_UPDATES,
381 connman_settings.allow_hostname_updates = boolean;
383 g_clear_error(&error);
385 boolean = __connman_config_get_bool(config, "General",
386 CONF_SINGLE_TECH, &error);
388 connman_settings.single_tech = boolean;
390 g_clear_error(&error);
392 tethering = __connman_config_get_string_list(config, "General",
393 CONF_TETHERING_TECHNOLOGIES, &len, &error);
396 connman_settings.tethering_technologies = tethering;
398 g_clear_error(&error);
400 boolean = __connman_config_get_bool(config, "General",
401 CONF_PERSISTENT_TETHERING_MODE,
404 connman_settings.persistent_tethering_mode = boolean;
406 g_clear_error(&error);
408 boolean = __connman_config_get_bool(config, "General",
409 CONF_ENABLE_6TO4, &error);
411 connman_settings.enable_6to4 = boolean;
413 g_clear_error(&error);
415 #if defined TIZEN_EXT
416 check_Tizen_configuration(config);
420 static int config_init(const char *file)
424 #if defined TIZEN_EXT
427 config = load_config(file);
428 check_config(config);
429 parse_config(config);
431 g_key_file_free(config);
436 static GMainLoop *main_loop = NULL;
438 static unsigned int __terminated = 0;
440 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
443 struct signalfd_siginfo si;
447 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
450 fd = g_io_channel_unix_get_fd(channel);
452 result = read(fd, &si, sizeof(si));
453 if (result != sizeof(si))
456 switch (si.ssi_signo) {
459 if (__terminated == 0) {
460 connman_info("Terminating");
461 g_main_loop_quit(main_loop);
471 static guint setup_signalfd(void)
479 sigaddset(&mask, SIGINT);
480 sigaddset(&mask, SIGTERM);
482 if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
483 perror("Failed to set signal mask");
487 fd = signalfd(-1, &mask, 0);
489 perror("Failed to create signal descriptor");
493 channel = g_io_channel_unix_new(fd);
495 g_io_channel_set_close_on_unref(channel, TRUE);
496 g_io_channel_set_encoding(channel, NULL, NULL);
497 g_io_channel_set_buffered(channel, FALSE);
499 source = g_io_add_watch(channel,
500 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
501 signal_handler, NULL);
503 g_io_channel_unref(channel);
508 static void disconnect_callback(DBusConnection *conn, void *user_data)
510 connman_error("D-Bus disconnect");
512 g_main_loop_quit(main_loop);
515 static gchar *option_config = NULL;
516 static gchar *option_debug = NULL;
517 static gchar *option_device = NULL;
518 static gchar *option_plugin = NULL;
519 static gchar *option_nodevice = NULL;
520 static gchar *option_noplugin = NULL;
521 static gchar *option_wifi = NULL;
522 static gboolean option_detach = TRUE;
523 static gboolean option_dnsproxy = TRUE;
524 static gboolean option_backtrace = TRUE;
525 static gboolean option_version = FALSE;
527 static bool parse_debug(const char *key, const char *value,
528 gpointer user_data, GError **error)
531 option_debug = g_strdup(value);
533 option_debug = g_strdup("*");
538 static GOptionEntry options[] = {
539 { "config", 'c', 0, G_OPTION_ARG_STRING, &option_config,
540 "Load the specified configuration file "
541 "instead of " CONFIGMAINFILE, "FILE" },
542 { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
543 G_OPTION_ARG_CALLBACK, parse_debug,
544 "Specify debug options to enable", "DEBUG" },
545 { "device", 'i', 0, G_OPTION_ARG_STRING, &option_device,
546 "Specify networking device or interface", "DEV" },
547 { "nodevice", 'I', 0, G_OPTION_ARG_STRING, &option_nodevice,
548 "Specify networking interface to ignore", "DEV" },
549 { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
550 "Specify plugins to load", "NAME,..." },
551 { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
552 "Specify plugins not to load", "NAME,..." },
553 { "wifi", 'W', 0, G_OPTION_ARG_STRING, &option_wifi,
554 "Specify driver for WiFi/Supplicant", "NAME" },
555 { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
556 G_OPTION_ARG_NONE, &option_detach,
557 "Don't fork daemon to background" },
558 { "nodnsproxy", 'r', G_OPTION_FLAG_REVERSE,
559 G_OPTION_ARG_NONE, &option_dnsproxy,
560 "Don't enable DNS Proxy" },
561 { "nobacktrace", 0, G_OPTION_FLAG_REVERSE,
562 G_OPTION_ARG_NONE, &option_backtrace,
563 "Don't print out backtrace information" },
564 { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
565 "Show version information and exit" },
569 const char *connman_option_get_string(const char *key)
571 if (g_strcmp0(key, "wifi") == 0) {
573 return "nl80211,wext";
581 bool connman_setting_get_bool(const char *key)
583 if (g_str_equal(key, CONF_BG_SCAN))
584 return connman_settings.bg_scan;
586 if (g_str_equal(key, CONF_ALLOW_HOSTNAME_UPDATES))
587 return connman_settings.allow_hostname_updates;
589 if (g_str_equal(key, CONF_SINGLE_TECH))
590 return connman_settings.single_tech;
592 if (g_str_equal(key, CONF_PERSISTENT_TETHERING_MODE))
593 return connman_settings.persistent_tethering_mode;
595 if (g_str_equal(key, CONF_ENABLE_6TO4))
596 return connman_settings.enable_6to4;
601 char **connman_setting_get_string_list(const char *key)
603 if (g_str_equal(key, CONF_PREF_TIMESERVERS))
604 return connman_settings.pref_timeservers;
606 if (g_str_equal(key, CONF_FALLBACK_NAMESERVERS))
607 return connman_settings.fallback_nameservers;
609 if (g_str_equal(key, CONF_BLACKLISTED_INTERFACES))
610 return connman_settings.blacklisted_interfaces;
612 if (g_str_equal(key, CONF_TETHERING_TECHNOLOGIES))
613 return connman_settings.tethering_technologies;
615 #if defined TIZEN_EXT
616 if (g_str_equal(key, CONF_CELLULAR_INTERFACE))
617 return connman_settings.cellular_interfaces;
623 unsigned int *connman_setting_get_uint_list(const char *key)
625 if (g_str_equal(key, CONF_AUTO_CONNECT))
626 return connman_settings.auto_connect;
628 if (g_str_equal(key, CONF_PREFERRED_TECHS))
629 return connman_settings.preferred_techs;
634 unsigned int connman_timeout_input_request(void)
636 return connman_settings.timeout_inputreq;
639 unsigned int connman_timeout_browser_launch(void)
641 return connman_settings.timeout_browserlaunch;
644 int main(int argc, char *argv[])
646 GOptionContext *context;
647 GError *error = NULL;
648 DBusConnection *conn;
652 context = g_option_context_new(NULL);
653 g_option_context_add_main_entries(context, options, NULL);
655 if (!g_option_context_parse(context, &argc, &argv, &error)) {
657 g_printerr("%s\n", error->message);
660 g_printerr("An unknown error occurred\n");
664 g_option_context_free(context);
666 if (option_version) {
667 printf("%s\n", VERSION);
673 perror("Can't start daemon");
678 if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
679 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
681 perror("Failed to create storage directory");
686 main_loop = g_main_loop_new(NULL, FALSE);
688 signal = setup_signalfd();
690 dbus_error_init(&err);
692 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
694 if (dbus_error_is_set(&err)) {
695 fprintf(stderr, "%s\n", err.message);
696 dbus_error_free(&err);
698 fprintf(stderr, "Can't register with system bus\n");
702 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
704 __connman_log_init(argv[0], option_debug, option_detach,
705 option_backtrace, "Connection Manager", VERSION);
707 __connman_dbus_init(conn);
710 config_init(CONFIGMAINFILE);
712 config_init(option_config);
714 __connman_util_init();
715 __connman_inotify_init();
716 __connman_technology_init();
717 __connman_notifier_init();
718 __connman_agent_init();
719 __connman_service_init();
720 __connman_peer_service_init();
721 __connman_peer_init();
722 __connman_provider_init();
723 __connman_network_init();
724 __connman_config_init();
725 __connman_device_init(option_device, option_nodevice);
727 __connman_ippool_init();
728 __connman_iptables_init();
729 __connman_firewall_init();
730 __connman_nat_init();
731 __connman_tethering_init();
732 __connman_counter_init();
733 __connman_manager_init();
734 __connman_stats_init();
735 __connman_clock_init();
737 __connman_resolver_init(option_dnsproxy);
738 __connman_ipconfig_init();
739 __connman_rtnl_init();
740 __connman_task_init();
741 __connman_proxy_init();
742 __connman_detect_init();
743 __connman_session_init();
744 __connman_timeserver_init();
745 __connman_connection_init();
747 __connman_plugin_init(option_plugin, option_noplugin);
749 __connman_rtnl_start();
750 __connman_dhcp_init();
751 __connman_dhcpv6_init();
752 __connman_wpad_init();
753 __connman_wispr_init();
754 #if !defined TIZEN_EXT
755 __connman_rfkill_init();
757 __connman_machine_init();
759 g_free(option_config);
760 g_free(option_device);
761 g_free(option_plugin);
762 g_free(option_nodevice);
763 g_free(option_noplugin);
765 g_main_loop_run(main_loop);
767 g_source_remove(signal);
769 __connman_machine_cleanup();
770 #if !defined TIZEN_EXT
771 __connman_rfkill_cleanup();
773 __connman_wispr_cleanup();
774 __connman_wpad_cleanup();
775 __connman_dhcpv6_cleanup();
776 __connman_session_cleanup();
777 __connman_plugin_cleanup();
778 __connman_provider_cleanup();
779 __connman_connection_cleanup();
780 __connman_timeserver_cleanup();
781 __connman_detect_cleanup();
782 __connman_proxy_cleanup();
783 __connman_task_cleanup();
784 __connman_rtnl_cleanup();
785 __connman_resolver_cleanup();
787 __connman_clock_cleanup();
788 __connman_stats_cleanup();
789 __connman_config_cleanup();
790 __connman_manager_cleanup();
791 __connman_counter_cleanup();
792 __connman_tethering_cleanup();
793 __connman_nat_cleanup();
794 __connman_firewall_cleanup();
795 __connman_iptables_cleanup();
796 __connman_peer_service_cleanup();
797 __connman_peer_cleanup();
798 __connman_ippool_cleanup();
799 __connman_device_cleanup();
800 __connman_network_cleanup();
801 __connman_dhcp_cleanup();
802 __connman_service_cleanup();
803 __connman_agent_cleanup();
804 __connman_ipconfig_cleanup();
805 __connman_notifier_cleanup();
806 __connman_technology_cleanup();
807 __connman_inotify_cleanup();
809 __connman_util_cleanup();
810 __connman_dbus_cleanup();
812 __connman_log_cleanup(option_backtrace);
814 dbus_connection_unref(conn);
816 g_main_loop_unref(main_loop);
818 if (connman_settings.pref_timeservers)
819 g_strfreev(connman_settings.pref_timeservers);
821 g_free(connman_settings.auto_connect);
822 g_free(connman_settings.preferred_techs);
823 g_strfreev(connman_settings.fallback_nameservers);
824 g_strfreev(connman_settings.blacklisted_interfaces);
825 g_strfreev(connman_settings.tethering_technologies);
827 g_free(option_debug);