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;
76 } connman_settings = {
78 .pref_timeservers = NULL,
80 .preferred_techs = NULL,
81 .fallback_nameservers = NULL,
82 .timeout_inputreq = DEFAULT_INPUT_REQUEST_TIMEOUT,
83 .timeout_browserlaunch = DEFAULT_BROWSER_LAUNCH_TIMEOUT,
84 .blacklisted_interfaces = NULL,
85 .allow_hostname_updates = true,
87 .tethering_technologies = NULL,
88 .persistent_tethering_mode = false,
91 #define CONF_BG_SCAN "BackgroundScanning"
92 #define CONF_PREF_TIMESERVERS "FallbackTimeservers"
93 #define CONF_AUTO_CONNECT "DefaultAutoConnectTechnologies"
94 #define CONF_PREFERRED_TECHS "PreferredTechnologies"
95 #define CONF_FALLBACK_NAMESERVERS "FallbackNameservers"
96 #define CONF_TIMEOUT_INPUTREQ "InputRequestTimeout"
97 #define CONF_TIMEOUT_BROWSERLAUNCH "BrowserLaunchTimeout"
98 #define CONF_BLACKLISTED_INTERFACES "NetworkInterfaceBlacklist"
99 #define CONF_ALLOW_HOSTNAME_UPDATES "AllowHostnameUpdates"
100 #define CONF_SINGLE_TECH "SingleConnectedTechnology"
101 #define CONF_TETHERING_TECHNOLOGIES "TetheringTechnologies"
102 #define CONF_PERSISTENT_TETHERING_MODE "PersistentTetheringMode"
104 static const char *supported_options[] = {
106 CONF_PREF_TIMESERVERS,
108 CONF_PREFERRED_TECHS,
109 CONF_FALLBACK_NAMESERVERS,
110 CONF_TIMEOUT_INPUTREQ,
111 CONF_TIMEOUT_BROWSERLAUNCH,
112 CONF_BLACKLISTED_INTERFACES,
113 CONF_ALLOW_HOSTNAME_UPDATES,
115 CONF_TETHERING_TECHNOLOGIES,
116 CONF_PERSISTENT_TETHERING_MODE,
120 static GKeyFile *load_config(const char *file)
125 keyfile = g_key_file_new();
127 g_key_file_set_list_separator(keyfile, ',');
129 if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
130 if (err->code != G_FILE_ERROR_NOENT) {
131 connman_error("Parsing %s failed: %s", file,
136 g_key_file_free(keyfile);
143 static uint *parse_service_types(char **str_list, gsize len)
145 unsigned int *type_list;
147 enum connman_service_type type;
149 type_list = g_try_new0(unsigned int, len + 1);
155 while (str_list[i]) {
156 type = __connman_service_string2type(str_list[i]);
158 if (type != CONNMAN_SERVICE_TYPE_UNKNOWN) {
165 type_list[j] = CONNMAN_SERVICE_TYPE_UNKNOWN;
170 static char **parse_fallback_nameservers(char **nameservers, gsize len)
175 servers = g_try_new0(char *, len + 1);
181 while (nameservers[i]) {
182 if (connman_inet_check_ipaddress(nameservers[i]) > 0) {
183 servers[j] = g_strdup(nameservers[i]);
192 static void check_config(GKeyFile *config)
200 keys = g_key_file_get_groups(config, NULL);
202 for (j = 0; keys && keys[j]; j++) {
203 if (g_strcmp0(keys[j], "General") != 0)
204 connman_warn("Unknown group %s in %s",
210 keys = g_key_file_get_keys(config, "General", NULL, NULL);
212 for (j = 0; keys && keys[j]; j++) {
217 for (i = 0; supported_options[i]; i++) {
218 if (g_strcmp0(keys[j], supported_options[i]) == 0) {
223 if (!found && !supported_options[i])
224 connman_warn("Unknown option %s in %s",
231 static void parse_config(GKeyFile *config)
233 GError *error = NULL;
243 connman_settings.auto_connect =
244 parse_service_types(default_auto_connect, 3);
245 connman_settings.blacklisted_interfaces =
246 g_strdupv(default_blacklist);
250 DBG("parsing %s", MAINFILE);
252 boolean = g_key_file_get_boolean(config, "General",
253 CONF_BG_SCAN, &error);
255 connman_settings.bg_scan = boolean;
257 g_clear_error(&error);
259 timeservers = __connman_config_get_string_list(config, "General",
260 CONF_PREF_TIMESERVERS, NULL, &error);
262 connman_settings.pref_timeservers = timeservers;
264 g_clear_error(&error);
266 str_list = __connman_config_get_string_list(config, "General",
267 CONF_AUTO_CONNECT, &len, &error);
270 connman_settings.auto_connect =
271 parse_service_types(str_list, len);
273 connman_settings.auto_connect =
274 parse_service_types(default_auto_connect, 3);
276 g_strfreev(str_list);
278 g_clear_error(&error);
280 str_list = __connman_config_get_string_list(config, "General",
281 CONF_PREFERRED_TECHS, &len, &error);
284 connman_settings.preferred_techs =
285 parse_service_types(str_list, len);
287 g_strfreev(str_list);
289 g_clear_error(&error);
291 str_list = __connman_config_get_string_list(config, "General",
292 CONF_FALLBACK_NAMESERVERS, &len, &error);
295 connman_settings.fallback_nameservers =
296 parse_fallback_nameservers(str_list, len);
298 g_strfreev(str_list);
300 g_clear_error(&error);
302 timeout = g_key_file_get_integer(config, "General",
303 CONF_TIMEOUT_INPUTREQ, &error);
304 if (!error && timeout >= 0)
305 connman_settings.timeout_inputreq = timeout * 1000;
307 g_clear_error(&error);
309 timeout = g_key_file_get_integer(config, "General",
310 CONF_TIMEOUT_BROWSERLAUNCH, &error);
311 if (!error && timeout >= 0)
312 connman_settings.timeout_browserlaunch = timeout * 1000;
314 g_clear_error(&error);
316 interfaces = __connman_config_get_string_list(config, "General",
317 CONF_BLACKLISTED_INTERFACES, &len, &error);
320 connman_settings.blacklisted_interfaces = interfaces;
322 connman_settings.blacklisted_interfaces =
323 g_strdupv(default_blacklist);
325 g_clear_error(&error);
327 boolean = __connman_config_get_bool(config, "General",
328 CONF_ALLOW_HOSTNAME_UPDATES,
331 connman_settings.allow_hostname_updates = boolean;
333 g_clear_error(&error);
335 boolean = __connman_config_get_bool(config, "General",
336 CONF_SINGLE_TECH, &error);
338 connman_settings.single_tech = boolean;
340 g_clear_error(&error);
342 tethering = __connman_config_get_string_list(config, "General",
343 CONF_TETHERING_TECHNOLOGIES, &len, &error);
346 connman_settings.tethering_technologies = tethering;
348 g_clear_error(&error);
350 boolean = __connman_config_get_bool(config, "General",
351 CONF_PERSISTENT_TETHERING_MODE,
354 connman_settings.persistent_tethering_mode = boolean;
356 g_clear_error(&error);
359 static int config_init(const char *file)
363 config = load_config(file);
364 check_config(config);
365 parse_config(config);
367 g_key_file_free(config);
372 static GMainLoop *main_loop = NULL;
374 static unsigned int __terminated = 0;
376 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
379 struct signalfd_siginfo si;
383 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
386 fd = g_io_channel_unix_get_fd(channel);
388 result = read(fd, &si, sizeof(si));
389 if (result != sizeof(si))
392 switch (si.ssi_signo) {
395 if (__terminated == 0) {
396 connman_info("Terminating");
397 g_main_loop_quit(main_loop);
407 static guint setup_signalfd(void)
415 sigaddset(&mask, SIGINT);
416 sigaddset(&mask, SIGTERM);
418 if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
419 perror("Failed to set signal mask");
423 fd = signalfd(-1, &mask, 0);
425 perror("Failed to create signal descriptor");
429 channel = g_io_channel_unix_new(fd);
431 g_io_channel_set_close_on_unref(channel, TRUE);
432 g_io_channel_set_encoding(channel, NULL, NULL);
433 g_io_channel_set_buffered(channel, FALSE);
435 source = g_io_add_watch(channel,
436 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
437 signal_handler, NULL);
439 g_io_channel_unref(channel);
444 static void disconnect_callback(DBusConnection *conn, void *user_data)
446 connman_error("D-Bus disconnect");
448 g_main_loop_quit(main_loop);
451 static gchar *option_config = NULL;
452 static gchar *option_debug = NULL;
453 static gchar *option_device = NULL;
454 static gchar *option_plugin = NULL;
455 static gchar *option_nodevice = NULL;
456 static gchar *option_noplugin = NULL;
457 static gchar *option_wifi = NULL;
458 static gboolean option_detach = TRUE;
459 static gboolean option_dnsproxy = TRUE;
460 static gboolean option_backtrace = TRUE;
461 static gboolean option_version = FALSE;
463 static bool parse_debug(const char *key, const char *value,
464 gpointer user_data, GError **error)
467 option_debug = g_strdup(value);
469 option_debug = g_strdup("*");
474 static GOptionEntry options[] = {
475 { "config", 'c', 0, G_OPTION_ARG_STRING, &option_config,
476 "Load the specified configuration file "
477 "instead of " CONFIGMAINFILE, "FILE" },
478 { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
479 G_OPTION_ARG_CALLBACK, parse_debug,
480 "Specify debug options to enable", "DEBUG" },
481 { "device", 'i', 0, G_OPTION_ARG_STRING, &option_device,
482 "Specify networking device or interface", "DEV" },
483 { "nodevice", 'I', 0, G_OPTION_ARG_STRING, &option_nodevice,
484 "Specify networking interface to ignore", "DEV" },
485 { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
486 "Specify plugins to load", "NAME,..." },
487 { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
488 "Specify plugins not to load", "NAME,..." },
489 { "wifi", 'W', 0, G_OPTION_ARG_STRING, &option_wifi,
490 "Specify driver for WiFi/Supplicant", "NAME" },
491 { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
492 G_OPTION_ARG_NONE, &option_detach,
493 "Don't fork daemon to background" },
494 { "nodnsproxy", 'r', G_OPTION_FLAG_REVERSE,
495 G_OPTION_ARG_NONE, &option_dnsproxy,
496 "Don't enable DNS Proxy" },
497 { "nobacktrace", 0, G_OPTION_FLAG_REVERSE,
498 G_OPTION_ARG_NONE, &option_backtrace,
499 "Don't print out backtrace information" },
500 { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
501 "Show version information and exit" },
505 const char *connman_option_get_string(const char *key)
507 if (g_strcmp0(key, "wifi") == 0) {
509 return "nl80211,wext";
517 bool connman_setting_get_bool(const char *key)
519 if (g_str_equal(key, CONF_BG_SCAN))
520 return connman_settings.bg_scan;
522 if (g_str_equal(key, CONF_ALLOW_HOSTNAME_UPDATES))
523 return connman_settings.allow_hostname_updates;
525 if (g_str_equal(key, CONF_SINGLE_TECH))
526 return connman_settings.single_tech;
528 if (g_str_equal(key, CONF_PERSISTENT_TETHERING_MODE))
529 return connman_settings.persistent_tethering_mode;
534 char **connman_setting_get_string_list(const char *key)
536 if (g_str_equal(key, CONF_PREF_TIMESERVERS))
537 return connman_settings.pref_timeservers;
539 if (g_str_equal(key, CONF_FALLBACK_NAMESERVERS))
540 return connman_settings.fallback_nameservers;
542 if (g_str_equal(key, CONF_BLACKLISTED_INTERFACES))
543 return connman_settings.blacklisted_interfaces;
545 if (g_str_equal(key, CONF_TETHERING_TECHNOLOGIES))
546 return connman_settings.tethering_technologies;
551 unsigned int *connman_setting_get_uint_list(const char *key)
553 if (g_str_equal(key, CONF_AUTO_CONNECT))
554 return connman_settings.auto_connect;
556 if (g_str_equal(key, CONF_PREFERRED_TECHS))
557 return connman_settings.preferred_techs;
562 unsigned int connman_timeout_input_request(void)
564 return connman_settings.timeout_inputreq;
567 unsigned int connman_timeout_browser_launch(void)
569 return connman_settings.timeout_browserlaunch;
572 int main(int argc, char *argv[])
574 GOptionContext *context;
575 GError *error = NULL;
576 DBusConnection *conn;
580 context = g_option_context_new(NULL);
581 g_option_context_add_main_entries(context, options, NULL);
583 if (!g_option_context_parse(context, &argc, &argv, &error)) {
585 g_printerr("%s\n", error->message);
588 g_printerr("An unknown error occurred\n");
592 g_option_context_free(context);
594 if (option_version) {
595 printf("%s\n", VERSION);
601 perror("Can't start daemon");
606 if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
607 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
609 perror("Failed to create storage directory");
614 main_loop = g_main_loop_new(NULL, FALSE);
616 signal = setup_signalfd();
618 dbus_error_init(&err);
620 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
622 if (dbus_error_is_set(&err)) {
623 fprintf(stderr, "%s\n", err.message);
624 dbus_error_free(&err);
626 fprintf(stderr, "Can't register with system bus\n");
630 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
632 __connman_log_init(argv[0], option_debug, option_detach,
633 option_backtrace, "Connection Manager", VERSION);
635 __connman_dbus_init(conn);
638 config_init(CONFIGMAINFILE);
640 config_init(option_config);
642 __connman_util_init();
643 __connman_inotify_init();
644 __connman_technology_init();
645 __connman_notifier_init();
646 __connman_agent_init();
647 __connman_service_init();
648 __connman_peer_service_init();
649 __connman_peer_init();
650 __connman_provider_init();
651 __connman_network_init();
652 __connman_config_init();
653 __connman_device_init(option_device, option_nodevice);
655 __connman_ippool_init();
656 __connman_iptables_init();
657 __connman_firewall_init();
658 __connman_nat_init();
659 __connman_tethering_init();
660 __connman_counter_init();
661 __connman_manager_init();
662 __connman_stats_init();
663 __connman_clock_init();
665 __connman_resolver_init(option_dnsproxy);
666 __connman_ipconfig_init();
667 __connman_rtnl_init();
668 __connman_task_init();
669 __connman_proxy_init();
670 __connman_detect_init();
671 __connman_session_init();
672 __connman_timeserver_init();
673 __connman_connection_init();
675 __connman_plugin_init(option_plugin, option_noplugin);
677 __connman_rtnl_start();
678 __connman_dhcp_init();
679 __connman_dhcpv6_init();
680 __connman_wpad_init();
681 __connman_wispr_init();
682 __connman_rfkill_init();
683 __connman_machine_init();
685 g_free(option_config);
686 g_free(option_device);
687 g_free(option_plugin);
688 g_free(option_nodevice);
689 g_free(option_noplugin);
691 g_main_loop_run(main_loop);
693 g_source_remove(signal);
695 __connman_machine_cleanup();
696 __connman_rfkill_cleanup();
697 __connman_wispr_cleanup();
698 __connman_wpad_cleanup();
699 __connman_dhcpv6_cleanup();
700 __connman_session_cleanup();
701 __connman_plugin_cleanup();
702 __connman_provider_cleanup();
703 __connman_connection_cleanup();
704 __connman_timeserver_cleanup();
705 __connman_detect_cleanup();
706 __connman_proxy_cleanup();
707 __connman_task_cleanup();
708 __connman_rtnl_cleanup();
709 __connman_resolver_cleanup();
711 __connman_clock_cleanup();
712 __connman_stats_cleanup();
713 __connman_config_cleanup();
714 __connman_manager_cleanup();
715 __connman_counter_cleanup();
716 __connman_tethering_cleanup();
717 __connman_nat_cleanup();
718 __connman_firewall_cleanup();
719 __connman_iptables_cleanup();
720 __connman_peer_service_cleanup();
721 __connman_peer_cleanup();
722 __connman_ippool_cleanup();
723 __connman_device_cleanup();
724 __connman_network_cleanup();
725 __connman_dhcp_cleanup();
726 __connman_service_cleanup();
727 __connman_agent_cleanup();
728 __connman_ipconfig_cleanup();
729 __connman_notifier_cleanup();
730 __connman_technology_cleanup();
731 __connman_inotify_cleanup();
733 __connman_util_cleanup();
734 __connman_dbus_cleanup();
736 __connman_log_cleanup(option_backtrace);
738 dbus_connection_unref(conn);
740 g_main_loop_unref(main_loop);
742 if (connman_settings.pref_timeservers)
743 g_strfreev(connman_settings.pref_timeservers);
745 g_free(connman_settings.auto_connect);
746 g_free(connman_settings.preferred_techs);
747 g_strfreev(connman_settings.fallback_nameservers);
748 g_strfreev(connman_settings.blacklisted_interfaces);
749 g_strfreev(connman_settings.tethering_technologies);
751 g_free(option_debug);