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_bool_t persistent_tethering_mode;
75 } connman_settings = {
77 .pref_timeservers = NULL,
79 .preferred_techs = NULL,
80 .fallback_nameservers = NULL,
81 .timeout_inputreq = DEFAULT_INPUT_REQUEST_TIMEOUT,
82 .timeout_browserlaunch = DEFAULT_BROWSER_LAUNCH_TIMEOUT,
83 .blacklisted_interfaces = NULL,
84 .allow_hostname_updates = TRUE,
86 .allowed_tethering_technologies = NULL,
87 .persistent_tethering_mode = FALSE,
90 #define CONF_BG_SCAN "BackgroundScanning"
91 #define CONF_PREF_TIMESERVERS "FallbackTimeservers"
92 #define CONF_AUTO_CONNECT "DefaultAutoConnectTechnologies"
93 #define CONF_PREFERRED_TECHS "PreferredTechnologies"
94 #define CONF_FALLBACK_NAMESERVERS "FallbackNameservers"
95 #define CONF_TIMEOUT_INPUTREQ "InputRequestTimeout"
96 #define CONF_TIMEOUT_BROWSERLAUNCH "BrowserLaunchTimeout"
97 #define CONF_BLACKLISTED_INTERFACES "NetworkInterfaceBlacklist"
98 #define CONF_ALLOW_HOSTNAME_UPDATES "AllowHostnameUpdates"
99 #define CONF_SINGLE_TECH "SingleConnectedTechnology"
100 #define CONF_ALLOWED_TETHERING_TECHNOLOGIES "AllowedTetheringTechnologies"
101 #define CONF_PERSISTENT_TETHERING_MODE "PersistentTetheringMode"
103 static const char *supported_options[] = {
105 CONF_PREF_TIMESERVERS,
107 CONF_PREFERRED_TECHS,
108 CONF_FALLBACK_NAMESERVERS,
109 CONF_TIMEOUT_INPUTREQ,
110 CONF_TIMEOUT_BROWSERLAUNCH,
111 CONF_BLACKLISTED_INTERFACES,
112 CONF_ALLOW_HOSTNAME_UPDATES,
114 CONF_ALLOWED_TETHERING_TECHNOLOGIES,
115 CONF_PERSISTENT_TETHERING_MODE,
119 static GKeyFile *load_config(const char *file)
124 keyfile = g_key_file_new();
126 g_key_file_set_list_separator(keyfile, ',');
128 if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
129 if (err->code != G_FILE_ERROR_NOENT) {
130 connman_error("Parsing %s failed: %s", file,
135 g_key_file_free(keyfile);
142 static uint *parse_service_types(char **str_list, gsize len)
144 unsigned int *type_list;
146 enum connman_service_type type;
148 type_list = g_try_new0(unsigned int, len + 1);
149 if (type_list == NULL)
154 while (str_list[i] != NULL)
156 type = __connman_service_string2type(str_list[i]);
158 if (type != CONNMAN_SERVICE_TYPE_UNKNOWN) {
168 static char **parse_fallback_nameservers(char **nameservers, gsize len)
173 servers = g_try_new0(char *, len + 1);
179 while (nameservers[i] != NULL) {
180 if (connman_inet_check_ipaddress(nameservers[i]) > 0) {
181 servers[j] = g_strdup(nameservers[i]);
190 static void check_config(GKeyFile *config)
198 keys = g_key_file_get_groups(config, NULL);
200 for (j = 0; keys != NULL && keys[j] != NULL; j++) {
201 if (g_strcmp0(keys[j], "General") != 0)
202 connman_warn("Unknown group %s in %s",
208 keys = g_key_file_get_keys(config, "General", NULL, NULL);
210 for (j = 0; keys != NULL && keys[j] != NULL; j++) {
211 connman_bool_t found;
215 for (i = 0; supported_options[i] != NULL; i++) {
216 if (g_strcmp0(keys[j], supported_options[i]) == 0) {
221 if (found == FALSE && supported_options[i] == NULL)
222 connman_warn("Unknown option %s in %s",
229 static void parse_config(GKeyFile *config)
231 GError *error = NULL;
240 if (config == NULL) {
241 connman_settings.auto_connect =
242 parse_service_types(default_auto_connect, 3);
243 connman_settings.blacklisted_interfaces =
244 g_strdupv(default_blacklist);
248 DBG("parsing %s", MAINFILE);
250 boolean = g_key_file_get_boolean(config, "General",
251 CONF_BG_SCAN, &error);
253 connman_settings.bg_scan = boolean;
255 g_clear_error(&error);
257 timeservers = g_key_file_get_string_list(config, "General",
258 CONF_PREF_TIMESERVERS, NULL, &error);
260 connman_settings.pref_timeservers = timeservers;
262 g_clear_error(&error);
264 str_list = g_key_file_get_string_list(config, "General",
265 CONF_AUTO_CONNECT, &len, &error);
268 connman_settings.auto_connect =
269 parse_service_types(str_list, len);
271 connman_settings.auto_connect =
272 parse_service_types(default_auto_connect, 3);
274 g_strfreev(str_list);
276 g_clear_error(&error);
278 str_list = g_key_file_get_string_list(config, "General",
279 CONF_PREFERRED_TECHS, &len, &error);
282 connman_settings.preferred_techs =
283 parse_service_types(str_list, len);
285 g_strfreev(str_list);
287 g_clear_error(&error);
289 str_list = g_key_file_get_string_list(config, "General",
290 CONF_FALLBACK_NAMESERVERS, &len, &error);
293 connman_settings.fallback_nameservers =
294 parse_fallback_nameservers(str_list, len);
296 g_strfreev(str_list);
298 g_clear_error(&error);
300 timeout = g_key_file_get_integer(config, "General",
301 CONF_TIMEOUT_INPUTREQ, &error);
302 if (error == NULL && timeout >= 0)
303 connman_settings.timeout_inputreq = timeout * 1000;
305 g_clear_error(&error);
307 timeout = g_key_file_get_integer(config, "General",
308 CONF_TIMEOUT_BROWSERLAUNCH, &error);
309 if (error == NULL && timeout >= 0)
310 connman_settings.timeout_browserlaunch = timeout * 1000;
312 g_clear_error(&error);
314 interfaces = g_key_file_get_string_list(config, "General",
315 CONF_BLACKLISTED_INTERFACES, &len, &error);
318 connman_settings.blacklisted_interfaces = interfaces;
320 connman_settings.blacklisted_interfaces =
321 g_strdupv(default_blacklist);
323 g_clear_error(&error);
325 boolean = g_key_file_get_boolean(config, "General",
326 CONF_ALLOW_HOSTNAME_UPDATES,
329 connman_settings.allow_hostname_updates = boolean;
331 g_clear_error(&error);
333 boolean = g_key_file_get_boolean(config, "General",
334 CONF_SINGLE_TECH, &error);
336 connman_settings.single_tech = boolean;
338 g_clear_error(&error);
340 tethering = g_key_file_get_string_list(config, "General",
341 CONF_ALLOWED_TETHERING_TECHNOLOGIES, &len, &error);
344 connman_settings.allowed_tethering_technologies = tethering;
346 g_clear_error(&error);
348 boolean = g_key_file_get_boolean(config, "General",
349 CONF_PERSISTENT_TETHERING_MODE,
352 connman_settings.persistent_tethering_mode = boolean;
354 g_clear_error(&error);
357 static int config_init(const char *file)
361 config = load_config(file);
362 check_config(config);
363 parse_config(config);
365 g_key_file_free(config);
370 static GMainLoop *main_loop = NULL;
372 static unsigned int __terminated = 0;
374 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
377 struct signalfd_siginfo si;
381 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
384 fd = g_io_channel_unix_get_fd(channel);
386 result = read(fd, &si, sizeof(si));
387 if (result != sizeof(si))
390 switch (si.ssi_signo) {
393 if (__terminated == 0) {
394 connman_info("Terminating");
395 g_main_loop_quit(main_loop);
405 static guint setup_signalfd(void)
413 sigaddset(&mask, SIGINT);
414 sigaddset(&mask, SIGTERM);
416 if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
417 perror("Failed to set signal mask");
421 fd = signalfd(-1, &mask, 0);
423 perror("Failed to create signal descriptor");
427 channel = g_io_channel_unix_new(fd);
429 g_io_channel_set_close_on_unref(channel, TRUE);
430 g_io_channel_set_encoding(channel, NULL, NULL);
431 g_io_channel_set_buffered(channel, FALSE);
433 source = g_io_add_watch(channel,
434 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
435 signal_handler, NULL);
437 g_io_channel_unref(channel);
442 static void disconnect_callback(DBusConnection *conn, void *user_data)
444 connman_error("D-Bus disconnect");
446 g_main_loop_quit(main_loop);
449 static gchar *option_config = NULL;
450 static gchar *option_debug = NULL;
451 static gchar *option_device = NULL;
452 static gchar *option_plugin = NULL;
453 static gchar *option_nodevice = NULL;
454 static gchar *option_noplugin = NULL;
455 static gchar *option_wifi = NULL;
456 static gboolean option_detach = TRUE;
457 static gboolean option_dnsproxy = TRUE;
458 static gboolean option_backtrace = TRUE;
459 static gboolean option_version = FALSE;
461 static gboolean parse_debug(const char *key, const char *value,
462 gpointer user_data, GError **error)
465 option_debug = g_strdup(value);
467 option_debug = g_strdup("*");
472 static GOptionEntry options[] = {
473 { "config", 'c', 0, G_OPTION_ARG_STRING, &option_config,
474 "Load the specified configuration file "
475 "instead of " CONFIGMAINFILE, "FILE" },
476 { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
477 G_OPTION_ARG_CALLBACK, parse_debug,
478 "Specify debug options to enable", "DEBUG" },
479 { "device", 'i', 0, G_OPTION_ARG_STRING, &option_device,
480 "Specify networking device or interface", "DEV" },
481 { "nodevice", 'I', 0, G_OPTION_ARG_STRING, &option_nodevice,
482 "Specify networking interface to ignore", "DEV" },
483 { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
484 "Specify plugins to load", "NAME,..." },
485 { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
486 "Specify plugins not to load", "NAME,..." },
487 { "wifi", 'W', 0, G_OPTION_ARG_STRING, &option_wifi,
488 "Specify driver for WiFi/Supplicant", "NAME" },
489 { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
490 G_OPTION_ARG_NONE, &option_detach,
491 "Don't fork daemon to background" },
492 { "nodnsproxy", 'r', G_OPTION_FLAG_REVERSE,
493 G_OPTION_ARG_NONE, &option_dnsproxy,
494 "Don't enable DNS Proxy" },
495 { "nobacktrace", 0, G_OPTION_FLAG_REVERSE,
496 G_OPTION_ARG_NONE, &option_backtrace,
497 "Don't print out backtrace information" },
498 { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
499 "Show version information and exit" },
503 const char *connman_option_get_string(const char *key)
505 if (g_strcmp0(key, "wifi") == 0) {
506 if (option_wifi == NULL)
507 return "nl80211,wext";
515 connman_bool_t connman_setting_get_bool(const char *key)
517 if (g_str_equal(key, CONF_BG_SCAN) == TRUE)
518 return connman_settings.bg_scan;
520 if (g_str_equal(key, CONF_ALLOW_HOSTNAME_UPDATES) == TRUE)
521 return connman_settings.allow_hostname_updates;
523 if (g_str_equal(key, CONF_SINGLE_TECH) == TRUE)
524 return connman_settings.single_tech;
526 if (g_str_equal(key, CONF_PERSISTENT_TETHERING_MODE) == TRUE)
527 return connman_settings.persistent_tethering_mode;
532 char **connman_setting_get_string_list(const char *key)
534 if (g_str_equal(key, CONF_PREF_TIMESERVERS) == TRUE)
535 return connman_settings.pref_timeservers;
537 if (g_str_equal(key, CONF_FALLBACK_NAMESERVERS) == TRUE)
538 return connman_settings.fallback_nameservers;
540 if (g_str_equal(key, CONF_BLACKLISTED_INTERFACES) == TRUE)
541 return connman_settings.blacklisted_interfaces;
543 if (g_str_equal(key, CONF_ALLOWED_TETHERING_TECHNOLOGIES) == TRUE)
544 return connman_settings.allowed_tethering_technologies;
549 unsigned int *connman_setting_get_uint_list(const char *key)
551 if (g_str_equal(key, CONF_AUTO_CONNECT) == TRUE)
552 return connman_settings.auto_connect;
554 if (g_str_equal(key, CONF_PREFERRED_TECHS) == TRUE)
555 return connman_settings.preferred_techs;
560 unsigned int connman_timeout_input_request(void) {
561 return connman_settings.timeout_inputreq;
564 unsigned int connman_timeout_browser_launch(void) {
565 return connman_settings.timeout_browserlaunch;
568 int main(int argc, char *argv[])
570 GOptionContext *context;
571 GError *error = NULL;
572 DBusConnection *conn;
577 if (g_thread_supported() == FALSE)
581 context = g_option_context_new(NULL);
582 g_option_context_add_main_entries(context, options, NULL);
584 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
586 g_printerr("%s\n", error->message);
589 g_printerr("An unknown error occurred\n");
593 g_option_context_free(context);
595 if (option_version == TRUE) {
596 printf("%s\n", VERSION);
600 if (option_detach == TRUE) {
602 perror("Can't start daemon");
607 if (mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
608 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
610 perror("Failed to create state directory");
613 if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
614 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
616 perror("Failed to create storage directory");
621 main_loop = g_main_loop_new(NULL, FALSE);
624 if (dbus_threads_init_default() == FALSE) {
625 fprintf(stderr, "Can't init usage of threads\n");
630 signal = setup_signalfd();
632 dbus_error_init(&err);
634 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
636 if (dbus_error_is_set(&err) == TRUE) {
637 fprintf(stderr, "%s\n", err.message);
638 dbus_error_free(&err);
640 fprintf(stderr, "Can't register with system bus\n");
644 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
646 __connman_log_init(argv[0], option_debug, option_detach,
647 option_backtrace, "Connection Manager", VERSION);
649 __connman_dbus_init(conn);
651 if (option_config == NULL)
652 config_init(CONFIGMAINFILE);
654 config_init(option_config);
656 __connman_inotify_init();
657 __connman_technology_init();
658 __connman_notifier_init();
659 __connman_agent_init();
660 __connman_service_init();
661 __connman_provider_init();
662 __connman_network_init();
663 __connman_device_init(option_device, option_nodevice);
665 __connman_ippool_init();
666 __connman_iptables_init();
667 __connman_firewall_init();
668 __connman_nat_init();
669 __connman_tethering_init();
670 __connman_counter_init();
671 __connman_manager_init();
672 __connman_config_init();
673 __connman_stats_init();
674 __connman_clock_init();
676 __connman_resolver_init(option_dnsproxy);
677 __connman_ipconfig_init();
678 __connman_rtnl_init();
679 __connman_task_init();
680 __connman_proxy_init();
681 __connman_detect_init();
682 __connman_session_init();
683 __connman_timeserver_init();
684 __connman_connection_init();
686 __connman_plugin_init(option_plugin, option_noplugin);
688 __connman_rtnl_start();
689 __connman_dhcp_init();
690 __connman_dhcpv6_init();
691 __connman_wpad_init();
692 __connman_wispr_init();
693 __connman_rfkill_init();
695 g_free(option_config);
696 g_free(option_device);
697 g_free(option_plugin);
698 g_free(option_nodevice);
699 g_free(option_noplugin);
701 g_main_loop_run(main_loop);
703 g_source_remove(signal);
705 __connman_rfkill_cleanup();
706 __connman_wispr_cleanup();
707 __connman_wpad_cleanup();
708 __connman_dhcpv6_cleanup();
709 __connman_dhcp_cleanup();
710 __connman_session_cleanup();
711 __connman_plugin_cleanup();
712 __connman_provider_cleanup();
713 __connman_connection_cleanup();
714 __connman_timeserver_cleanup();
715 __connman_detect_cleanup();
716 __connman_proxy_cleanup();
717 __connman_task_cleanup();
718 __connman_rtnl_cleanup();
719 __connman_resolver_cleanup();
721 __connman_clock_cleanup();
722 __connman_stats_cleanup();
723 __connman_config_cleanup();
724 __connman_manager_cleanup();
725 __connman_counter_cleanup();
726 __connman_tethering_cleanup();
727 __connman_nat_cleanup();
728 __connman_firewall_cleanup();
729 __connman_iptables_cleanup();
730 __connman_ippool_cleanup();
731 __connman_device_cleanup();
732 __connman_network_cleanup();
733 __connman_service_cleanup();
734 __connman_agent_cleanup();
735 __connman_ipconfig_cleanup();
736 __connman_notifier_cleanup();
737 __connman_technology_cleanup();
738 __connman_inotify_cleanup();
740 __connman_dbus_cleanup();
742 __connman_log_cleanup(option_backtrace);
744 dbus_connection_unref(conn);
746 g_main_loop_unref(main_loop);
748 if (connman_settings.pref_timeservers != NULL)
749 g_strfreev(connman_settings.pref_timeservers);
751 g_free(connman_settings.auto_connect);
752 g_free(connman_settings.preferred_techs);
753 g_strfreev(connman_settings.fallback_nameservers);
754 g_strfreev(connman_settings.blacklisted_interfaces);
755 g_strfreev(connman_settings.allowed_tethering_technologies);
757 g_free(option_debug);