5 * Copyright (C) 2007-2012 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[] = {
63 connman_bool_t bg_scan;
64 char **pref_timeservers;
65 unsigned int *auto_connect;
66 unsigned int *preferred_techs;
67 char **fallback_nameservers;
68 unsigned int timeout_inputreq;
69 unsigned int timeout_browserlaunch;
70 char **blacklisted_interfaces;
71 connman_bool_t allow_hostname_updates;
72 connman_bool_t single_tech;
73 char **allowed_tethering_technologies;
74 } connman_settings = {
76 .pref_timeservers = NULL,
78 .preferred_techs = NULL,
79 .fallback_nameservers = NULL,
80 .timeout_inputreq = DEFAULT_INPUT_REQUEST_TIMEOUT,
81 .timeout_browserlaunch = DEFAULT_BROWSER_LAUNCH_TIMEOUT,
82 .blacklisted_interfaces = NULL,
83 .allow_hostname_updates = TRUE,
85 .allowed_tethering_technologies = NULL,
88 #define CONF_BG_SCAN "BackgroundScanning"
89 #define CONF_PREF_TIMESERVERS "FallbackTimeservers"
90 #define CONF_AUTO_CONNECT "DefaultAutoConnectTechnologies"
91 #define CONF_PREFERRED_TECHS "PreferredTechnologies"
92 #define CONF_FALLBACK_NAMESERVERS "FallbackNameservers"
93 #define CONF_TIMEOUT_INPUTREQ "InputRequestTimeout"
94 #define CONF_TIMEOUT_BROWSERLAUNCH "BrowserLaunchTimeout"
95 #define CONF_BLACKLISTED_INTERFACES "NetworkInterfaceBlacklist"
96 #define CONF_ALLOW_HOSTNAME_UPDATES "AllowHostnameUpdates"
97 #define CONF_SINGLE_TECH "SingleConnectedTechnology"
98 #define CONF_ALLOWED_TETHERING_TECHNOLOGIES "AllowedTetheringTechnologies"
100 static const char *supported_options[] = {
102 CONF_PREF_TIMESERVERS,
104 CONF_PREFERRED_TECHS,
105 CONF_FALLBACK_NAMESERVERS,
106 CONF_TIMEOUT_INPUTREQ,
107 CONF_TIMEOUT_BROWSERLAUNCH,
108 CONF_BLACKLISTED_INTERFACES,
109 CONF_ALLOW_HOSTNAME_UPDATES,
111 CONF_ALLOWED_TETHERING_TECHNOLOGIES,
115 static GKeyFile *load_config(const char *file)
120 keyfile = g_key_file_new();
122 g_key_file_set_list_separator(keyfile, ',');
124 if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
125 if (err->code != G_FILE_ERROR_NOENT) {
126 connman_error("Parsing %s failed: %s", file,
131 g_key_file_free(keyfile);
138 static uint *parse_service_types(char **str_list, gsize len)
140 unsigned int *type_list;
142 enum connman_service_type type;
144 type_list = g_try_new0(unsigned int, len + 1);
145 if (type_list == NULL)
150 while (str_list[i] != NULL)
152 type = __connman_service_string2type(str_list[i]);
154 if (type != CONNMAN_SERVICE_TYPE_UNKNOWN) {
164 static char **parse_fallback_nameservers(char **nameservers, gsize len)
169 servers = g_try_new0(char *, len + 1);
175 while (nameservers[i] != NULL) {
176 if (connman_inet_check_ipaddress(nameservers[i]) > 0) {
177 servers[j] = g_strdup(nameservers[i]);
186 static void check_config(GKeyFile *config)
194 keys = g_key_file_get_groups(config, NULL);
196 for (j = 0; keys != NULL && keys[j] != NULL; j++) {
197 if (g_strcmp0(keys[j], "General") != 0)
198 connman_warn("Unknown group %s in %s",
204 keys = g_key_file_get_keys(config, "General", NULL, NULL);
206 for (j = 0; keys != NULL && keys[j] != NULL; j++) {
207 connman_bool_t found;
211 for (i = 0; supported_options[i] != NULL; i++) {
212 if (g_strcmp0(keys[j], supported_options[i]) == 0) {
217 if (found == FALSE && supported_options[i] == NULL)
218 connman_warn("Unknown option %s in %s",
225 static void parse_config(GKeyFile *config)
227 GError *error = NULL;
236 if (config == NULL) {
237 connman_settings.auto_connect =
238 parse_service_types(default_auto_connect, 3);
239 connman_settings.blacklisted_interfaces =
240 g_strdupv(default_blacklist);
244 DBG("parsing %s", MAINFILE);
246 boolean = g_key_file_get_boolean(config, "General",
247 CONF_BG_SCAN, &error);
249 connman_settings.bg_scan = boolean;
251 g_clear_error(&error);
253 timeservers = g_key_file_get_string_list(config, "General",
254 CONF_PREF_TIMESERVERS, NULL, &error);
256 connman_settings.pref_timeservers = timeservers;
258 g_clear_error(&error);
260 str_list = g_key_file_get_string_list(config, "General",
261 CONF_AUTO_CONNECT, &len, &error);
264 connman_settings.auto_connect =
265 parse_service_types(str_list, len);
267 connman_settings.auto_connect =
268 parse_service_types(default_auto_connect, 3);
270 g_strfreev(str_list);
272 g_clear_error(&error);
274 str_list = g_key_file_get_string_list(config, "General",
275 CONF_PREFERRED_TECHS, &len, &error);
278 connman_settings.preferred_techs =
279 parse_service_types(str_list, len);
281 g_strfreev(str_list);
283 g_clear_error(&error);
285 str_list = g_key_file_get_string_list(config, "General",
286 CONF_FALLBACK_NAMESERVERS, &len, &error);
289 connman_settings.fallback_nameservers =
290 parse_fallback_nameservers(str_list, len);
292 g_strfreev(str_list);
294 g_clear_error(&error);
296 timeout = g_key_file_get_integer(config, "General",
297 CONF_TIMEOUT_INPUTREQ, &error);
298 if (error == NULL && timeout >= 0)
299 connman_settings.timeout_inputreq = timeout * 1000;
301 g_clear_error(&error);
303 timeout = g_key_file_get_integer(config, "General",
304 CONF_TIMEOUT_BROWSERLAUNCH, &error);
305 if (error == NULL && timeout >= 0)
306 connman_settings.timeout_browserlaunch = timeout * 1000;
308 g_clear_error(&error);
310 interfaces = g_key_file_get_string_list(config, "General",
311 CONF_BLACKLISTED_INTERFACES, &len, &error);
314 connman_settings.blacklisted_interfaces = interfaces;
316 connman_settings.blacklisted_interfaces =
317 g_strdupv(default_blacklist);
319 g_clear_error(&error);
321 boolean = g_key_file_get_boolean(config, "General",
322 CONF_ALLOW_HOSTNAME_UPDATES,
325 connman_settings.allow_hostname_updates = boolean;
327 g_clear_error(&error);
329 boolean = g_key_file_get_boolean(config, "General",
330 CONF_SINGLE_TECH, &error);
332 connman_settings.single_tech = boolean;
334 g_clear_error(&error);
336 tethering = g_key_file_get_string_list(config, "General",
337 CONF_ALLOWED_TETHERING_TECHNOLOGIES, &len, &error);
340 connman_settings.allowed_tethering_technologies = tethering;
342 g_clear_error(&error);
345 static int config_init(const char *file)
349 config = load_config(file);
350 check_config(config);
351 parse_config(config);
353 g_key_file_free(config);
358 static GMainLoop *main_loop = NULL;
360 static unsigned int __terminated = 0;
362 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
365 struct signalfd_siginfo si;
369 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
372 fd = g_io_channel_unix_get_fd(channel);
374 result = read(fd, &si, sizeof(si));
375 if (result != sizeof(si))
378 switch (si.ssi_signo) {
381 if (__terminated == 0) {
382 connman_info("Terminating");
383 g_main_loop_quit(main_loop);
393 static guint setup_signalfd(void)
401 sigaddset(&mask, SIGINT);
402 sigaddset(&mask, SIGTERM);
404 if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
405 perror("Failed to set signal mask");
409 fd = signalfd(-1, &mask, 0);
411 perror("Failed to create signal descriptor");
415 channel = g_io_channel_unix_new(fd);
417 g_io_channel_set_close_on_unref(channel, TRUE);
418 g_io_channel_set_encoding(channel, NULL, NULL);
419 g_io_channel_set_buffered(channel, FALSE);
421 source = g_io_add_watch(channel,
422 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
423 signal_handler, NULL);
425 g_io_channel_unref(channel);
430 static void disconnect_callback(DBusConnection *conn, void *user_data)
432 connman_error("D-Bus disconnect");
434 g_main_loop_quit(main_loop);
437 static gchar *option_config = NULL;
438 static gchar *option_debug = NULL;
439 static gchar *option_device = NULL;
440 static gchar *option_plugin = NULL;
441 static gchar *option_nodevice = NULL;
442 static gchar *option_noplugin = NULL;
443 static gchar *option_wifi = NULL;
444 static gboolean option_detach = TRUE;
445 static gboolean option_dnsproxy = TRUE;
446 static gboolean option_backtrace = TRUE;
447 static gboolean option_version = FALSE;
449 static gboolean parse_debug(const char *key, const char *value,
450 gpointer user_data, GError **error)
453 option_debug = g_strdup(value);
455 option_debug = g_strdup("*");
460 static GOptionEntry options[] = {
461 { "config", 'c', 0, G_OPTION_ARG_STRING, &option_config,
462 "Load the specified configuration file "
463 "instead of " CONFIGMAINFILE, "FILE" },
464 { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
465 G_OPTION_ARG_CALLBACK, parse_debug,
466 "Specify debug options to enable", "DEBUG" },
467 { "device", 'i', 0, G_OPTION_ARG_STRING, &option_device,
468 "Specify networking device or interface", "DEV" },
469 { "nodevice", 'I', 0, G_OPTION_ARG_STRING, &option_nodevice,
470 "Specify networking interface to ignore", "DEV" },
471 { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
472 "Specify plugins to load", "NAME,..." },
473 { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
474 "Specify plugins not to load", "NAME,..." },
475 { "wifi", 'W', 0, G_OPTION_ARG_STRING, &option_wifi,
476 "Specify driver for WiFi/Supplicant", "NAME" },
477 { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
478 G_OPTION_ARG_NONE, &option_detach,
479 "Don't fork daemon to background" },
480 { "nodnsproxy", 'r', G_OPTION_FLAG_REVERSE,
481 G_OPTION_ARG_NONE, &option_dnsproxy,
482 "Don't enable DNS Proxy" },
483 { "nobacktrace", 0, G_OPTION_FLAG_REVERSE,
484 G_OPTION_ARG_NONE, &option_backtrace,
485 "Don't print out backtrace information" },
486 { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
487 "Show version information and exit" },
491 const char *connman_option_get_string(const char *key)
493 if (g_strcmp0(key, "wifi") == 0) {
494 if (option_wifi == NULL)
495 return "nl80211,wext";
503 connman_bool_t connman_setting_get_bool(const char *key)
505 if (g_str_equal(key, CONF_BG_SCAN) == TRUE)
506 return connman_settings.bg_scan;
508 if (g_str_equal(key, CONF_ALLOW_HOSTNAME_UPDATES) == TRUE)
509 return connman_settings.allow_hostname_updates;
511 if (g_str_equal(key, CONF_SINGLE_TECH) == TRUE)
512 return connman_settings.single_tech;
517 char **connman_setting_get_string_list(const char *key)
519 if (g_str_equal(key, CONF_PREF_TIMESERVERS) == TRUE)
520 return connman_settings.pref_timeservers;
522 if (g_str_equal(key, CONF_FALLBACK_NAMESERVERS) == TRUE)
523 return connman_settings.fallback_nameservers;
525 if (g_str_equal(key, CONF_BLACKLISTED_INTERFACES) == TRUE)
526 return connman_settings.blacklisted_interfaces;
528 if (g_str_equal(key, CONF_ALLOWED_TETHERING_TECHNOLOGIES) == TRUE)
529 return connman_settings.allowed_tethering_technologies;
534 unsigned int *connman_setting_get_uint_list(const char *key)
536 if (g_str_equal(key, CONF_AUTO_CONNECT) == TRUE)
537 return connman_settings.auto_connect;
539 if (g_str_equal(key, CONF_PREFERRED_TECHS) == TRUE)
540 return connman_settings.preferred_techs;
545 unsigned int connman_timeout_input_request(void) {
546 return connman_settings.timeout_inputreq;
549 unsigned int connman_timeout_browser_launch(void) {
550 return connman_settings.timeout_browserlaunch;
553 int main(int argc, char *argv[])
555 GOptionContext *context;
556 GError *error = NULL;
557 DBusConnection *conn;
562 if (g_thread_supported() == FALSE)
566 context = g_option_context_new(NULL);
567 g_option_context_add_main_entries(context, options, NULL);
569 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
571 g_printerr("%s\n", error->message);
574 g_printerr("An unknown error occurred\n");
578 g_option_context_free(context);
580 if (option_version == TRUE) {
581 printf("%s\n", VERSION);
585 if (option_detach == TRUE) {
587 perror("Can't start daemon");
592 if (mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
593 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
595 perror("Failed to create state directory");
598 if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
599 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
601 perror("Failed to create storage directory");
606 main_loop = g_main_loop_new(NULL, FALSE);
609 if (dbus_threads_init_default() == FALSE) {
610 fprintf(stderr, "Can't init usage of threads\n");
615 signal = setup_signalfd();
617 dbus_error_init(&err);
619 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
621 if (dbus_error_is_set(&err) == TRUE) {
622 fprintf(stderr, "%s\n", err.message);
623 dbus_error_free(&err);
625 fprintf(stderr, "Can't register with system bus\n");
629 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
631 __connman_log_init(argv[0], option_debug, option_detach,
632 option_backtrace, "Connection Manager", VERSION);
634 __connman_dbus_init(conn);
636 if (option_config == NULL)
637 config_init(CONFIGMAINFILE);
639 config_init(option_config);
641 __connman_inotify_init();
642 __connman_technology_init();
643 __connman_notifier_init();
644 __connman_agent_init();
645 __connman_service_init();
646 __connman_provider_init();
647 __connman_network_init();
648 __connman_device_init(option_device, option_nodevice);
650 __connman_ippool_init();
651 __connman_iptables_init();
652 __connman_firewall_init();
653 __connman_nat_init();
654 __connman_tethering_init();
655 __connman_counter_init();
656 __connman_manager_init();
657 __connman_config_init();
658 __connman_stats_init();
659 __connman_clock_init();
661 __connman_resolver_init(option_dnsproxy);
662 __connman_ipconfig_init();
663 __connman_rtnl_init();
664 __connman_task_init();
665 __connman_proxy_init();
666 __connman_detect_init();
667 __connman_session_init();
668 __connman_timeserver_init();
669 __connman_connection_init();
671 __connman_plugin_init(option_plugin, option_noplugin);
673 __connman_rtnl_start();
674 __connman_dhcp_init();
675 __connman_dhcpv6_init();
676 __connman_wpad_init();
677 __connman_wispr_init();
678 __connman_rfkill_init();
680 g_free(option_config);
681 g_free(option_device);
682 g_free(option_plugin);
683 g_free(option_nodevice);
684 g_free(option_noplugin);
686 g_main_loop_run(main_loop);
688 g_source_remove(signal);
690 __connman_rfkill_cleanup();
691 __connman_wispr_cleanup();
692 __connman_wpad_cleanup();
693 __connman_dhcpv6_cleanup();
694 __connman_dhcp_cleanup();
695 __connman_session_cleanup();
696 __connman_plugin_cleanup();
697 __connman_provider_cleanup();
698 __connman_connection_cleanup();
699 __connman_timeserver_cleanup();
700 __connman_detect_cleanup();
701 __connman_proxy_cleanup();
702 __connman_task_cleanup();
703 __connman_rtnl_cleanup();
704 __connman_resolver_cleanup();
706 __connman_clock_cleanup();
707 __connman_stats_cleanup();
708 __connman_config_cleanup();
709 __connman_manager_cleanup();
710 __connman_counter_cleanup();
711 __connman_tethering_cleanup();
712 __connman_nat_cleanup();
713 __connman_firewall_cleanup();
714 __connman_iptables_cleanup();
715 __connman_ippool_cleanup();
716 __connman_device_cleanup();
717 __connman_network_cleanup();
718 __connman_service_cleanup();
719 __connman_agent_cleanup();
720 __connman_ipconfig_cleanup();
721 __connman_notifier_cleanup();
722 __connman_technology_cleanup();
723 __connman_inotify_cleanup();
725 __connman_dbus_cleanup();
727 __connman_log_cleanup(option_backtrace);
729 dbus_connection_unref(conn);
731 g_main_loop_unref(main_loop);
733 if (connman_settings.pref_timeservers != NULL)
734 g_strfreev(connman_settings.pref_timeservers);
736 g_free(connman_settings.auto_connect);
737 g_free(connman_settings.preferred_techs);
738 g_strfreev(connman_settings.fallback_nameservers);
739 g_strfreev(connman_settings.blacklisted_interfaces);
740 g_strfreev(connman_settings.allowed_tethering_technologies);
742 g_free(option_debug);