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_settings = {
72 .pref_timeservers = NULL,
74 .preferred_techs = NULL,
75 .fallback_nameservers = NULL,
76 .timeout_inputreq = DEFAULT_INPUT_REQUEST_TIMEOUT,
77 .timeout_browserlaunch = DEFAULT_BROWSER_LAUNCH_TIMEOUT,
78 .blacklisted_interfaces = NULL,
81 static GKeyFile *load_config(const char *file)
86 keyfile = g_key_file_new();
88 g_key_file_set_list_separator(keyfile, ',');
90 if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
91 if (err->code != G_FILE_ERROR_NOENT) {
92 connman_error("Parsing %s failed: %s", file,
97 g_key_file_free(keyfile);
104 static uint *parse_service_types(char **str_list, gsize len)
106 unsigned int *type_list;
108 enum connman_service_type type;
110 type_list = g_try_new0(unsigned int, len + 1);
111 if (type_list == NULL)
116 while (str_list[i] != NULL)
118 type = __connman_service_string2type(str_list[i]);
120 if (type != CONNMAN_SERVICE_TYPE_UNKNOWN) {
130 static char **parse_fallback_nameservers(char **nameservers, gsize len)
135 servers = g_try_new0(char *, len + 1);
141 while (nameservers[i] != NULL) {
142 if (connman_inet_check_ipaddress(nameservers[i]) > 0) {
143 servers[j] = g_strdup(nameservers[i]);
152 static void parse_config(GKeyFile *config)
154 GError *error = NULL;
162 if (config == NULL) {
163 connman_settings.auto_connect =
164 parse_service_types(default_auto_connect, 3);
165 connman_settings.blacklisted_interfaces =
166 g_strdupv(default_blacklist);
170 DBG("parsing main.conf");
172 boolean = g_key_file_get_boolean(config, "General",
173 "BackgroundScanning", &error);
175 connman_settings.bg_scan = boolean;
177 g_clear_error(&error);
179 timeservers = g_key_file_get_string_list(config, "General",
180 "FallbackTimeservers", NULL, &error);
182 connman_settings.pref_timeservers = timeservers;
184 g_clear_error(&error);
186 str_list = g_key_file_get_string_list(config, "General",
187 "DefaultAutoConnectTechnologies", &len, &error);
190 connman_settings.auto_connect =
191 parse_service_types(str_list, len);
193 connman_settings.auto_connect =
194 parse_service_types(default_auto_connect, 3);
196 g_strfreev(str_list);
198 g_clear_error(&error);
200 str_list = g_key_file_get_string_list(config, "General",
201 "PreferredTechnologies", &len, &error);
204 connman_settings.preferred_techs =
205 parse_service_types(str_list, len);
207 g_strfreev(str_list);
209 g_clear_error(&error);
211 str_list = g_key_file_get_string_list(config, "General",
212 "FallbackNameservers", &len, &error);
215 connman_settings.fallback_nameservers =
216 parse_fallback_nameservers(str_list, len);
218 g_strfreev(str_list);
220 g_clear_error(&error);
222 timeout = g_key_file_get_integer(config, "General",
223 "InputRequestTimeout", &error);
224 if (error == NULL && timeout >= 0)
225 connman_settings.timeout_inputreq = timeout * 1000;
227 g_clear_error(&error);
229 timeout = g_key_file_get_integer(config, "General",
230 "BrowserLaunchTimeout", &error);
231 if (error == NULL && timeout >= 0)
232 connman_settings.timeout_browserlaunch = timeout * 1000;
234 g_clear_error(&error);
236 interfaces = g_key_file_get_string_list(config, "General",
237 "NetworkInterfaceBlacklist", &len, &error);
240 connman_settings.blacklisted_interfaces = interfaces;
242 connman_settings.blacklisted_interfaces =
243 g_strdupv(default_blacklist);
245 g_clear_error(&error);
248 static int config_init(const char *file)
252 config = load_config(file);
253 parse_config(config);
255 g_key_file_free(config);
260 static GMainLoop *main_loop = NULL;
262 static unsigned int __terminated = 0;
264 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
267 struct signalfd_siginfo si;
271 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
274 fd = g_io_channel_unix_get_fd(channel);
276 result = read(fd, &si, sizeof(si));
277 if (result != sizeof(si))
280 switch (si.ssi_signo) {
283 if (__terminated == 0) {
284 connman_info("Terminating");
285 g_main_loop_quit(main_loop);
295 static guint setup_signalfd(void)
303 sigaddset(&mask, SIGINT);
304 sigaddset(&mask, SIGTERM);
306 if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
307 perror("Failed to set signal mask");
311 fd = signalfd(-1, &mask, 0);
313 perror("Failed to create signal descriptor");
317 channel = g_io_channel_unix_new(fd);
319 g_io_channel_set_close_on_unref(channel, TRUE);
320 g_io_channel_set_encoding(channel, NULL, NULL);
321 g_io_channel_set_buffered(channel, FALSE);
323 source = g_io_add_watch(channel,
324 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
325 signal_handler, NULL);
327 g_io_channel_unref(channel);
332 static void disconnect_callback(DBusConnection *conn, void *user_data)
334 connman_error("D-Bus disconnect");
336 g_main_loop_quit(main_loop);
339 static gchar *option_config = NULL;
340 static gchar *option_debug = NULL;
341 static gchar *option_device = NULL;
342 static gchar *option_plugin = NULL;
343 static gchar *option_nodevice = NULL;
344 static gchar *option_noplugin = NULL;
345 static gchar *option_wifi = NULL;
346 static gboolean option_detach = TRUE;
347 static gboolean option_dnsproxy = TRUE;
348 static gboolean option_version = FALSE;
350 static gboolean parse_debug(const char *key, const char *value,
351 gpointer user_data, GError **error)
354 option_debug = g_strdup(value);
356 option_debug = g_strdup("*");
361 static GOptionEntry options[] = {
362 { "config", 'c', 0, G_OPTION_ARG_STRING, &option_config,
363 "Load the specified configuration file "
364 "instead of " CONFIGMAINFILE, "FILE" },
365 { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
366 G_OPTION_ARG_CALLBACK, parse_debug,
367 "Specify debug options to enable", "DEBUG" },
368 { "device", 'i', 0, G_OPTION_ARG_STRING, &option_device,
369 "Specify networking device or interface", "DEV" },
370 { "nodevice", 'I', 0, G_OPTION_ARG_STRING, &option_nodevice,
371 "Specify networking interface to ignore", "DEV" },
372 { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
373 "Specify plugins to load", "NAME,..." },
374 { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
375 "Specify plugins not to load", "NAME,..." },
376 { "wifi", 'W', 0, G_OPTION_ARG_STRING, &option_wifi,
377 "Specify driver for WiFi/Supplicant", "NAME" },
378 { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
379 G_OPTION_ARG_NONE, &option_detach,
380 "Don't fork daemon to background" },
381 { "nodnsproxy", 'r', G_OPTION_FLAG_REVERSE,
382 G_OPTION_ARG_NONE, &option_dnsproxy,
383 "Don't enable DNS Proxy" },
384 { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
385 "Show version information and exit" },
389 const char *connman_option_get_string(const char *key)
391 if (g_strcmp0(key, "wifi") == 0) {
392 if (option_wifi == NULL)
393 return "nl80211,wext";
401 connman_bool_t connman_setting_get_bool(const char *key)
403 if (g_str_equal(key, "BackgroundScanning") == TRUE)
404 return connman_settings.bg_scan;
409 char **connman_setting_get_string_list(const char *key)
411 if (g_str_equal(key, "FallbackTimeservers") == TRUE)
412 return connman_settings.pref_timeservers;
414 if (g_str_equal(key, "FallbackNameservers") == TRUE)
415 return connman_settings.fallback_nameservers;
417 if (g_str_equal(key, "NetworkInterfaceBlacklist") == TRUE)
418 return connman_settings.blacklisted_interfaces;
423 unsigned int *connman_setting_get_uint_list(const char *key)
425 if (g_str_equal(key, "DefaultAutoConnectTechnologies") == TRUE)
426 return connman_settings.auto_connect;
428 if (g_str_equal(key, "PreferredTechnologies") == TRUE)
429 return connman_settings.preferred_techs;
434 unsigned int connman_timeout_input_request(void) {
435 return connman_settings.timeout_inputreq;
438 unsigned int connman_timeout_browser_launch(void) {
439 return connman_settings.timeout_browserlaunch;
442 int main(int argc, char *argv[])
444 GOptionContext *context;
445 GError *error = NULL;
446 DBusConnection *conn;
451 if (g_thread_supported() == FALSE)
455 context = g_option_context_new(NULL);
456 g_option_context_add_main_entries(context, options, NULL);
458 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
460 g_printerr("%s\n", error->message);
463 g_printerr("An unknown error occurred\n");
467 g_option_context_free(context);
469 if (option_version == TRUE) {
470 printf("%s\n", VERSION);
474 if (option_detach == TRUE) {
476 perror("Can't start daemon");
481 if (mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
482 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
484 perror("Failed to create state directory");
487 if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
488 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
490 perror("Failed to create storage directory");
495 main_loop = g_main_loop_new(NULL, FALSE);
498 if (dbus_threads_init_default() == FALSE) {
499 fprintf(stderr, "Can't init usage of threads\n");
504 signal = setup_signalfd();
506 dbus_error_init(&err);
508 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
510 if (dbus_error_is_set(&err) == TRUE) {
511 fprintf(stderr, "%s\n", err.message);
512 dbus_error_free(&err);
514 fprintf(stderr, "Can't register with system bus\n");
518 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
520 __connman_log_init(argv[0], option_debug, option_detach);
522 __connman_dbus_init(conn);
524 if (option_config == NULL)
525 config_init(CONFIGMAINFILE);
527 config_init(option_config);
529 __connman_storage_migrate();
530 __connman_technology_init();
531 __connman_notifier_init();
532 __connman_service_init();
533 __connman_provider_init();
534 __connman_network_init();
535 __connman_device_init(option_device, option_nodevice);
537 __connman_agent_init();
538 __connman_ippool_init();
539 __connman_iptables_init();
540 __connman_nat_init();
541 __connman_tethering_init();
542 __connman_counter_init();
543 __connman_manager_init();
544 __connman_config_init();
545 __connman_stats_init();
546 __connman_clock_init();
548 __connman_resolver_init(option_dnsproxy);
549 __connman_ipconfig_init();
550 __connman_rtnl_init();
551 __connman_task_init();
552 __connman_proxy_init();
553 __connman_detect_init();
554 __connman_session_init();
555 __connman_timeserver_init();
556 __connman_connection_init();
558 __connman_plugin_init(option_plugin, option_noplugin);
560 __connman_rtnl_start();
561 __connman_dhcp_init();
562 __connman_dhcpv6_init();
563 __connman_wpad_init();
564 __connman_wispr_init();
565 __connman_rfkill_init();
567 g_free(option_config);
568 g_free(option_device);
569 g_free(option_plugin);
570 g_free(option_nodevice);
571 g_free(option_noplugin);
573 g_main_loop_run(main_loop);
575 g_source_remove(signal);
577 __connman_rfkill_cleanup();
578 __connman_wispr_cleanup();
579 __connman_wpad_cleanup();
580 __connman_dhcpv6_cleanup();
581 __connman_dhcp_cleanup();
582 __connman_provider_cleanup();
583 __connman_plugin_cleanup();
584 __connman_connection_cleanup();
585 __connman_timeserver_cleanup();
586 __connman_session_cleanup();
587 __connman_detect_cleanup();
588 __connman_proxy_cleanup();
589 __connman_task_cleanup();
590 __connman_rtnl_cleanup();
591 __connman_resolver_cleanup();
593 __connman_clock_cleanup();
594 __connman_stats_cleanup();
595 __connman_config_cleanup();
596 __connman_manager_cleanup();
597 __connman_counter_cleanup();
598 __connman_agent_cleanup();
599 __connman_tethering_cleanup();
600 __connman_nat_cleanup();
601 __connman_iptables_cleanup();
602 __connman_ippool_cleanup();
603 __connman_device_cleanup();
604 __connman_network_cleanup();
605 __connman_service_cleanup();
606 __connman_ipconfig_cleanup();
607 __connman_notifier_cleanup();
608 __connman_technology_cleanup();
610 __connman_dbus_cleanup();
612 __connman_log_cleanup();
614 dbus_connection_unref(conn);
616 g_main_loop_unref(main_loop);
618 if (connman_settings.pref_timeservers != NULL)
619 g_strfreev(connman_settings.pref_timeservers);
621 g_free(connman_settings.auto_connect);
622 g_free(connman_settings.preferred_techs);
623 g_strfreev(connman_settings.fallback_nameservers);
624 g_strfreev(connman_settings.blacklisted_interfaces);
626 g_free(option_debug);