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[] = {
65 char **pref_timeservers;
66 unsigned int *auto_connect;
67 unsigned int *preferred_techs;
68 char **fallback_nameservers;
69 unsigned int timeout_inputreq;
70 unsigned int timeout_browserlaunch;
71 char **blacklisted_interfaces;
72 bool allow_hostname_updates;
74 char **tethering_technologies;
75 bool persistent_tethering_mode;
78 char **cellular_interfaces;
80 } connman_settings = {
82 .pref_timeservers = NULL,
84 .preferred_techs = NULL,
85 .fallback_nameservers = NULL,
86 .timeout_inputreq = DEFAULT_INPUT_REQUEST_TIMEOUT,
87 .timeout_browserlaunch = DEFAULT_BROWSER_LAUNCH_TIMEOUT,
88 .blacklisted_interfaces = NULL,
89 .allow_hostname_updates = true,
91 .tethering_technologies = NULL,
92 .persistent_tethering_mode = false,
95 .cellular_interfaces = NULL,
99 #define CONF_BG_SCAN "BackgroundScanning"
100 #define CONF_PREF_TIMESERVERS "FallbackTimeservers"
101 #define CONF_AUTO_CONNECT "DefaultAutoConnectTechnologies"
102 #define CONF_PREFERRED_TECHS "PreferredTechnologies"
103 #define CONF_FALLBACK_NAMESERVERS "FallbackNameservers"
104 #define CONF_TIMEOUT_INPUTREQ "InputRequestTimeout"
105 #define CONF_TIMEOUT_BROWSERLAUNCH "BrowserLaunchTimeout"
106 #define CONF_BLACKLISTED_INTERFACES "NetworkInterfaceBlacklist"
107 #define CONF_ALLOW_HOSTNAME_UPDATES "AllowHostnameUpdates"
108 #define CONF_SINGLE_TECH "SingleConnectedTechnology"
109 #define CONF_TETHERING_TECHNOLOGIES "TetheringTechnologies"
110 #define CONF_PERSISTENT_TETHERING_MODE "PersistentTetheringMode"
111 #define CONF_ENABLE_6TO4 "Enable6to4"
112 #if defined TIZEN_EXT
113 #define CONF_CELLULAR_INTERFACE "NetworkCellularInterfaceList"
116 static const char *supported_options[] = {
118 CONF_PREF_TIMESERVERS,
120 CONF_PREFERRED_TECHS,
121 CONF_FALLBACK_NAMESERVERS,
122 CONF_TIMEOUT_INPUTREQ,
123 CONF_TIMEOUT_BROWSERLAUNCH,
124 CONF_BLACKLISTED_INTERFACES,
125 CONF_ALLOW_HOSTNAME_UPDATES,
127 CONF_TETHERING_TECHNOLOGIES,
128 CONF_PERSISTENT_TETHERING_MODE,
130 #if defined TIZEN_EXT
131 CONF_CELLULAR_INTERFACE,
136 static GKeyFile *load_config(const char *file)
141 keyfile = g_key_file_new();
143 g_key_file_set_list_separator(keyfile, ',');
145 if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
146 if (err->code != G_FILE_ERROR_NOENT) {
147 connman_error("Parsing %s failed: %s", file,
152 g_key_file_free(keyfile);
159 static uint *parse_service_types(char **str_list, gsize len)
161 unsigned int *type_list;
163 enum connman_service_type type;
165 type_list = g_try_new0(unsigned int, len + 1);
171 while (str_list[i]) {
172 type = __connman_service_string2type(str_list[i]);
174 if (type != CONNMAN_SERVICE_TYPE_UNKNOWN) {
181 type_list[j] = CONNMAN_SERVICE_TYPE_UNKNOWN;
186 static char **parse_fallback_nameservers(char **nameservers, gsize len)
191 servers = g_try_new0(char *, len + 1);
197 while (nameservers[i]) {
198 if (connman_inet_check_ipaddress(nameservers[i]) > 0) {
199 servers[j] = g_strdup(nameservers[i]);
208 static void check_config(GKeyFile *config)
216 keys = g_key_file_get_groups(config, NULL);
218 for (j = 0; keys && keys[j]; j++) {
219 if (g_strcmp0(keys[j], "General") != 0)
220 connman_warn("Unknown group %s in %s",
226 keys = g_key_file_get_keys(config, "General", NULL, NULL);
228 for (j = 0; keys && keys[j]; j++) {
233 for (i = 0; supported_options[i]; i++) {
234 if (g_strcmp0(keys[j], supported_options[i]) == 0) {
239 if (!found && !supported_options[i])
240 connman_warn("Unknown option %s in %s",
247 #if defined TIZEN_EXT
248 static void check_Tizen_configuration(GKeyFile *config)
250 GError *error = NULL;
251 char **cellular_interfaces;
254 cellular_interfaces = g_key_file_get_string_list(config, "General",
255 CONF_CELLULAR_INTERFACE, &len, &error);
258 connman_settings.cellular_interfaces = cellular_interfaces;
260 g_clear_error(&error);
264 static void parse_config(GKeyFile *config)
266 GError *error = NULL;
276 connman_settings.auto_connect =
277 parse_service_types(default_auto_connect, 3);
278 connman_settings.blacklisted_interfaces =
279 g_strdupv(default_blacklist);
283 DBG("parsing %s", MAINFILE);
285 boolean = g_key_file_get_boolean(config, "General",
286 CONF_BG_SCAN, &error);
288 connman_settings.bg_scan = boolean;
290 g_clear_error(&error);
292 timeservers = __connman_config_get_string_list(config, "General",
293 CONF_PREF_TIMESERVERS, NULL, &error);
295 connman_settings.pref_timeservers = timeservers;
297 g_clear_error(&error);
299 str_list = __connman_config_get_string_list(config, "General",
300 CONF_AUTO_CONNECT, &len, &error);
303 connman_settings.auto_connect =
304 parse_service_types(str_list, len);
306 connman_settings.auto_connect =
307 parse_service_types(default_auto_connect, 3);
309 g_strfreev(str_list);
311 g_clear_error(&error);
313 str_list = __connman_config_get_string_list(config, "General",
314 CONF_PREFERRED_TECHS, &len, &error);
317 connman_settings.preferred_techs =
318 parse_service_types(str_list, len);
320 g_strfreev(str_list);
322 g_clear_error(&error);
324 str_list = __connman_config_get_string_list(config, "General",
325 CONF_FALLBACK_NAMESERVERS, &len, &error);
328 connman_settings.fallback_nameservers =
329 parse_fallback_nameservers(str_list, len);
331 g_strfreev(str_list);
333 g_clear_error(&error);
335 timeout = g_key_file_get_integer(config, "General",
336 CONF_TIMEOUT_INPUTREQ, &error);
337 if (!error && timeout >= 0)
338 connman_settings.timeout_inputreq = timeout * 1000;
340 g_clear_error(&error);
342 timeout = g_key_file_get_integer(config, "General",
343 CONF_TIMEOUT_BROWSERLAUNCH, &error);
344 if (!error && timeout >= 0)
345 connman_settings.timeout_browserlaunch = timeout * 1000;
347 g_clear_error(&error);
349 interfaces = __connman_config_get_string_list(config, "General",
350 CONF_BLACKLISTED_INTERFACES, &len, &error);
353 connman_settings.blacklisted_interfaces = interfaces;
355 connman_settings.blacklisted_interfaces =
356 g_strdupv(default_blacklist);
358 g_clear_error(&error);
360 boolean = __connman_config_get_bool(config, "General",
361 CONF_ALLOW_HOSTNAME_UPDATES,
364 connman_settings.allow_hostname_updates = boolean;
366 g_clear_error(&error);
368 boolean = __connman_config_get_bool(config, "General",
369 CONF_SINGLE_TECH, &error);
371 connman_settings.single_tech = boolean;
373 g_clear_error(&error);
375 tethering = __connman_config_get_string_list(config, "General",
376 CONF_TETHERING_TECHNOLOGIES, &len, &error);
379 connman_settings.tethering_technologies = tethering;
381 g_clear_error(&error);
383 boolean = __connman_config_get_bool(config, "General",
384 CONF_PERSISTENT_TETHERING_MODE,
387 connman_settings.persistent_tethering_mode = boolean;
389 g_clear_error(&error);
391 boolean = __connman_config_get_bool(config, "General",
392 CONF_ENABLE_6TO4, &error);
394 connman_settings.enable_6to4 = boolean;
396 g_clear_error(&error);
398 #if defined TIZEN_EXT
399 check_Tizen_configuration(config);
403 static int config_init(const char *file)
407 config = load_config(file);
408 check_config(config);
409 parse_config(config);
411 g_key_file_free(config);
416 static GMainLoop *main_loop = NULL;
418 static unsigned int __terminated = 0;
420 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
423 struct signalfd_siginfo si;
427 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
430 fd = g_io_channel_unix_get_fd(channel);
432 result = read(fd, &si, sizeof(si));
433 if (result != sizeof(si))
436 switch (si.ssi_signo) {
439 if (__terminated == 0) {
440 connman_info("Terminating");
441 g_main_loop_quit(main_loop);
451 static guint setup_signalfd(void)
459 sigaddset(&mask, SIGINT);
460 sigaddset(&mask, SIGTERM);
462 if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
463 perror("Failed to set signal mask");
467 fd = signalfd(-1, &mask, 0);
469 perror("Failed to create signal descriptor");
473 channel = g_io_channel_unix_new(fd);
475 g_io_channel_set_close_on_unref(channel, TRUE);
476 g_io_channel_set_encoding(channel, NULL, NULL);
477 g_io_channel_set_buffered(channel, FALSE);
479 source = g_io_add_watch(channel,
480 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
481 signal_handler, NULL);
483 g_io_channel_unref(channel);
488 static void disconnect_callback(DBusConnection *conn, void *user_data)
490 connman_error("D-Bus disconnect");
492 g_main_loop_quit(main_loop);
495 static gchar *option_config = NULL;
496 static gchar *option_debug = NULL;
497 static gchar *option_device = NULL;
498 static gchar *option_plugin = NULL;
499 static gchar *option_nodevice = NULL;
500 static gchar *option_noplugin = NULL;
501 static gchar *option_wifi = NULL;
502 static gboolean option_detach = TRUE;
503 static gboolean option_dnsproxy = TRUE;
504 static gboolean option_backtrace = TRUE;
505 static gboolean option_version = FALSE;
507 static bool parse_debug(const char *key, const char *value,
508 gpointer user_data, GError **error)
511 option_debug = g_strdup(value);
513 option_debug = g_strdup("*");
518 static GOptionEntry options[] = {
519 { "config", 'c', 0, G_OPTION_ARG_STRING, &option_config,
520 "Load the specified configuration file "
521 "instead of " CONFIGMAINFILE, "FILE" },
522 { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
523 G_OPTION_ARG_CALLBACK, parse_debug,
524 "Specify debug options to enable", "DEBUG" },
525 { "device", 'i', 0, G_OPTION_ARG_STRING, &option_device,
526 "Specify networking device or interface", "DEV" },
527 { "nodevice", 'I', 0, G_OPTION_ARG_STRING, &option_nodevice,
528 "Specify networking interface to ignore", "DEV" },
529 { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
530 "Specify plugins to load", "NAME,..." },
531 { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
532 "Specify plugins not to load", "NAME,..." },
533 { "wifi", 'W', 0, G_OPTION_ARG_STRING, &option_wifi,
534 "Specify driver for WiFi/Supplicant", "NAME" },
535 { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
536 G_OPTION_ARG_NONE, &option_detach,
537 "Don't fork daemon to background" },
538 { "nodnsproxy", 'r', G_OPTION_FLAG_REVERSE,
539 G_OPTION_ARG_NONE, &option_dnsproxy,
540 "Don't enable DNS Proxy" },
541 { "nobacktrace", 0, G_OPTION_FLAG_REVERSE,
542 G_OPTION_ARG_NONE, &option_backtrace,
543 "Don't print out backtrace information" },
544 { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
545 "Show version information and exit" },
549 const char *connman_option_get_string(const char *key)
551 if (g_strcmp0(key, "wifi") == 0) {
553 return "nl80211,wext";
561 bool connman_setting_get_bool(const char *key)
563 if (g_str_equal(key, CONF_BG_SCAN))
564 return connman_settings.bg_scan;
566 if (g_str_equal(key, CONF_ALLOW_HOSTNAME_UPDATES))
567 return connman_settings.allow_hostname_updates;
569 if (g_str_equal(key, CONF_SINGLE_TECH))
570 return connman_settings.single_tech;
572 if (g_str_equal(key, CONF_PERSISTENT_TETHERING_MODE))
573 return connman_settings.persistent_tethering_mode;
575 if (g_str_equal(key, CONF_ENABLE_6TO4))
576 return connman_settings.enable_6to4;
581 char **connman_setting_get_string_list(const char *key)
583 if (g_str_equal(key, CONF_PREF_TIMESERVERS))
584 return connman_settings.pref_timeservers;
586 if (g_str_equal(key, CONF_FALLBACK_NAMESERVERS))
587 return connman_settings.fallback_nameservers;
589 if (g_str_equal(key, CONF_BLACKLISTED_INTERFACES))
590 return connman_settings.blacklisted_interfaces;
592 if (g_str_equal(key, CONF_TETHERING_TECHNOLOGIES))
593 return connman_settings.tethering_technologies;
595 #if defined TIZEN_EXT
596 if (g_str_equal(key, CONF_CELLULAR_INTERFACE))
597 return connman_settings.cellular_interfaces;
603 unsigned int *connman_setting_get_uint_list(const char *key)
605 if (g_str_equal(key, CONF_AUTO_CONNECT))
606 return connman_settings.auto_connect;
608 if (g_str_equal(key, CONF_PREFERRED_TECHS))
609 return connman_settings.preferred_techs;
614 unsigned int connman_timeout_input_request(void)
616 return connman_settings.timeout_inputreq;
619 unsigned int connman_timeout_browser_launch(void)
621 return connman_settings.timeout_browserlaunch;
624 int main(int argc, char *argv[])
626 GOptionContext *context;
627 GError *error = NULL;
628 DBusConnection *conn;
632 context = g_option_context_new(NULL);
633 g_option_context_add_main_entries(context, options, NULL);
635 if (!g_option_context_parse(context, &argc, &argv, &error)) {
637 g_printerr("%s\n", error->message);
640 g_printerr("An unknown error occurred\n");
644 g_option_context_free(context);
646 if (option_version) {
647 printf("%s\n", VERSION);
653 perror("Can't start daemon");
658 if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
659 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
661 perror("Failed to create storage directory");
666 main_loop = g_main_loop_new(NULL, FALSE);
668 signal = setup_signalfd();
670 dbus_error_init(&err);
672 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
674 if (dbus_error_is_set(&err)) {
675 fprintf(stderr, "%s\n", err.message);
676 dbus_error_free(&err);
678 fprintf(stderr, "Can't register with system bus\n");
682 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
684 __connman_log_init(argv[0], option_debug, option_detach,
685 option_backtrace, "Connection Manager", VERSION);
687 __connman_dbus_init(conn);
690 config_init(CONFIGMAINFILE);
692 config_init(option_config);
694 __connman_util_init();
695 __connman_inotify_init();
696 __connman_technology_init();
697 __connman_notifier_init();
698 __connman_agent_init();
699 __connman_service_init();
700 __connman_peer_service_init();
701 __connman_peer_init();
702 __connman_provider_init();
703 __connman_network_init();
704 __connman_config_init();
705 __connman_device_init(option_device, option_nodevice);
707 __connman_ippool_init();
708 __connman_iptables_init();
709 __connman_firewall_init();
710 __connman_nat_init();
711 __connman_tethering_init();
712 __connman_counter_init();
713 __connman_manager_init();
714 __connman_stats_init();
715 __connman_clock_init();
717 __connman_resolver_init(option_dnsproxy);
718 __connman_ipconfig_init();
719 __connman_rtnl_init();
720 __connman_task_init();
721 __connman_proxy_init();
722 __connman_detect_init();
723 __connman_session_init();
724 __connman_timeserver_init();
725 __connman_connection_init();
727 __connman_plugin_init(option_plugin, option_noplugin);
729 __connman_rtnl_start();
730 __connman_dhcp_init();
731 __connman_dhcpv6_init();
732 __connman_wpad_init();
733 __connman_wispr_init();
734 __connman_rfkill_init();
735 __connman_machine_init();
737 g_free(option_config);
738 g_free(option_device);
739 g_free(option_plugin);
740 g_free(option_nodevice);
741 g_free(option_noplugin);
743 g_main_loop_run(main_loop);
745 g_source_remove(signal);
747 __connman_machine_cleanup();
748 __connman_rfkill_cleanup();
749 __connman_wispr_cleanup();
750 __connman_wpad_cleanup();
751 __connman_dhcpv6_cleanup();
752 __connman_session_cleanup();
753 __connman_plugin_cleanup();
754 __connman_provider_cleanup();
755 __connman_connection_cleanup();
756 __connman_timeserver_cleanup();
757 __connman_detect_cleanup();
758 __connman_proxy_cleanup();
759 __connman_task_cleanup();
760 __connman_rtnl_cleanup();
761 __connman_resolver_cleanup();
763 __connman_clock_cleanup();
764 __connman_stats_cleanup();
765 __connman_config_cleanup();
766 __connman_manager_cleanup();
767 __connman_counter_cleanup();
768 __connman_tethering_cleanup();
769 __connman_nat_cleanup();
770 __connman_firewall_cleanup();
771 __connman_iptables_cleanup();
772 __connman_peer_service_cleanup();
773 __connman_peer_cleanup();
774 __connman_ippool_cleanup();
775 __connman_device_cleanup();
776 __connman_network_cleanup();
777 __connman_dhcp_cleanup();
778 __connman_service_cleanup();
779 __connman_agent_cleanup();
780 __connman_ipconfig_cleanup();
781 __connman_notifier_cleanup();
782 __connman_technology_cleanup();
783 __connman_inotify_cleanup();
785 __connman_util_cleanup();
786 __connman_dbus_cleanup();
788 __connman_log_cleanup(option_backtrace);
790 dbus_connection_unref(conn);
792 g_main_loop_unref(main_loop);
794 if (connman_settings.pref_timeservers)
795 g_strfreev(connman_settings.pref_timeservers);
797 g_free(connman_settings.auto_connect);
798 g_free(connman_settings.preferred_techs);
799 g_strfreev(connman_settings.fallback_nameservers);
800 g_strfreev(connman_settings.blacklisted_interfaces);
801 g_strfreev(connman_settings.tethering_technologies);
803 g_free(option_debug);