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 } connman_settings = {
75 .pref_timeservers = NULL,
77 .preferred_techs = NULL,
78 .fallback_nameservers = NULL,
79 .timeout_inputreq = DEFAULT_INPUT_REQUEST_TIMEOUT,
80 .timeout_browserlaunch = DEFAULT_BROWSER_LAUNCH_TIMEOUT,
81 .blacklisted_interfaces = NULL,
82 .allow_hostname_updates = TRUE,
86 #define CONF_BG_SCAN "BackgroundScanning"
87 #define CONF_PREF_TIMESERVERS "FallbackTimeservers"
88 #define CONF_AUTO_CONNECT "DefaultAutoConnectTechnologies"
89 #define CONF_PREFERRED_TECHS "PreferredTechnologies"
90 #define CONF_FALLBACK_NAMESERVERS "FallbackNameservers"
91 #define CONF_TIMEOUT_INPUTREQ "InputRequestTimeout"
92 #define CONF_TIMEOUT_BROWSERLAUNCH "BrowserLaunchTimeout"
93 #define CONF_BLACKLISTED_INTERFACES "NetworkInterfaceBlacklist"
94 #define CONF_ALLOW_HOSTNAME_UPDATES "AllowHostnameUpdates"
95 #define CONF_SINGLE_TECH "SingleConnectedTechnology"
97 static const char *supported_options[] = {
99 CONF_PREF_TIMESERVERS,
101 CONF_PREFERRED_TECHS,
102 CONF_FALLBACK_NAMESERVERS,
103 CONF_TIMEOUT_INPUTREQ,
104 CONF_TIMEOUT_BROWSERLAUNCH,
105 CONF_BLACKLISTED_INTERFACES,
106 CONF_ALLOW_HOSTNAME_UPDATES,
111 static GKeyFile *load_config(const char *file)
116 keyfile = g_key_file_new();
118 g_key_file_set_list_separator(keyfile, ',');
120 if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
121 if (err->code != G_FILE_ERROR_NOENT) {
122 connman_error("Parsing %s failed: %s", file,
127 g_key_file_free(keyfile);
134 static uint *parse_service_types(char **str_list, gsize len)
136 unsigned int *type_list;
138 enum connman_service_type type;
140 type_list = g_try_new0(unsigned int, len + 1);
141 if (type_list == NULL)
146 while (str_list[i] != NULL)
148 type = __connman_service_string2type(str_list[i]);
150 if (type != CONNMAN_SERVICE_TYPE_UNKNOWN) {
160 static char **parse_fallback_nameservers(char **nameservers, gsize len)
165 servers = g_try_new0(char *, len + 1);
171 while (nameservers[i] != NULL) {
172 if (connman_inet_check_ipaddress(nameservers[i]) > 0) {
173 servers[j] = g_strdup(nameservers[i]);
182 static void check_config(GKeyFile *config)
190 keys = g_key_file_get_groups(config, NULL);
192 for (j = 0; keys != NULL && keys[j] != NULL; j++) {
193 if (g_strcmp0(keys[j], "General") != 0)
194 connman_warn("Unknown group %s in %s",
200 keys = g_key_file_get_keys(config, "General", NULL, NULL);
202 for (j = 0; keys != NULL && keys[j] != NULL; j++) {
203 connman_bool_t found;
207 for (i = 0; supported_options[i] != NULL; i++) {
208 if (g_strcmp0(keys[j], supported_options[i]) == 0) {
213 if (found == FALSE && supported_options[i] == NULL)
214 connman_warn("Unknown option %s in %s",
221 static void parse_config(GKeyFile *config)
223 GError *error = NULL;
231 if (config == NULL) {
232 connman_settings.auto_connect =
233 parse_service_types(default_auto_connect, 3);
234 connman_settings.blacklisted_interfaces =
235 g_strdupv(default_blacklist);
239 DBG("parsing %s", MAINFILE);
241 boolean = g_key_file_get_boolean(config, "General",
242 CONF_BG_SCAN, &error);
244 connman_settings.bg_scan = boolean;
246 g_clear_error(&error);
248 timeservers = g_key_file_get_string_list(config, "General",
249 CONF_PREF_TIMESERVERS, NULL, &error);
251 connman_settings.pref_timeservers = timeservers;
253 g_clear_error(&error);
255 str_list = g_key_file_get_string_list(config, "General",
256 CONF_AUTO_CONNECT, &len, &error);
259 connman_settings.auto_connect =
260 parse_service_types(str_list, len);
262 connman_settings.auto_connect =
263 parse_service_types(default_auto_connect, 3);
265 g_strfreev(str_list);
267 g_clear_error(&error);
269 str_list = g_key_file_get_string_list(config, "General",
270 CONF_PREFERRED_TECHS, &len, &error);
273 connman_settings.preferred_techs =
274 parse_service_types(str_list, len);
276 g_strfreev(str_list);
278 g_clear_error(&error);
280 str_list = g_key_file_get_string_list(config, "General",
281 CONF_FALLBACK_NAMESERVERS, &len, &error);
284 connman_settings.fallback_nameservers =
285 parse_fallback_nameservers(str_list, len);
287 g_strfreev(str_list);
289 g_clear_error(&error);
291 timeout = g_key_file_get_integer(config, "General",
292 CONF_TIMEOUT_INPUTREQ, &error);
293 if (error == NULL && timeout >= 0)
294 connman_settings.timeout_inputreq = timeout * 1000;
296 g_clear_error(&error);
298 timeout = g_key_file_get_integer(config, "General",
299 CONF_TIMEOUT_BROWSERLAUNCH, &error);
300 if (error == NULL && timeout >= 0)
301 connman_settings.timeout_browserlaunch = timeout * 1000;
303 g_clear_error(&error);
305 interfaces = g_key_file_get_string_list(config, "General",
306 CONF_BLACKLISTED_INTERFACES, &len, &error);
309 connman_settings.blacklisted_interfaces = interfaces;
311 connman_settings.blacklisted_interfaces =
312 g_strdupv(default_blacklist);
314 g_clear_error(&error);
316 boolean = g_key_file_get_boolean(config, "General",
317 CONF_ALLOW_HOSTNAME_UPDATES,
320 connman_settings.allow_hostname_updates = boolean;
322 g_clear_error(&error);
324 boolean = g_key_file_get_boolean(config, "General",
325 CONF_SINGLE_TECH, &error);
327 connman_settings.single_tech = boolean;
329 g_clear_error(&error);
332 static int config_init(const char *file)
336 config = load_config(file);
337 check_config(config);
338 parse_config(config);
340 g_key_file_free(config);
345 static GMainLoop *main_loop = NULL;
347 static unsigned int __terminated = 0;
349 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
352 struct signalfd_siginfo si;
356 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
359 fd = g_io_channel_unix_get_fd(channel);
361 result = read(fd, &si, sizeof(si));
362 if (result != sizeof(si))
365 switch (si.ssi_signo) {
368 if (__terminated == 0) {
369 connman_info("Terminating");
370 g_main_loop_quit(main_loop);
380 static guint setup_signalfd(void)
388 sigaddset(&mask, SIGINT);
389 sigaddset(&mask, SIGTERM);
391 if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
392 perror("Failed to set signal mask");
396 fd = signalfd(-1, &mask, 0);
398 perror("Failed to create signal descriptor");
402 channel = g_io_channel_unix_new(fd);
404 g_io_channel_set_close_on_unref(channel, TRUE);
405 g_io_channel_set_encoding(channel, NULL, NULL);
406 g_io_channel_set_buffered(channel, FALSE);
408 source = g_io_add_watch(channel,
409 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
410 signal_handler, NULL);
412 g_io_channel_unref(channel);
417 static void disconnect_callback(DBusConnection *conn, void *user_data)
419 connman_error("D-Bus disconnect");
421 g_main_loop_quit(main_loop);
424 static gchar *option_config = NULL;
425 static gchar *option_debug = NULL;
426 static gchar *option_device = NULL;
427 static gchar *option_plugin = NULL;
428 static gchar *option_nodevice = NULL;
429 static gchar *option_noplugin = NULL;
430 static gchar *option_wifi = NULL;
431 static gboolean option_detach = TRUE;
432 static gboolean option_dnsproxy = TRUE;
433 static gboolean option_backtrace = TRUE;
434 static gboolean option_version = FALSE;
436 static gboolean parse_debug(const char *key, const char *value,
437 gpointer user_data, GError **error)
440 option_debug = g_strdup(value);
442 option_debug = g_strdup("*");
447 static GOptionEntry options[] = {
448 { "config", 'c', 0, G_OPTION_ARG_STRING, &option_config,
449 "Load the specified configuration file "
450 "instead of " CONFIGMAINFILE, "FILE" },
451 { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
452 G_OPTION_ARG_CALLBACK, parse_debug,
453 "Specify debug options to enable", "DEBUG" },
454 { "device", 'i', 0, G_OPTION_ARG_STRING, &option_device,
455 "Specify networking device or interface", "DEV" },
456 { "nodevice", 'I', 0, G_OPTION_ARG_STRING, &option_nodevice,
457 "Specify networking interface to ignore", "DEV" },
458 { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
459 "Specify plugins to load", "NAME,..." },
460 { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
461 "Specify plugins not to load", "NAME,..." },
462 { "wifi", 'W', 0, G_OPTION_ARG_STRING, &option_wifi,
463 "Specify driver for WiFi/Supplicant", "NAME" },
464 { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
465 G_OPTION_ARG_NONE, &option_detach,
466 "Don't fork daemon to background" },
467 { "nodnsproxy", 'r', G_OPTION_FLAG_REVERSE,
468 G_OPTION_ARG_NONE, &option_dnsproxy,
469 "Don't enable DNS Proxy" },
470 { "nobacktrace", 0, G_OPTION_FLAG_REVERSE,
471 G_OPTION_ARG_NONE, &option_backtrace,
472 "Don't print out backtrace information" },
473 { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
474 "Show version information and exit" },
478 const char *connman_option_get_string(const char *key)
480 if (g_strcmp0(key, "wifi") == 0) {
481 if (option_wifi == NULL)
482 return "nl80211,wext";
490 connman_bool_t connman_setting_get_bool(const char *key)
492 if (g_str_equal(key, CONF_BG_SCAN) == TRUE)
493 return connman_settings.bg_scan;
495 if (g_str_equal(key, CONF_ALLOW_HOSTNAME_UPDATES) == TRUE)
496 return connman_settings.allow_hostname_updates;
498 if (g_str_equal(key, CONF_SINGLE_TECH) == TRUE)
499 return connman_settings.single_tech;
504 char **connman_setting_get_string_list(const char *key)
506 if (g_str_equal(key, CONF_PREF_TIMESERVERS) == TRUE)
507 return connman_settings.pref_timeservers;
509 if (g_str_equal(key, CONF_FALLBACK_NAMESERVERS) == TRUE)
510 return connman_settings.fallback_nameservers;
512 if (g_str_equal(key, CONF_BLACKLISTED_INTERFACES) == TRUE)
513 return connman_settings.blacklisted_interfaces;
518 unsigned int *connman_setting_get_uint_list(const char *key)
520 if (g_str_equal(key, CONF_AUTO_CONNECT) == TRUE)
521 return connman_settings.auto_connect;
523 if (g_str_equal(key, CONF_PREFERRED_TECHS) == TRUE)
524 return connman_settings.preferred_techs;
529 unsigned int connman_timeout_input_request(void) {
530 return connman_settings.timeout_inputreq;
533 unsigned int connman_timeout_browser_launch(void) {
534 return connman_settings.timeout_browserlaunch;
537 int main(int argc, char *argv[])
539 GOptionContext *context;
540 GError *error = NULL;
541 DBusConnection *conn;
546 if (g_thread_supported() == FALSE)
550 context = g_option_context_new(NULL);
551 g_option_context_add_main_entries(context, options, NULL);
553 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
555 g_printerr("%s\n", error->message);
558 g_printerr("An unknown error occurred\n");
562 g_option_context_free(context);
564 if (option_version == TRUE) {
565 printf("%s\n", VERSION);
569 if (option_detach == TRUE) {
571 perror("Can't start daemon");
576 if (mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
577 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
579 perror("Failed to create state directory");
582 if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
583 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
585 perror("Failed to create storage directory");
590 main_loop = g_main_loop_new(NULL, FALSE);
593 if (dbus_threads_init_default() == FALSE) {
594 fprintf(stderr, "Can't init usage of threads\n");
599 signal = setup_signalfd();
601 dbus_error_init(&err);
603 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
605 if (dbus_error_is_set(&err) == TRUE) {
606 fprintf(stderr, "%s\n", err.message);
607 dbus_error_free(&err);
609 fprintf(stderr, "Can't register with system bus\n");
613 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
615 __connman_log_init(argv[0], option_debug, option_detach,
616 option_backtrace, "Connection Manager", VERSION);
618 __connman_dbus_init(conn);
620 if (option_config == NULL)
621 config_init(CONFIGMAINFILE);
623 config_init(option_config);
625 __connman_inotify_init();
626 __connman_technology_init();
627 __connman_notifier_init();
628 __connman_agent_init();
629 __connman_service_init();
630 __connman_provider_init();
631 __connman_network_init();
632 __connman_device_init(option_device, option_nodevice);
634 __connman_ippool_init();
635 __connman_iptables_init();
636 __connman_nat_init();
637 __connman_tethering_init();
638 __connman_counter_init();
639 __connman_manager_init();
640 __connman_config_init();
641 __connman_stats_init();
642 __connman_clock_init();
644 __connman_resolver_init(option_dnsproxy);
645 __connman_ipconfig_init();
646 __connman_rtnl_init();
647 __connman_task_init();
648 __connman_proxy_init();
649 __connman_detect_init();
650 __connman_session_init();
651 __connman_timeserver_init();
652 __connman_connection_init();
654 __connman_plugin_init(option_plugin, option_noplugin);
656 __connman_rtnl_start();
657 __connman_dhcp_init();
658 __connman_dhcpv6_init();
659 __connman_wpad_init();
660 __connman_wispr_init();
661 __connman_rfkill_init();
663 g_free(option_config);
664 g_free(option_device);
665 g_free(option_plugin);
666 g_free(option_nodevice);
667 g_free(option_noplugin);
669 g_main_loop_run(main_loop);
671 g_source_remove(signal);
673 __connman_rfkill_cleanup();
674 __connman_wispr_cleanup();
675 __connman_wpad_cleanup();
676 __connman_dhcpv6_cleanup();
677 __connman_dhcp_cleanup();
678 __connman_plugin_cleanup();
679 __connman_provider_cleanup();
680 __connman_connection_cleanup();
681 __connman_timeserver_cleanup();
682 __connman_session_cleanup();
683 __connman_detect_cleanup();
684 __connman_proxy_cleanup();
685 __connman_task_cleanup();
686 __connman_rtnl_cleanup();
687 __connman_resolver_cleanup();
689 __connman_clock_cleanup();
690 __connman_stats_cleanup();
691 __connman_config_cleanup();
692 __connman_manager_cleanup();
693 __connman_counter_cleanup();
694 __connman_tethering_cleanup();
695 __connman_nat_cleanup();
696 __connman_iptables_cleanup();
697 __connman_ippool_cleanup();
698 __connman_device_cleanup();
699 __connman_network_cleanup();
700 __connman_service_cleanup();
701 __connman_agent_cleanup();
702 __connman_ipconfig_cleanup();
703 __connman_notifier_cleanup();
704 __connman_technology_cleanup();
705 __connman_inotify_cleanup();
707 __connman_dbus_cleanup();
709 __connman_log_cleanup(option_backtrace);
711 dbus_connection_unref(conn);
713 g_main_loop_unref(main_loop);
715 if (connman_settings.pref_timeservers != NULL)
716 g_strfreev(connman_settings.pref_timeservers);
718 g_free(connman_settings.auto_connect);
719 g_free(connman_settings.preferred_techs);
720 g_strfreev(connman_settings.fallback_nameservers);
721 g_strfreev(connman_settings.blacklisted_interfaces);
723 g_free(option_debug);