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 static char *default_auto_connect[] = {
52 static char *default_blacklist[] = {
60 connman_bool_t bg_scan;
61 char **pref_timeservers;
62 unsigned int *auto_connect;
63 unsigned int *preferred_techs;
64 char **fallback_nameservers;
65 unsigned int timeout_inputreq;
66 unsigned int timeout_browserlaunch;
67 char **blacklisted_interfaces;
68 } connman_settings = {
70 .pref_timeservers = NULL,
72 .preferred_techs = NULL,
73 .fallback_nameservers = NULL,
74 .timeout_inputreq = DEFAULT_INPUT_REQUEST_TIMEOUT,
75 .timeout_browserlaunch = DEFAULT_BROWSER_LAUNCH_TIMEOUT,
76 .blacklisted_interfaces = NULL,
79 static GKeyFile *load_config(const char *file)
84 keyfile = g_key_file_new();
86 g_key_file_set_list_separator(keyfile, ',');
88 if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
89 if (err->code != G_FILE_ERROR_NOENT) {
90 connman_error("Parsing %s failed: %s", file,
95 g_key_file_free(keyfile);
102 static uint *parse_service_types(char **str_list, gsize len)
104 unsigned int *type_list;
106 enum connman_service_type type;
108 type_list = g_try_new0(unsigned int, len + 1);
109 if (type_list == NULL)
114 while (str_list[i] != NULL)
116 type = __connman_service_string2type(str_list[i]);
118 if (type != CONNMAN_SERVICE_TYPE_UNKNOWN) {
128 static char **parse_fallback_nameservers(char **nameservers, gsize len)
133 servers = g_try_new0(char *, len + 1);
139 while (nameservers[i] != NULL) {
140 if (connman_inet_check_ipaddress(nameservers[i]) > 0) {
141 servers[j] = g_strdup(nameservers[i]);
150 static void parse_config(GKeyFile *config)
152 GError *error = NULL;
160 if (config == NULL) {
161 connman_settings.auto_connect =
162 parse_service_types(default_auto_connect, 3);
163 connman_settings.blacklisted_interfaces =
164 g_strdupv(default_blacklist);
168 DBG("parsing main.conf");
170 boolean = g_key_file_get_boolean(config, "General",
171 "BackgroundScanning", &error);
173 connman_settings.bg_scan = boolean;
175 g_clear_error(&error);
177 timeservers = g_key_file_get_string_list(config, "General",
178 "FallbackTimeservers", NULL, &error);
180 connman_settings.pref_timeservers = timeservers;
182 g_clear_error(&error);
184 str_list = g_key_file_get_string_list(config, "General",
185 "DefaultAutoConnectTechnologies", &len, &error);
188 connman_settings.auto_connect =
189 parse_service_types(str_list, len);
191 connman_settings.auto_connect =
192 parse_service_types(default_auto_connect, 3);
194 g_strfreev(str_list);
196 g_clear_error(&error);
198 str_list = g_key_file_get_string_list(config, "General",
199 "PreferredTechnologies", &len, &error);
202 connman_settings.preferred_techs =
203 parse_service_types(str_list, len);
205 g_strfreev(str_list);
207 g_clear_error(&error);
209 str_list = g_key_file_get_string_list(config, "General",
210 "FallbackNameservers", &len, &error);
213 connman_settings.fallback_nameservers =
214 parse_fallback_nameservers(str_list, len);
216 g_strfreev(str_list);
218 g_clear_error(&error);
220 timeout = g_key_file_get_integer(config, "General",
221 "InputRequestTimeout", &error);
222 if (error == NULL && timeout >= 0)
223 connman_settings.timeout_inputreq = timeout * 1000;
225 g_clear_error(&error);
227 timeout = g_key_file_get_integer(config, "General",
228 "BrowserLaunchTimeout", &error);
229 if (error == NULL && timeout >= 0)
230 connman_settings.timeout_browserlaunch = timeout * 1000;
232 g_clear_error(&error);
234 interfaces = g_key_file_get_string_list(config, "General",
235 "NetworkInterfaceBlacklist", &len, &error);
238 connman_settings.blacklisted_interfaces = interfaces;
240 connman_settings.blacklisted_interfaces =
241 g_strdupv(default_blacklist);
243 g_clear_error(&error);
246 static GMainLoop *main_loop = NULL;
248 static unsigned int __terminated = 0;
250 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
253 struct signalfd_siginfo si;
257 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
260 fd = g_io_channel_unix_get_fd(channel);
262 result = read(fd, &si, sizeof(si));
263 if (result != sizeof(si))
266 switch (si.ssi_signo) {
269 if (__terminated == 0) {
270 connman_info("Terminating");
271 g_main_loop_quit(main_loop);
281 static guint setup_signalfd(void)
289 sigaddset(&mask, SIGINT);
290 sigaddset(&mask, SIGTERM);
292 if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
293 perror("Failed to set signal mask");
297 fd = signalfd(-1, &mask, 0);
299 perror("Failed to create signal descriptor");
303 channel = g_io_channel_unix_new(fd);
305 g_io_channel_set_close_on_unref(channel, TRUE);
306 g_io_channel_set_encoding(channel, NULL, NULL);
307 g_io_channel_set_buffered(channel, FALSE);
309 source = g_io_add_watch(channel,
310 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
311 signal_handler, NULL);
313 g_io_channel_unref(channel);
318 static void disconnect_callback(DBusConnection *conn, void *user_data)
320 connman_error("D-Bus disconnect");
322 g_main_loop_quit(main_loop);
325 static gchar *option_debug = NULL;
326 static gchar *option_device = NULL;
327 static gchar *option_plugin = NULL;
328 static gchar *option_nodevice = NULL;
329 static gchar *option_noplugin = NULL;
330 static gchar *option_wifi = NULL;
331 static gboolean option_detach = TRUE;
332 static gboolean option_dnsproxy = TRUE;
333 static gboolean option_version = FALSE;
335 static gboolean parse_debug(const char *key, const char *value,
336 gpointer user_data, GError **error)
339 option_debug = g_strdup(value);
341 option_debug = g_strdup("*");
346 static GOptionEntry options[] = {
347 { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
348 G_OPTION_ARG_CALLBACK, parse_debug,
349 "Specify debug options to enable", "DEBUG" },
350 { "device", 'i', 0, G_OPTION_ARG_STRING, &option_device,
351 "Specify networking device or interface", "DEV" },
352 { "nodevice", 'I', 0, G_OPTION_ARG_STRING, &option_nodevice,
353 "Specify networking interface to ignore", "DEV" },
354 { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
355 "Specify plugins to load", "NAME,..." },
356 { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
357 "Specify plugins not to load", "NAME,..." },
358 { "wifi", 'W', 0, G_OPTION_ARG_STRING, &option_wifi,
359 "Specify driver for WiFi/Supplicant", "NAME" },
360 { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
361 G_OPTION_ARG_NONE, &option_detach,
362 "Don't fork daemon to background" },
363 { "nodnsproxy", 'r', G_OPTION_FLAG_REVERSE,
364 G_OPTION_ARG_NONE, &option_dnsproxy,
365 "Don't enable DNS Proxy" },
366 { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
367 "Show version information and exit" },
371 const char *connman_option_get_string(const char *key)
373 if (g_strcmp0(key, "wifi") == 0) {
374 if (option_wifi == NULL)
375 return "nl80211,wext";
383 connman_bool_t connman_setting_get_bool(const char *key)
385 if (g_str_equal(key, "BackgroundScanning") == TRUE)
386 return connman_settings.bg_scan;
391 char **connman_setting_get_string_list(const char *key)
393 if (g_str_equal(key, "FallbackTimeservers") == TRUE)
394 return connman_settings.pref_timeservers;
396 if (g_str_equal(key, "FallbackNameservers") == TRUE)
397 return connman_settings.fallback_nameservers;
399 if (g_str_equal(key, "NetworkInterfaceBlacklist") == TRUE)
400 return connman_settings.blacklisted_interfaces;
405 unsigned int *connman_setting_get_uint_list(const char *key)
407 if (g_str_equal(key, "DefaultAutoConnectTechnologies") == TRUE)
408 return connman_settings.auto_connect;
410 if (g_str_equal(key, "PreferredTechnologies") == TRUE)
411 return connman_settings.preferred_techs;
416 unsigned int connman_timeout_input_request(void) {
417 return connman_settings.timeout_inputreq;
420 unsigned int connman_timeout_browser_launch(void) {
421 return connman_settings.timeout_browserlaunch;
424 int main(int argc, char *argv[])
426 GOptionContext *context;
427 GError *error = NULL;
428 DBusConnection *conn;
434 if (g_thread_supported() == FALSE)
438 context = g_option_context_new(NULL);
439 g_option_context_add_main_entries(context, options, NULL);
441 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
443 g_printerr("%s\n", error->message);
446 g_printerr("An unknown error occurred\n");
450 g_option_context_free(context);
452 if (option_version == TRUE) {
453 printf("%s\n", VERSION);
457 if (option_detach == TRUE) {
459 perror("Can't start daemon");
464 if (mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
465 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
467 perror("Failed to create state directory");
470 if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
471 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
473 perror("Failed to create storage directory");
478 main_loop = g_main_loop_new(NULL, FALSE);
481 if (dbus_threads_init_default() == FALSE) {
482 fprintf(stderr, "Can't init usage of threads\n");
487 signal = setup_signalfd();
489 dbus_error_init(&err);
491 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
493 if (dbus_error_is_set(&err) == TRUE) {
494 fprintf(stderr, "%s\n", err.message);
495 dbus_error_free(&err);
497 fprintf(stderr, "Can't register with system bus\n");
501 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
503 __connman_log_init(argv[0], option_debug, option_detach);
505 __connman_dbus_init(conn);
507 config = load_config(CONFIGDIR "/main.conf");
508 parse_config(config);
510 g_key_file_free(config);
512 __connman_storage_migrate();
513 __connman_technology_init();
514 __connman_notifier_init();
515 __connman_service_init();
516 __connman_provider_init();
517 __connman_network_init();
518 __connman_device_init(option_device, option_nodevice);
520 __connman_agent_init();
521 __connman_ippool_init();
522 __connman_iptables_init();
523 __connman_nat_init();
524 __connman_tethering_init();
525 __connman_counter_init();
526 __connman_manager_init();
527 __connman_config_init();
528 __connman_stats_init();
529 __connman_clock_init();
531 __connman_resolver_init(option_dnsproxy);
532 __connman_ipconfig_init();
533 __connman_rtnl_init();
534 __connman_task_init();
535 __connman_proxy_init();
536 __connman_detect_init();
537 __connman_session_init();
538 __connman_timeserver_init();
539 __connman_connection_init();
541 __connman_plugin_init(option_plugin, option_noplugin);
543 __connman_rtnl_start();
544 __connman_dhcp_init();
545 __connman_dhcpv6_init();
546 __connman_wpad_init();
547 __connman_wispr_init();
548 __connman_rfkill_init();
550 g_free(option_device);
551 g_free(option_plugin);
552 g_free(option_nodevice);
553 g_free(option_noplugin);
555 g_main_loop_run(main_loop);
557 g_source_remove(signal);
559 __connman_rfkill_cleanup();
560 __connman_wispr_cleanup();
561 __connman_wpad_cleanup();
562 __connman_dhcpv6_cleanup();
563 __connman_dhcp_cleanup();
564 __connman_provider_cleanup();
565 __connman_plugin_cleanup();
566 __connman_connection_cleanup();
567 __connman_timeserver_cleanup();
568 __connman_session_cleanup();
569 __connman_detect_cleanup();
570 __connman_proxy_cleanup();
571 __connman_task_cleanup();
572 __connman_rtnl_cleanup();
573 __connman_resolver_cleanup();
575 __connman_clock_cleanup();
576 __connman_stats_cleanup();
577 __connman_config_cleanup();
578 __connman_manager_cleanup();
579 __connman_counter_cleanup();
580 __connman_agent_cleanup();
581 __connman_tethering_cleanup();
582 __connman_nat_cleanup();
583 __connman_iptables_cleanup();
584 __connman_ippool_cleanup();
585 __connman_device_cleanup();
586 __connman_network_cleanup();
587 __connman_service_cleanup();
588 __connman_ipconfig_cleanup();
589 __connman_notifier_cleanup();
590 __connman_technology_cleanup();
592 __connman_dbus_cleanup();
594 __connman_log_cleanup();
596 dbus_connection_unref(conn);
598 g_main_loop_unref(main_loop);
600 if (connman_settings.pref_timeservers != NULL)
601 g_strfreev(connman_settings.pref_timeservers);
603 g_free(connman_settings.auto_connect);
604 g_free(connman_settings.preferred_techs);
605 g_strfreev(connman_settings.fallback_nameservers);
606 g_strfreev(connman_settings.blacklisted_interfaces);
608 g_free(option_debug);