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_settings = {
73 .pref_timeservers = NULL,
75 .preferred_techs = NULL,
76 .fallback_nameservers = NULL,
77 .timeout_inputreq = DEFAULT_INPUT_REQUEST_TIMEOUT,
78 .timeout_browserlaunch = DEFAULT_BROWSER_LAUNCH_TIMEOUT,
79 .blacklisted_interfaces = NULL,
80 .allow_hostname_updates = TRUE,
83 static GKeyFile *load_config(const char *file)
88 keyfile = g_key_file_new();
90 g_key_file_set_list_separator(keyfile, ',');
92 if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
93 if (err->code != G_FILE_ERROR_NOENT) {
94 connman_error("Parsing %s failed: %s", file,
99 g_key_file_free(keyfile);
106 static uint *parse_service_types(char **str_list, gsize len)
108 unsigned int *type_list;
110 enum connman_service_type type;
112 type_list = g_try_new0(unsigned int, len + 1);
113 if (type_list == NULL)
118 while (str_list[i] != NULL)
120 type = __connman_service_string2type(str_list[i]);
122 if (type != CONNMAN_SERVICE_TYPE_UNKNOWN) {
132 static char **parse_fallback_nameservers(char **nameservers, gsize len)
137 servers = g_try_new0(char *, len + 1);
143 while (nameservers[i] != NULL) {
144 if (connman_inet_check_ipaddress(nameservers[i]) > 0) {
145 servers[j] = g_strdup(nameservers[i]);
154 static void parse_config(GKeyFile *config)
156 GError *error = NULL;
164 if (config == NULL) {
165 connman_settings.auto_connect =
166 parse_service_types(default_auto_connect, 3);
167 connman_settings.blacklisted_interfaces =
168 g_strdupv(default_blacklist);
172 DBG("parsing main.conf");
174 boolean = g_key_file_get_boolean(config, "General",
175 "BackgroundScanning", &error);
177 connman_settings.bg_scan = boolean;
179 g_clear_error(&error);
181 timeservers = g_key_file_get_string_list(config, "General",
182 "FallbackTimeservers", NULL, &error);
184 connman_settings.pref_timeservers = timeservers;
186 g_clear_error(&error);
188 str_list = g_key_file_get_string_list(config, "General",
189 "DefaultAutoConnectTechnologies", &len, &error);
192 connman_settings.auto_connect =
193 parse_service_types(str_list, len);
195 connman_settings.auto_connect =
196 parse_service_types(default_auto_connect, 3);
198 g_strfreev(str_list);
200 g_clear_error(&error);
202 str_list = g_key_file_get_string_list(config, "General",
203 "PreferredTechnologies", &len, &error);
206 connman_settings.preferred_techs =
207 parse_service_types(str_list, len);
209 g_strfreev(str_list);
211 g_clear_error(&error);
213 str_list = g_key_file_get_string_list(config, "General",
214 "FallbackNameservers", &len, &error);
217 connman_settings.fallback_nameservers =
218 parse_fallback_nameservers(str_list, len);
220 g_strfreev(str_list);
222 g_clear_error(&error);
224 timeout = g_key_file_get_integer(config, "General",
225 "InputRequestTimeout", &error);
226 if (error == NULL && timeout >= 0)
227 connman_settings.timeout_inputreq = timeout * 1000;
229 g_clear_error(&error);
231 timeout = g_key_file_get_integer(config, "General",
232 "BrowserLaunchTimeout", &error);
233 if (error == NULL && timeout >= 0)
234 connman_settings.timeout_browserlaunch = timeout * 1000;
236 g_clear_error(&error);
238 interfaces = g_key_file_get_string_list(config, "General",
239 "NetworkInterfaceBlacklist", &len, &error);
242 connman_settings.blacklisted_interfaces = interfaces;
244 connman_settings.blacklisted_interfaces =
245 g_strdupv(default_blacklist);
247 g_clear_error(&error);
249 boolean = g_key_file_get_boolean(config, "General",
250 "AllowHostnameUpdates",
253 connman_settings.allow_hostname_updates = boolean;
255 g_clear_error(&error);
258 static int config_init(const char *file)
262 config = load_config(file);
263 parse_config(config);
265 g_key_file_free(config);
270 static GMainLoop *main_loop = NULL;
272 static unsigned int __terminated = 0;
274 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
277 struct signalfd_siginfo si;
281 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
284 fd = g_io_channel_unix_get_fd(channel);
286 result = read(fd, &si, sizeof(si));
287 if (result != sizeof(si))
290 switch (si.ssi_signo) {
293 if (__terminated == 0) {
294 connman_info("Terminating");
295 g_main_loop_quit(main_loop);
305 static guint setup_signalfd(void)
313 sigaddset(&mask, SIGINT);
314 sigaddset(&mask, SIGTERM);
316 if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
317 perror("Failed to set signal mask");
321 fd = signalfd(-1, &mask, 0);
323 perror("Failed to create signal descriptor");
327 channel = g_io_channel_unix_new(fd);
329 g_io_channel_set_close_on_unref(channel, TRUE);
330 g_io_channel_set_encoding(channel, NULL, NULL);
331 g_io_channel_set_buffered(channel, FALSE);
333 source = g_io_add_watch(channel,
334 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
335 signal_handler, NULL);
337 g_io_channel_unref(channel);
342 static void disconnect_callback(DBusConnection *conn, void *user_data)
344 connman_error("D-Bus disconnect");
346 g_main_loop_quit(main_loop);
349 static gchar *option_config = NULL;
350 static gchar *option_debug = NULL;
351 static gchar *option_device = NULL;
352 static gchar *option_plugin = NULL;
353 static gchar *option_nodevice = NULL;
354 static gchar *option_noplugin = NULL;
355 static gchar *option_wifi = NULL;
356 static gboolean option_detach = TRUE;
357 static gboolean option_dnsproxy = TRUE;
358 static gboolean option_version = FALSE;
360 static gboolean parse_debug(const char *key, const char *value,
361 gpointer user_data, GError **error)
364 option_debug = g_strdup(value);
366 option_debug = g_strdup("*");
371 static GOptionEntry options[] = {
372 { "config", 'c', 0, G_OPTION_ARG_STRING, &option_config,
373 "Load the specified configuration file "
374 "instead of " CONFIGMAINFILE, "FILE" },
375 { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
376 G_OPTION_ARG_CALLBACK, parse_debug,
377 "Specify debug options to enable", "DEBUG" },
378 { "device", 'i', 0, G_OPTION_ARG_STRING, &option_device,
379 "Specify networking device or interface", "DEV" },
380 { "nodevice", 'I', 0, G_OPTION_ARG_STRING, &option_nodevice,
381 "Specify networking interface to ignore", "DEV" },
382 { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
383 "Specify plugins to load", "NAME,..." },
384 { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
385 "Specify plugins not to load", "NAME,..." },
386 { "wifi", 'W', 0, G_OPTION_ARG_STRING, &option_wifi,
387 "Specify driver for WiFi/Supplicant", "NAME" },
388 { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
389 G_OPTION_ARG_NONE, &option_detach,
390 "Don't fork daemon to background" },
391 { "nodnsproxy", 'r', G_OPTION_FLAG_REVERSE,
392 G_OPTION_ARG_NONE, &option_dnsproxy,
393 "Don't enable DNS Proxy" },
394 { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
395 "Show version information and exit" },
399 const char *connman_option_get_string(const char *key)
401 if (g_strcmp0(key, "wifi") == 0) {
402 if (option_wifi == NULL)
403 return "nl80211,wext";
411 connman_bool_t connman_setting_get_bool(const char *key)
413 if (g_str_equal(key, "BackgroundScanning") == TRUE)
414 return connman_settings.bg_scan;
416 if (g_str_equal(key, "AllowHostnameUpdates") == TRUE)
417 return connman_settings.allow_hostname_updates;
422 char **connman_setting_get_string_list(const char *key)
424 if (g_str_equal(key, "FallbackTimeservers") == TRUE)
425 return connman_settings.pref_timeservers;
427 if (g_str_equal(key, "FallbackNameservers") == TRUE)
428 return connman_settings.fallback_nameservers;
430 if (g_str_equal(key, "NetworkInterfaceBlacklist") == TRUE)
431 return connman_settings.blacklisted_interfaces;
436 unsigned int *connman_setting_get_uint_list(const char *key)
438 if (g_str_equal(key, "DefaultAutoConnectTechnologies") == TRUE)
439 return connman_settings.auto_connect;
441 if (g_str_equal(key, "PreferredTechnologies") == TRUE)
442 return connman_settings.preferred_techs;
447 unsigned int connman_timeout_input_request(void) {
448 return connman_settings.timeout_inputreq;
451 unsigned int connman_timeout_browser_launch(void) {
452 return connman_settings.timeout_browserlaunch;
455 int main(int argc, char *argv[])
457 GOptionContext *context;
458 GError *error = NULL;
459 DBusConnection *conn;
464 if (g_thread_supported() == FALSE)
468 context = g_option_context_new(NULL);
469 g_option_context_add_main_entries(context, options, NULL);
471 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
473 g_printerr("%s\n", error->message);
476 g_printerr("An unknown error occurred\n");
480 g_option_context_free(context);
482 if (option_version == TRUE) {
483 printf("%s\n", VERSION);
487 if (option_detach == TRUE) {
489 perror("Can't start daemon");
494 if (mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
495 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
497 perror("Failed to create state directory");
500 if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
501 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
503 perror("Failed to create storage directory");
508 main_loop = g_main_loop_new(NULL, FALSE);
511 if (dbus_threads_init_default() == FALSE) {
512 fprintf(stderr, "Can't init usage of threads\n");
517 signal = setup_signalfd();
519 dbus_error_init(&err);
521 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
523 if (dbus_error_is_set(&err) == TRUE) {
524 fprintf(stderr, "%s\n", err.message);
525 dbus_error_free(&err);
527 fprintf(stderr, "Can't register with system bus\n");
531 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
533 __connman_log_init(argv[0], option_debug, option_detach);
535 __connman_dbus_init(conn);
537 if (option_config == NULL)
538 config_init(CONFIGMAINFILE);
540 config_init(option_config);
542 __connman_storage_migrate();
543 __connman_technology_init();
544 __connman_notifier_init();
545 __connman_service_init();
546 __connman_provider_init();
547 __connman_network_init();
548 __connman_device_init(option_device, option_nodevice);
550 __connman_agent_init();
551 __connman_ippool_init();
552 __connman_iptables_init();
553 __connman_nat_init();
554 __connman_tethering_init();
555 __connman_counter_init();
556 __connman_manager_init();
557 __connman_config_init();
558 __connman_stats_init();
559 __connman_clock_init();
561 __connman_resolver_init(option_dnsproxy);
562 __connman_ipconfig_init();
563 __connman_rtnl_init();
564 __connman_task_init();
565 __connman_proxy_init();
566 __connman_detect_init();
567 __connman_session_init();
568 __connman_timeserver_init();
569 __connman_connection_init();
571 __connman_plugin_init(option_plugin, option_noplugin);
573 __connman_rtnl_start();
574 __connman_dhcp_init();
575 __connman_dhcpv6_init();
576 __connman_wpad_init();
577 __connman_wispr_init();
578 __connman_rfkill_init();
580 g_free(option_config);
581 g_free(option_device);
582 g_free(option_plugin);
583 g_free(option_nodevice);
584 g_free(option_noplugin);
586 g_main_loop_run(main_loop);
588 g_source_remove(signal);
590 __connman_rfkill_cleanup();
591 __connman_wispr_cleanup();
592 __connman_wpad_cleanup();
593 __connman_dhcpv6_cleanup();
594 __connman_dhcp_cleanup();
595 __connman_provider_cleanup();
596 __connman_plugin_cleanup();
597 __connman_connection_cleanup();
598 __connman_timeserver_cleanup();
599 __connman_session_cleanup();
600 __connman_detect_cleanup();
601 __connman_proxy_cleanup();
602 __connman_task_cleanup();
603 __connman_rtnl_cleanup();
604 __connman_resolver_cleanup();
606 __connman_clock_cleanup();
607 __connman_stats_cleanup();
608 __connman_config_cleanup();
609 __connman_manager_cleanup();
610 __connman_counter_cleanup();
611 __connman_agent_cleanup();
612 __connman_tethering_cleanup();
613 __connman_nat_cleanup();
614 __connman_iptables_cleanup();
615 __connman_ippool_cleanup();
616 __connman_device_cleanup();
617 __connman_network_cleanup();
618 __connman_service_cleanup();
619 __connman_ipconfig_cleanup();
620 __connman_notifier_cleanup();
621 __connman_technology_cleanup();
623 __connman_dbus_cleanup();
625 __connman_log_cleanup();
627 dbus_connection_unref(conn);
629 g_main_loop_unref(main_loop);
631 if (connman_settings.pref_timeservers != NULL)
632 g_strfreev(connman_settings.pref_timeservers);
634 g_free(connman_settings.auto_connect);
635 g_free(connman_settings.preferred_techs);
636 g_strfreev(connman_settings.fallback_nameservers);
637 g_strfreev(connman_settings.blacklisted_interfaces);
639 g_free(option_debug);