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 CONFIGMAINFILE CONFIGDIR "/main.conf"
47 static char *default_auto_connect[] = {
54 static char *default_blacklist[] = {
62 connman_bool_t bg_scan;
63 char **pref_timeservers;
64 unsigned int *auto_connect;
65 unsigned int *preferred_techs;
66 char **fallback_nameservers;
67 unsigned int timeout_inputreq;
68 unsigned int timeout_browserlaunch;
69 char **blacklisted_interfaces;
70 connman_bool_t allow_hostname_updates;
71 connman_bool_t single_tech;
72 } connman_settings = {
74 .pref_timeservers = NULL,
76 .preferred_techs = NULL,
77 .fallback_nameservers = NULL,
78 .timeout_inputreq = DEFAULT_INPUT_REQUEST_TIMEOUT,
79 .timeout_browserlaunch = DEFAULT_BROWSER_LAUNCH_TIMEOUT,
80 .blacklisted_interfaces = NULL,
81 .allow_hostname_updates = TRUE,
85 #define CONF_BG_SCAN "BackgroundScanning"
86 #define CONF_PREF_TIMESERVERS "FallbackTimeservers"
87 #define CONF_AUTO_CONNECT "DefaultAutoConnectTechnologies"
88 #define CONF_PREFERRED_TECHS "PreferredTechnologies"
89 #define CONF_FALLBACK_NAMESERVERS "FallbackNameservers"
90 #define CONF_TIMEOUT_INPUTREQ "InputRequestTimeout"
91 #define CONF_TIMEOUT_BROWSERLAUNCH "BrowserLaunchTimeout"
92 #define CONF_BLACKLISTED_INTERFACES "NetworkInterfaceBlacklist"
93 #define CONF_ALLOW_HOSTNAME_UPDATES "AllowHostnameUpdates"
94 #define CONF_SINGLE_TECH "SingleConnectedTechnology"
96 static const char *supported_options[] = {
98 CONF_PREF_TIMESERVERS,
100 CONF_PREFERRED_TECHS,
101 CONF_FALLBACK_NAMESERVERS,
102 CONF_TIMEOUT_INPUTREQ,
103 CONF_TIMEOUT_BROWSERLAUNCH,
104 CONF_BLACKLISTED_INTERFACES,
105 CONF_ALLOW_HOSTNAME_UPDATES,
110 static GKeyFile *load_config(const char *file)
115 keyfile = g_key_file_new();
117 g_key_file_set_list_separator(keyfile, ',');
119 if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
120 if (err->code != G_FILE_ERROR_NOENT) {
121 connman_error("Parsing %s failed: %s", file,
126 g_key_file_free(keyfile);
133 static uint *parse_service_types(char **str_list, gsize len)
135 unsigned int *type_list;
137 enum connman_service_type type;
139 type_list = g_try_new0(unsigned int, len + 1);
140 if (type_list == NULL)
145 while (str_list[i] != NULL)
147 type = __connman_service_string2type(str_list[i]);
149 if (type != CONNMAN_SERVICE_TYPE_UNKNOWN) {
159 static char **parse_fallback_nameservers(char **nameservers, gsize len)
164 servers = g_try_new0(char *, len + 1);
170 while (nameservers[i] != NULL) {
171 if (connman_inet_check_ipaddress(nameservers[i]) > 0) {
172 servers[j] = g_strdup(nameservers[i]);
181 static void check_config(GKeyFile *config)
189 keys = g_key_file_get_groups(config, NULL);
191 for (j = 0; keys != NULL && keys[j] != NULL; j++) {
192 if (g_strcmp0(keys[j], "General") != 0)
193 connman_warn("Unknown group %s in main.conf", keys[j]);
198 keys = g_key_file_get_keys(config, "General", NULL, NULL);
200 for (j = 0; keys != NULL && keys[j] != NULL; j++) {
201 connman_bool_t found;
205 for (i = 0; supported_options[i] != NULL; i++) {
206 if (g_strcmp0(keys[j], supported_options[i]) == 0) {
211 if (found == FALSE && supported_options[i] == NULL)
212 connman_warn("Unknown key %s in main.conf", keys[j]);
218 static void parse_config(GKeyFile *config)
220 GError *error = NULL;
228 if (config == NULL) {
229 connman_settings.auto_connect =
230 parse_service_types(default_auto_connect, 3);
231 connman_settings.blacklisted_interfaces =
232 g_strdupv(default_blacklist);
236 DBG("parsing main.conf");
238 check_config(config);
240 boolean = g_key_file_get_boolean(config, "General",
241 CONF_BG_SCAN, &error);
243 connman_settings.bg_scan = boolean;
245 g_clear_error(&error);
247 timeservers = g_key_file_get_string_list(config, "General",
248 CONF_PREF_TIMESERVERS, NULL, &error);
250 connman_settings.pref_timeservers = timeservers;
252 g_clear_error(&error);
254 str_list = g_key_file_get_string_list(config, "General",
255 CONF_AUTO_CONNECT, &len, &error);
258 connman_settings.auto_connect =
259 parse_service_types(str_list, len);
261 connman_settings.auto_connect =
262 parse_service_types(default_auto_connect, 3);
264 g_strfreev(str_list);
266 g_clear_error(&error);
268 str_list = g_key_file_get_string_list(config, "General",
269 CONF_PREFERRED_TECHS, &len, &error);
272 connman_settings.preferred_techs =
273 parse_service_types(str_list, len);
275 g_strfreev(str_list);
277 g_clear_error(&error);
279 str_list = g_key_file_get_string_list(config, "General",
280 CONF_FALLBACK_NAMESERVERS, &len, &error);
283 connman_settings.fallback_nameservers =
284 parse_fallback_nameservers(str_list, len);
286 g_strfreev(str_list);
288 g_clear_error(&error);
290 timeout = g_key_file_get_integer(config, "General",
291 CONF_TIMEOUT_INPUTREQ, &error);
292 if (error == NULL && timeout >= 0)
293 connman_settings.timeout_inputreq = timeout * 1000;
295 g_clear_error(&error);
297 timeout = g_key_file_get_integer(config, "General",
298 CONF_TIMEOUT_BROWSERLAUNCH, &error);
299 if (error == NULL && timeout >= 0)
300 connman_settings.timeout_browserlaunch = timeout * 1000;
302 g_clear_error(&error);
304 interfaces = g_key_file_get_string_list(config, "General",
305 CONF_BLACKLISTED_INTERFACES, &len, &error);
308 connman_settings.blacklisted_interfaces = interfaces;
310 connman_settings.blacklisted_interfaces =
311 g_strdupv(default_blacklist);
313 g_clear_error(&error);
315 boolean = g_key_file_get_boolean(config, "General",
316 CONF_ALLOW_HOSTNAME_UPDATES,
319 connman_settings.allow_hostname_updates = boolean;
321 g_clear_error(&error);
323 boolean = g_key_file_get_boolean(config, "General",
324 CONF_SINGLE_TECH, &error);
326 connman_settings.single_tech = boolean;
328 g_clear_error(&error);
331 static int config_init(const char *file)
335 config = load_config(file);
336 parse_config(config);
338 g_key_file_free(config);
343 static GMainLoop *main_loop = NULL;
345 static unsigned int __terminated = 0;
347 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
350 struct signalfd_siginfo si;
354 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
357 fd = g_io_channel_unix_get_fd(channel);
359 result = read(fd, &si, sizeof(si));
360 if (result != sizeof(si))
363 switch (si.ssi_signo) {
366 if (__terminated == 0) {
367 connman_info("Terminating");
368 g_main_loop_quit(main_loop);
378 static guint setup_signalfd(void)
386 sigaddset(&mask, SIGINT);
387 sigaddset(&mask, SIGTERM);
389 if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
390 perror("Failed to set signal mask");
394 fd = signalfd(-1, &mask, 0);
396 perror("Failed to create signal descriptor");
400 channel = g_io_channel_unix_new(fd);
402 g_io_channel_set_close_on_unref(channel, TRUE);
403 g_io_channel_set_encoding(channel, NULL, NULL);
404 g_io_channel_set_buffered(channel, FALSE);
406 source = g_io_add_watch(channel,
407 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
408 signal_handler, NULL);
410 g_io_channel_unref(channel);
415 static void disconnect_callback(DBusConnection *conn, void *user_data)
417 connman_error("D-Bus disconnect");
419 g_main_loop_quit(main_loop);
422 static gchar *option_config = NULL;
423 static gchar *option_debug = NULL;
424 static gchar *option_device = NULL;
425 static gchar *option_plugin = NULL;
426 static gchar *option_nodevice = NULL;
427 static gchar *option_noplugin = NULL;
428 static gchar *option_wifi = NULL;
429 static gboolean option_detach = TRUE;
430 static gboolean option_dnsproxy = TRUE;
431 static gboolean option_backtrace = TRUE;
432 static gboolean option_version = FALSE;
434 static gboolean parse_debug(const char *key, const char *value,
435 gpointer user_data, GError **error)
438 option_debug = g_strdup(value);
440 option_debug = g_strdup("*");
445 static GOptionEntry options[] = {
446 { "config", 'c', 0, G_OPTION_ARG_STRING, &option_config,
447 "Load the specified configuration file "
448 "instead of " CONFIGMAINFILE, "FILE" },
449 { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
450 G_OPTION_ARG_CALLBACK, parse_debug,
451 "Specify debug options to enable", "DEBUG" },
452 { "device", 'i', 0, G_OPTION_ARG_STRING, &option_device,
453 "Specify networking device or interface", "DEV" },
454 { "nodevice", 'I', 0, G_OPTION_ARG_STRING, &option_nodevice,
455 "Specify networking interface to ignore", "DEV" },
456 { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
457 "Specify plugins to load", "NAME,..." },
458 { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
459 "Specify plugins not to load", "NAME,..." },
460 { "wifi", 'W', 0, G_OPTION_ARG_STRING, &option_wifi,
461 "Specify driver for WiFi/Supplicant", "NAME" },
462 { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
463 G_OPTION_ARG_NONE, &option_detach,
464 "Don't fork daemon to background" },
465 { "nodnsproxy", 'r', G_OPTION_FLAG_REVERSE,
466 G_OPTION_ARG_NONE, &option_dnsproxy,
467 "Don't enable DNS Proxy" },
468 { "nobacktrace", 0, G_OPTION_FLAG_REVERSE,
469 G_OPTION_ARG_NONE, &option_backtrace,
470 "Don't print out backtrace information" },
471 { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
472 "Show version information and exit" },
476 const char *connman_option_get_string(const char *key)
478 if (g_strcmp0(key, "wifi") == 0) {
479 if (option_wifi == NULL)
480 return "nl80211,wext";
488 connman_bool_t connman_setting_get_bool(const char *key)
490 if (g_str_equal(key, CONF_BG_SCAN) == TRUE)
491 return connman_settings.bg_scan;
493 if (g_str_equal(key, CONF_ALLOW_HOSTNAME_UPDATES) == TRUE)
494 return connman_settings.allow_hostname_updates;
496 if (g_str_equal(key, CONF_SINGLE_TECH) == TRUE)
497 return connman_settings.single_tech;
502 char **connman_setting_get_string_list(const char *key)
504 if (g_str_equal(key, CONF_PREF_TIMESERVERS) == TRUE)
505 return connman_settings.pref_timeservers;
507 if (g_str_equal(key, CONF_FALLBACK_NAMESERVERS) == TRUE)
508 return connman_settings.fallback_nameservers;
510 if (g_str_equal(key, CONF_BLACKLISTED_INTERFACES) == TRUE)
511 return connman_settings.blacklisted_interfaces;
516 unsigned int *connman_setting_get_uint_list(const char *key)
518 if (g_str_equal(key, CONF_AUTO_CONNECT) == TRUE)
519 return connman_settings.auto_connect;
521 if (g_str_equal(key, CONF_PREFERRED_TECHS) == TRUE)
522 return connman_settings.preferred_techs;
527 unsigned int connman_timeout_input_request(void) {
528 return connman_settings.timeout_inputreq;
531 unsigned int connman_timeout_browser_launch(void) {
532 return connman_settings.timeout_browserlaunch;
535 int main(int argc, char *argv[])
537 GOptionContext *context;
538 GError *error = NULL;
539 DBusConnection *conn;
544 if (g_thread_supported() == FALSE)
548 context = g_option_context_new(NULL);
549 g_option_context_add_main_entries(context, options, NULL);
551 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
553 g_printerr("%s\n", error->message);
556 g_printerr("An unknown error occurred\n");
560 g_option_context_free(context);
562 if (option_version == TRUE) {
563 printf("%s\n", VERSION);
567 if (option_detach == TRUE) {
569 perror("Can't start daemon");
574 if (mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
575 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
577 perror("Failed to create state directory");
580 if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
581 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
583 perror("Failed to create storage directory");
588 main_loop = g_main_loop_new(NULL, FALSE);
591 if (dbus_threads_init_default() == FALSE) {
592 fprintf(stderr, "Can't init usage of threads\n");
597 signal = setup_signalfd();
599 dbus_error_init(&err);
601 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
603 if (dbus_error_is_set(&err) == TRUE) {
604 fprintf(stderr, "%s\n", err.message);
605 dbus_error_free(&err);
607 fprintf(stderr, "Can't register with system bus\n");
611 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
613 __connman_log_init(argv[0], option_debug, option_detach,
614 option_backtrace, "Connection Manager", VERSION);
616 __connman_dbus_init(conn);
618 if (option_config == NULL)
619 config_init(CONFIGMAINFILE);
621 config_init(option_config);
623 __connman_storage_migrate();
624 __connman_inotify_init();
625 __connman_technology_init();
626 __connman_notifier_init();
627 __connman_agent_init();
628 __connman_service_init();
629 __connman_provider_init();
630 __connman_network_init();
631 __connman_device_init(option_device, option_nodevice);
633 __connman_ippool_init();
634 __connman_iptables_init();
635 __connman_nat_init();
636 __connman_tethering_init();
637 __connman_counter_init();
638 __connman_manager_init();
639 __connman_config_init();
640 __connman_stats_init();
641 __connman_clock_init();
643 __connman_resolver_init(option_dnsproxy);
644 __connman_ipconfig_init();
645 __connman_rtnl_init();
646 __connman_task_init();
647 __connman_proxy_init();
648 __connman_detect_init();
649 __connman_session_init();
650 __connman_timeserver_init();
651 __connman_connection_init();
653 __connman_plugin_init(option_plugin, option_noplugin);
655 __connman_rtnl_start();
656 __connman_dhcp_init();
657 __connman_dhcpv6_init();
658 __connman_wpad_init();
659 __connman_wispr_init();
660 __connman_rfkill_init();
662 g_free(option_config);
663 g_free(option_device);
664 g_free(option_plugin);
665 g_free(option_nodevice);
666 g_free(option_noplugin);
668 g_main_loop_run(main_loop);
670 g_source_remove(signal);
672 __connman_rfkill_cleanup();
673 __connman_wispr_cleanup();
674 __connman_wpad_cleanup();
675 __connman_dhcpv6_cleanup();
676 __connman_dhcp_cleanup();
677 __connman_plugin_cleanup();
678 __connman_provider_cleanup();
679 __connman_connection_cleanup();
680 __connman_timeserver_cleanup();
681 __connman_session_cleanup();
682 __connman_detect_cleanup();
683 __connman_proxy_cleanup();
684 __connman_task_cleanup();
685 __connman_rtnl_cleanup();
686 __connman_resolver_cleanup();
688 __connman_clock_cleanup();
689 __connman_stats_cleanup();
690 __connman_config_cleanup();
691 __connman_manager_cleanup();
692 __connman_counter_cleanup();
693 __connman_tethering_cleanup();
694 __connman_nat_cleanup();
695 __connman_iptables_cleanup();
696 __connman_ippool_cleanup();
697 __connman_device_cleanup();
698 __connman_network_cleanup();
699 __connman_service_cleanup();
700 __connman_agent_cleanup();
701 __connman_ipconfig_cleanup();
702 __connman_notifier_cleanup();
703 __connman_technology_cleanup();
704 __connman_inotify_cleanup();
706 __connman_dbus_cleanup();
708 __connman_log_cleanup(option_backtrace);
710 dbus_connection_unref(conn);
712 g_main_loop_unref(main_loop);
714 if (connman_settings.pref_timeservers != NULL)
715 g_strfreev(connman_settings.pref_timeservers);
717 g_free(connman_settings.auto_connect);
718 g_free(connman_settings.preferred_techs);
719 g_strfreev(connman_settings.fallback_nameservers);
720 g_strfreev(connman_settings.blacklisted_interfaces);
722 g_free(option_debug);