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 GKeyFile *load_config(const char *file)
101 keyfile = g_key_file_new();
103 g_key_file_set_list_separator(keyfile, ',');
105 if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
106 if (err->code != G_FILE_ERROR_NOENT) {
107 connman_error("Parsing %s failed: %s", file,
112 g_key_file_free(keyfile);
119 static uint *parse_service_types(char **str_list, gsize len)
121 unsigned int *type_list;
123 enum connman_service_type type;
125 type_list = g_try_new0(unsigned int, len + 1);
126 if (type_list == NULL)
131 while (str_list[i] != NULL)
133 type = __connman_service_string2type(str_list[i]);
135 if (type != CONNMAN_SERVICE_TYPE_UNKNOWN) {
145 static char **parse_fallback_nameservers(char **nameservers, gsize len)
150 servers = g_try_new0(char *, len + 1);
156 while (nameservers[i] != NULL) {
157 if (connman_inet_check_ipaddress(nameservers[i]) > 0) {
158 servers[j] = g_strdup(nameservers[i]);
167 static void parse_config(GKeyFile *config)
169 GError *error = NULL;
177 if (config == NULL) {
178 connman_settings.auto_connect =
179 parse_service_types(default_auto_connect, 3);
180 connman_settings.blacklisted_interfaces =
181 g_strdupv(default_blacklist);
185 DBG("parsing main.conf");
187 boolean = g_key_file_get_boolean(config, "General",
188 CONF_BG_SCAN, &error);
190 connman_settings.bg_scan = boolean;
192 g_clear_error(&error);
194 timeservers = g_key_file_get_string_list(config, "General",
195 CONF_PREF_TIMESERVERS, NULL, &error);
197 connman_settings.pref_timeservers = timeservers;
199 g_clear_error(&error);
201 str_list = g_key_file_get_string_list(config, "General",
202 CONF_AUTO_CONNECT, &len, &error);
205 connman_settings.auto_connect =
206 parse_service_types(str_list, len);
208 connman_settings.auto_connect =
209 parse_service_types(default_auto_connect, 3);
211 g_strfreev(str_list);
213 g_clear_error(&error);
215 str_list = g_key_file_get_string_list(config, "General",
216 CONF_PREFERRED_TECHS, &len, &error);
219 connman_settings.preferred_techs =
220 parse_service_types(str_list, len);
222 g_strfreev(str_list);
224 g_clear_error(&error);
226 str_list = g_key_file_get_string_list(config, "General",
227 CONF_FALLBACK_NAMESERVERS, &len, &error);
230 connman_settings.fallback_nameservers =
231 parse_fallback_nameservers(str_list, len);
233 g_strfreev(str_list);
235 g_clear_error(&error);
237 timeout = g_key_file_get_integer(config, "General",
238 CONF_TIMEOUT_INPUTREQ, &error);
239 if (error == NULL && timeout >= 0)
240 connman_settings.timeout_inputreq = timeout * 1000;
242 g_clear_error(&error);
244 timeout = g_key_file_get_integer(config, "General",
245 CONF_TIMEOUT_BROWSERLAUNCH, &error);
246 if (error == NULL && timeout >= 0)
247 connman_settings.timeout_browserlaunch = timeout * 1000;
249 g_clear_error(&error);
251 interfaces = g_key_file_get_string_list(config, "General",
252 CONF_BLACKLISTED_INTERFACES, &len, &error);
255 connman_settings.blacklisted_interfaces = interfaces;
257 connman_settings.blacklisted_interfaces =
258 g_strdupv(default_blacklist);
260 g_clear_error(&error);
262 boolean = g_key_file_get_boolean(config, "General",
263 CONF_ALLOW_HOSTNAME_UPDATES,
266 connman_settings.allow_hostname_updates = boolean;
268 g_clear_error(&error);
270 boolean = g_key_file_get_boolean(config, "General",
271 CONF_SINGLE_TECH, &error);
273 connman_settings.single_tech = boolean;
275 g_clear_error(&error);
278 static int config_init(const char *file)
282 config = load_config(file);
283 parse_config(config);
285 g_key_file_free(config);
290 static GMainLoop *main_loop = NULL;
292 static unsigned int __terminated = 0;
294 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
297 struct signalfd_siginfo si;
301 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
304 fd = g_io_channel_unix_get_fd(channel);
306 result = read(fd, &si, sizeof(si));
307 if (result != sizeof(si))
310 switch (si.ssi_signo) {
313 if (__terminated == 0) {
314 connman_info("Terminating");
315 g_main_loop_quit(main_loop);
325 static guint setup_signalfd(void)
333 sigaddset(&mask, SIGINT);
334 sigaddset(&mask, SIGTERM);
336 if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
337 perror("Failed to set signal mask");
341 fd = signalfd(-1, &mask, 0);
343 perror("Failed to create signal descriptor");
347 channel = g_io_channel_unix_new(fd);
349 g_io_channel_set_close_on_unref(channel, TRUE);
350 g_io_channel_set_encoding(channel, NULL, NULL);
351 g_io_channel_set_buffered(channel, FALSE);
353 source = g_io_add_watch(channel,
354 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
355 signal_handler, NULL);
357 g_io_channel_unref(channel);
362 static void disconnect_callback(DBusConnection *conn, void *user_data)
364 connman_error("D-Bus disconnect");
366 g_main_loop_quit(main_loop);
369 static gchar *option_config = NULL;
370 static gchar *option_debug = NULL;
371 static gchar *option_device = NULL;
372 static gchar *option_plugin = NULL;
373 static gchar *option_nodevice = NULL;
374 static gchar *option_noplugin = NULL;
375 static gchar *option_wifi = NULL;
376 static gboolean option_detach = TRUE;
377 static gboolean option_dnsproxy = TRUE;
378 static gboolean option_backtrace = TRUE;
379 static gboolean option_version = FALSE;
381 static gboolean parse_debug(const char *key, const char *value,
382 gpointer user_data, GError **error)
385 option_debug = g_strdup(value);
387 option_debug = g_strdup("*");
392 static GOptionEntry options[] = {
393 { "config", 'c', 0, G_OPTION_ARG_STRING, &option_config,
394 "Load the specified configuration file "
395 "instead of " CONFIGMAINFILE, "FILE" },
396 { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
397 G_OPTION_ARG_CALLBACK, parse_debug,
398 "Specify debug options to enable", "DEBUG" },
399 { "device", 'i', 0, G_OPTION_ARG_STRING, &option_device,
400 "Specify networking device or interface", "DEV" },
401 { "nodevice", 'I', 0, G_OPTION_ARG_STRING, &option_nodevice,
402 "Specify networking interface to ignore", "DEV" },
403 { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
404 "Specify plugins to load", "NAME,..." },
405 { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
406 "Specify plugins not to load", "NAME,..." },
407 { "wifi", 'W', 0, G_OPTION_ARG_STRING, &option_wifi,
408 "Specify driver for WiFi/Supplicant", "NAME" },
409 { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
410 G_OPTION_ARG_NONE, &option_detach,
411 "Don't fork daemon to background" },
412 { "nodnsproxy", 'r', G_OPTION_FLAG_REVERSE,
413 G_OPTION_ARG_NONE, &option_dnsproxy,
414 "Don't enable DNS Proxy" },
415 { "nobacktrace", 0, G_OPTION_FLAG_REVERSE,
416 G_OPTION_ARG_NONE, &option_backtrace,
417 "Don't print out backtrace information" },
418 { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
419 "Show version information and exit" },
423 const char *connman_option_get_string(const char *key)
425 if (g_strcmp0(key, "wifi") == 0) {
426 if (option_wifi == NULL)
427 return "nl80211,wext";
435 connman_bool_t connman_setting_get_bool(const char *key)
437 if (g_str_equal(key, CONF_BG_SCAN) == TRUE)
438 return connman_settings.bg_scan;
440 if (g_str_equal(key, CONF_ALLOW_HOSTNAME_UPDATES) == TRUE)
441 return connman_settings.allow_hostname_updates;
443 if (g_str_equal(key, CONF_SINGLE_TECH) == TRUE)
444 return connman_settings.single_tech;
449 char **connman_setting_get_string_list(const char *key)
451 if (g_str_equal(key, CONF_PREF_TIMESERVERS) == TRUE)
452 return connman_settings.pref_timeservers;
454 if (g_str_equal(key, CONF_FALLBACK_NAMESERVERS) == TRUE)
455 return connman_settings.fallback_nameservers;
457 if (g_str_equal(key, CONF_BLACKLISTED_INTERFACES) == TRUE)
458 return connman_settings.blacklisted_interfaces;
463 unsigned int *connman_setting_get_uint_list(const char *key)
465 if (g_str_equal(key, CONF_AUTO_CONNECT) == TRUE)
466 return connman_settings.auto_connect;
468 if (g_str_equal(key, CONF_PREFERRED_TECHS) == TRUE)
469 return connman_settings.preferred_techs;
474 unsigned int connman_timeout_input_request(void) {
475 return connman_settings.timeout_inputreq;
478 unsigned int connman_timeout_browser_launch(void) {
479 return connman_settings.timeout_browserlaunch;
482 int main(int argc, char *argv[])
484 GOptionContext *context;
485 GError *error = NULL;
486 DBusConnection *conn;
491 if (g_thread_supported() == FALSE)
495 context = g_option_context_new(NULL);
496 g_option_context_add_main_entries(context, options, NULL);
498 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
500 g_printerr("%s\n", error->message);
503 g_printerr("An unknown error occurred\n");
507 g_option_context_free(context);
509 if (option_version == TRUE) {
510 printf("%s\n", VERSION);
514 if (option_detach == TRUE) {
516 perror("Can't start daemon");
521 if (mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
522 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
524 perror("Failed to create state directory");
527 if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
528 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
530 perror("Failed to create storage directory");
535 main_loop = g_main_loop_new(NULL, FALSE);
538 if (dbus_threads_init_default() == FALSE) {
539 fprintf(stderr, "Can't init usage of threads\n");
544 signal = setup_signalfd();
546 dbus_error_init(&err);
548 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
550 if (dbus_error_is_set(&err) == TRUE) {
551 fprintf(stderr, "%s\n", err.message);
552 dbus_error_free(&err);
554 fprintf(stderr, "Can't register with system bus\n");
558 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
560 __connman_log_init(argv[0], option_debug, option_detach,
561 option_backtrace, "Connection Manager", VERSION);
563 __connman_dbus_init(conn);
565 if (option_config == NULL)
566 config_init(CONFIGMAINFILE);
568 config_init(option_config);
570 __connman_storage_migrate();
571 __connman_inotify_init();
572 __connman_technology_init();
573 __connman_notifier_init();
574 __connman_agent_init();
575 __connman_service_init();
576 __connman_provider_init();
577 __connman_network_init();
578 __connman_device_init(option_device, option_nodevice);
580 __connman_ippool_init();
581 __connman_iptables_init();
582 __connman_nat_init();
583 __connman_tethering_init();
584 __connman_counter_init();
585 __connman_manager_init();
586 __connman_config_init();
587 __connman_stats_init();
588 __connman_clock_init();
590 __connman_resolver_init(option_dnsproxy);
591 __connman_ipconfig_init();
592 __connman_rtnl_init();
593 __connman_task_init();
594 __connman_proxy_init();
595 __connman_detect_init();
596 __connman_session_init();
597 __connman_timeserver_init();
598 __connman_connection_init();
600 __connman_plugin_init(option_plugin, option_noplugin);
602 __connman_rtnl_start();
603 __connman_dhcp_init();
604 __connman_dhcpv6_init();
605 __connman_wpad_init();
606 __connman_wispr_init();
607 __connman_rfkill_init();
609 g_free(option_config);
610 g_free(option_device);
611 g_free(option_plugin);
612 g_free(option_nodevice);
613 g_free(option_noplugin);
615 g_main_loop_run(main_loop);
617 g_source_remove(signal);
619 __connman_rfkill_cleanup();
620 __connman_wispr_cleanup();
621 __connman_wpad_cleanup();
622 __connman_dhcpv6_cleanup();
623 __connman_dhcp_cleanup();
624 __connman_plugin_cleanup();
625 __connman_provider_cleanup();
626 __connman_connection_cleanup();
627 __connman_timeserver_cleanup();
628 __connman_session_cleanup();
629 __connman_detect_cleanup();
630 __connman_proxy_cleanup();
631 __connman_task_cleanup();
632 __connman_rtnl_cleanup();
633 __connman_resolver_cleanup();
635 __connman_clock_cleanup();
636 __connman_stats_cleanup();
637 __connman_config_cleanup();
638 __connman_manager_cleanup();
639 __connman_counter_cleanup();
640 __connman_tethering_cleanup();
641 __connman_nat_cleanup();
642 __connman_iptables_cleanup();
643 __connman_ippool_cleanup();
644 __connman_device_cleanup();
645 __connman_network_cleanup();
646 __connman_service_cleanup();
647 __connman_agent_cleanup();
648 __connman_ipconfig_cleanup();
649 __connman_notifier_cleanup();
650 __connman_technology_cleanup();
651 __connman_inotify_cleanup();
653 __connman_dbus_cleanup();
655 __connman_log_cleanup(option_backtrace);
657 dbus_connection_unref(conn);
659 g_main_loop_unref(main_loop);
661 if (connman_settings.pref_timeservers != NULL)
662 g_strfreev(connman_settings.pref_timeservers);
664 g_free(connman_settings.auto_connect);
665 g_free(connman_settings.preferred_techs);
666 g_strfreev(connman_settings.fallback_nameservers);
667 g_strfreev(connman_settings.blacklisted_interfaces);
669 g_free(option_debug);