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 static GKeyFile *load_config(const char *file)
90 keyfile = g_key_file_new();
92 g_key_file_set_list_separator(keyfile, ',');
94 if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
95 if (err->code != G_FILE_ERROR_NOENT) {
96 connman_error("Parsing %s failed: %s", file,
101 g_key_file_free(keyfile);
108 static uint *parse_service_types(char **str_list, gsize len)
110 unsigned int *type_list;
112 enum connman_service_type type;
114 type_list = g_try_new0(unsigned int, len + 1);
115 if (type_list == NULL)
120 while (str_list[i] != NULL)
122 type = __connman_service_string2type(str_list[i]);
124 if (type != CONNMAN_SERVICE_TYPE_UNKNOWN) {
134 static char **parse_fallback_nameservers(char **nameservers, gsize len)
139 servers = g_try_new0(char *, len + 1);
145 while (nameservers[i] != NULL) {
146 if (connman_inet_check_ipaddress(nameservers[i]) > 0) {
147 servers[j] = g_strdup(nameservers[i]);
156 static void parse_config(GKeyFile *config)
158 GError *error = NULL;
166 if (config == NULL) {
167 connman_settings.auto_connect =
168 parse_service_types(default_auto_connect, 3);
169 connman_settings.blacklisted_interfaces =
170 g_strdupv(default_blacklist);
174 DBG("parsing main.conf");
176 boolean = g_key_file_get_boolean(config, "General",
177 "BackgroundScanning", &error);
179 connman_settings.bg_scan = boolean;
181 g_clear_error(&error);
183 timeservers = g_key_file_get_string_list(config, "General",
184 "FallbackTimeservers", NULL, &error);
186 connman_settings.pref_timeservers = timeservers;
188 g_clear_error(&error);
190 str_list = g_key_file_get_string_list(config, "General",
191 "DefaultAutoConnectTechnologies", &len, &error);
194 connman_settings.auto_connect =
195 parse_service_types(str_list, len);
197 connman_settings.auto_connect =
198 parse_service_types(default_auto_connect, 3);
200 g_strfreev(str_list);
202 g_clear_error(&error);
204 str_list = g_key_file_get_string_list(config, "General",
205 "PreferredTechnologies", &len, &error);
208 connman_settings.preferred_techs =
209 parse_service_types(str_list, len);
211 g_strfreev(str_list);
213 g_clear_error(&error);
215 str_list = g_key_file_get_string_list(config, "General",
216 "FallbackNameservers", &len, &error);
219 connman_settings.fallback_nameservers =
220 parse_fallback_nameservers(str_list, len);
222 g_strfreev(str_list);
224 g_clear_error(&error);
226 timeout = g_key_file_get_integer(config, "General",
227 "InputRequestTimeout", &error);
228 if (error == NULL && timeout >= 0)
229 connman_settings.timeout_inputreq = timeout * 1000;
231 g_clear_error(&error);
233 timeout = g_key_file_get_integer(config, "General",
234 "BrowserLaunchTimeout", &error);
235 if (error == NULL && timeout >= 0)
236 connman_settings.timeout_browserlaunch = timeout * 1000;
238 g_clear_error(&error);
240 interfaces = g_key_file_get_string_list(config, "General",
241 "NetworkInterfaceBlacklist", &len, &error);
244 connman_settings.blacklisted_interfaces = interfaces;
246 connman_settings.blacklisted_interfaces =
247 g_strdupv(default_blacklist);
249 g_clear_error(&error);
251 boolean = g_key_file_get_boolean(config, "General",
252 "AllowHostnameUpdates",
255 connman_settings.allow_hostname_updates = boolean;
257 g_clear_error(&error);
259 boolean = g_key_file_get_boolean(config, "General",
260 "SingleConnectedTechnology", &error);
262 connman_settings.single_tech = boolean;
264 g_clear_error(&error);
267 static int config_init(const char *file)
271 config = load_config(file);
272 parse_config(config);
274 g_key_file_free(config);
279 static GMainLoop *main_loop = NULL;
281 static unsigned int __terminated = 0;
283 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
286 struct signalfd_siginfo si;
290 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
293 fd = g_io_channel_unix_get_fd(channel);
295 result = read(fd, &si, sizeof(si));
296 if (result != sizeof(si))
299 switch (si.ssi_signo) {
302 if (__terminated == 0) {
303 connman_info("Terminating");
304 g_main_loop_quit(main_loop);
314 static guint setup_signalfd(void)
322 sigaddset(&mask, SIGINT);
323 sigaddset(&mask, SIGTERM);
325 if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
326 perror("Failed to set signal mask");
330 fd = signalfd(-1, &mask, 0);
332 perror("Failed to create signal descriptor");
336 channel = g_io_channel_unix_new(fd);
338 g_io_channel_set_close_on_unref(channel, TRUE);
339 g_io_channel_set_encoding(channel, NULL, NULL);
340 g_io_channel_set_buffered(channel, FALSE);
342 source = g_io_add_watch(channel,
343 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
344 signal_handler, NULL);
346 g_io_channel_unref(channel);
351 static void disconnect_callback(DBusConnection *conn, void *user_data)
353 connman_error("D-Bus disconnect");
355 g_main_loop_quit(main_loop);
358 static gchar *option_config = NULL;
359 static gchar *option_debug = NULL;
360 static gchar *option_device = NULL;
361 static gchar *option_plugin = NULL;
362 static gchar *option_nodevice = NULL;
363 static gchar *option_noplugin = NULL;
364 static gchar *option_wifi = NULL;
365 static gboolean option_detach = TRUE;
366 static gboolean option_dnsproxy = TRUE;
367 static gboolean option_backtrace = TRUE;
368 static gboolean option_version = FALSE;
370 static gboolean parse_debug(const char *key, const char *value,
371 gpointer user_data, GError **error)
374 option_debug = g_strdup(value);
376 option_debug = g_strdup("*");
381 static GOptionEntry options[] = {
382 { "config", 'c', 0, G_OPTION_ARG_STRING, &option_config,
383 "Load the specified configuration file "
384 "instead of " CONFIGMAINFILE, "FILE" },
385 { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
386 G_OPTION_ARG_CALLBACK, parse_debug,
387 "Specify debug options to enable", "DEBUG" },
388 { "device", 'i', 0, G_OPTION_ARG_STRING, &option_device,
389 "Specify networking device or interface", "DEV" },
390 { "nodevice", 'I', 0, G_OPTION_ARG_STRING, &option_nodevice,
391 "Specify networking interface to ignore", "DEV" },
392 { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
393 "Specify plugins to load", "NAME,..." },
394 { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
395 "Specify plugins not to load", "NAME,..." },
396 { "wifi", 'W', 0, G_OPTION_ARG_STRING, &option_wifi,
397 "Specify driver for WiFi/Supplicant", "NAME" },
398 { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
399 G_OPTION_ARG_NONE, &option_detach,
400 "Don't fork daemon to background" },
401 { "nodnsproxy", 'r', G_OPTION_FLAG_REVERSE,
402 G_OPTION_ARG_NONE, &option_dnsproxy,
403 "Don't enable DNS Proxy" },
404 { "nobacktrace", 0, G_OPTION_FLAG_REVERSE,
405 G_OPTION_ARG_NONE, &option_backtrace,
406 "Don't print out backtrace information" },
407 { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
408 "Show version information and exit" },
412 const char *connman_option_get_string(const char *key)
414 if (g_strcmp0(key, "wifi") == 0) {
415 if (option_wifi == NULL)
416 return "nl80211,wext";
424 connman_bool_t connman_setting_get_bool(const char *key)
426 if (g_str_equal(key, "BackgroundScanning") == TRUE)
427 return connman_settings.bg_scan;
429 if (g_str_equal(key, "AllowHostnameUpdates") == TRUE)
430 return connman_settings.allow_hostname_updates;
432 if (g_str_equal(key, "SingleConnectedTechnology") == TRUE)
433 return connman_settings.single_tech;
438 char **connman_setting_get_string_list(const char *key)
440 if (g_str_equal(key, "FallbackTimeservers") == TRUE)
441 return connman_settings.pref_timeservers;
443 if (g_str_equal(key, "FallbackNameservers") == TRUE)
444 return connman_settings.fallback_nameservers;
446 if (g_str_equal(key, "NetworkInterfaceBlacklist") == TRUE)
447 return connman_settings.blacklisted_interfaces;
452 unsigned int *connman_setting_get_uint_list(const char *key)
454 if (g_str_equal(key, "DefaultAutoConnectTechnologies") == TRUE)
455 return connman_settings.auto_connect;
457 if (g_str_equal(key, "PreferredTechnologies") == TRUE)
458 return connman_settings.preferred_techs;
463 unsigned int connman_timeout_input_request(void) {
464 return connman_settings.timeout_inputreq;
467 unsigned int connman_timeout_browser_launch(void) {
468 return connman_settings.timeout_browserlaunch;
471 int main(int argc, char *argv[])
473 GOptionContext *context;
474 GError *error = NULL;
475 DBusConnection *conn;
480 if (g_thread_supported() == FALSE)
484 context = g_option_context_new(NULL);
485 g_option_context_add_main_entries(context, options, NULL);
487 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
489 g_printerr("%s\n", error->message);
492 g_printerr("An unknown error occurred\n");
496 g_option_context_free(context);
498 if (option_version == TRUE) {
499 printf("%s\n", VERSION);
503 if (option_detach == TRUE) {
505 perror("Can't start daemon");
510 if (mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
511 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
513 perror("Failed to create state directory");
516 if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
517 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
519 perror("Failed to create storage directory");
524 main_loop = g_main_loop_new(NULL, FALSE);
527 if (dbus_threads_init_default() == FALSE) {
528 fprintf(stderr, "Can't init usage of threads\n");
533 signal = setup_signalfd();
535 dbus_error_init(&err);
537 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
539 if (dbus_error_is_set(&err) == TRUE) {
540 fprintf(stderr, "%s\n", err.message);
541 dbus_error_free(&err);
543 fprintf(stderr, "Can't register with system bus\n");
547 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
549 __connman_log_init(argv[0], option_debug, option_detach,
550 option_backtrace, "Connection Manager", VERSION);
552 __connman_dbus_init(conn);
554 if (option_config == NULL)
555 config_init(CONFIGMAINFILE);
557 config_init(option_config);
559 __connman_storage_migrate();
560 __connman_technology_init();
561 __connman_notifier_init();
562 __connman_agent_init();
563 __connman_service_init();
564 __connman_provider_init();
565 __connman_network_init();
566 __connman_device_init(option_device, option_nodevice);
568 __connman_ippool_init();
569 __connman_iptables_init();
570 __connman_nat_init();
571 __connman_tethering_init();
572 __connman_counter_init();
573 __connman_manager_init();
574 __connman_config_init();
575 __connman_stats_init();
576 __connman_clock_init();
578 __connman_resolver_init(option_dnsproxy);
579 __connman_ipconfig_init();
580 __connman_rtnl_init();
581 __connman_task_init();
582 __connman_proxy_init();
583 __connman_detect_init();
584 __connman_session_init();
585 __connman_timeserver_init();
586 __connman_connection_init();
588 __connman_plugin_init(option_plugin, option_noplugin);
590 __connman_rtnl_start();
591 __connman_dhcp_init();
592 __connman_dhcpv6_init();
593 __connman_wpad_init();
594 __connman_wispr_init();
595 __connman_rfkill_init();
597 g_free(option_config);
598 g_free(option_device);
599 g_free(option_plugin);
600 g_free(option_nodevice);
601 g_free(option_noplugin);
603 g_main_loop_run(main_loop);
605 g_source_remove(signal);
607 __connman_rfkill_cleanup();
608 __connman_wispr_cleanup();
609 __connman_wpad_cleanup();
610 __connman_dhcpv6_cleanup();
611 __connman_dhcp_cleanup();
612 __connman_plugin_cleanup();
613 __connman_provider_cleanup();
614 __connman_connection_cleanup();
615 __connman_timeserver_cleanup();
616 __connman_session_cleanup();
617 __connman_detect_cleanup();
618 __connman_proxy_cleanup();
619 __connman_task_cleanup();
620 __connman_rtnl_cleanup();
621 __connman_resolver_cleanup();
623 __connman_clock_cleanup();
624 __connman_stats_cleanup();
625 __connman_config_cleanup();
626 __connman_manager_cleanup();
627 __connman_counter_cleanup();
628 __connman_tethering_cleanup();
629 __connman_nat_cleanup();
630 __connman_iptables_cleanup();
631 __connman_ippool_cleanup();
632 __connman_device_cleanup();
633 __connman_network_cleanup();
634 __connman_service_cleanup();
635 __connman_agent_cleanup();
636 __connman_ipconfig_cleanup();
637 __connman_notifier_cleanup();
638 __connman_technology_cleanup();
640 __connman_dbus_cleanup();
642 __connman_log_cleanup(option_backtrace);
644 dbus_connection_unref(conn);
646 g_main_loop_unref(main_loop);
648 if (connman_settings.pref_timeservers != NULL)
649 g_strfreev(connman_settings.pref_timeservers);
651 g_free(connman_settings.auto_connect);
652 g_free(connman_settings.preferred_techs);
653 g_strfreev(connman_settings.fallback_nameservers);
654 g_strfreev(connman_settings.blacklisted_interfaces);
656 g_free(option_debug);