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
46 connman_bool_t bg_scan;
47 char **pref_timeservers;
48 unsigned int *auto_connect;
49 unsigned int *preferred_techs;
50 char **fallback_nameservers;
51 unsigned int timeout_inputreq;
52 unsigned int timeout_browserlaunch;
53 } connman_settings = {
55 .pref_timeservers = NULL,
57 .preferred_techs = NULL,
58 .fallback_nameservers = NULL,
59 .timeout_inputreq = DEFAULT_INPUT_REQUEST_TIMEOUT,
60 .timeout_browserlaunch = DEFAULT_BROWSER_LAUNCH_TIMEOUT,
63 static GKeyFile *load_config(const char *file)
68 keyfile = g_key_file_new();
70 g_key_file_set_list_separator(keyfile, ',');
72 if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
73 if (err->code != G_FILE_ERROR_NOENT) {
74 connman_error("Parsing %s failed: %s", file,
79 g_key_file_free(keyfile);
86 static uint *parse_service_types(char **str_list, gsize len)
88 unsigned int *type_list;
90 enum connman_service_type type;
92 type_list = g_try_new0(unsigned int, len + 1);
93 if (type_list == NULL)
98 while (str_list[i] != NULL)
100 type = __connman_service_string2type(str_list[i]);
102 if (type != CONNMAN_SERVICE_TYPE_UNKNOWN) {
112 static char **parse_fallback_nameservers(char **nameservers, gsize len)
117 servers = g_try_new0(char *, len + 1);
123 while (nameservers[i] != NULL) {
124 if (connman_inet_check_ipaddress(nameservers[i]) > 0) {
125 servers[j] = g_strdup(nameservers[i]);
134 static void parse_config(GKeyFile *config)
136 GError *error = NULL;
141 char *default_auto_connect[] = {
149 if (config == NULL) {
150 connman_settings.auto_connect =
151 parse_service_types(default_auto_connect, 3);
155 DBG("parsing main.conf");
157 boolean = g_key_file_get_boolean(config, "General",
158 "BackgroundScanning", &error);
160 connman_settings.bg_scan = boolean;
162 g_clear_error(&error);
164 timeservers = g_key_file_get_string_list(config, "General",
165 "FallbackTimeservers", NULL, &error);
167 connman_settings.pref_timeservers = timeservers;
169 g_clear_error(&error);
171 str_list = g_key_file_get_string_list(config, "General",
172 "DefaultAutoConnectTechnologies", &len, &error);
175 connman_settings.auto_connect =
176 parse_service_types(str_list, len);
178 connman_settings.auto_connect =
179 parse_service_types(default_auto_connect, 3);
181 g_strfreev(str_list);
183 g_clear_error(&error);
185 str_list = g_key_file_get_string_list(config, "General",
186 "PreferredTechnologies", &len, &error);
189 connman_settings.preferred_techs =
190 parse_service_types(str_list, len);
192 g_strfreev(str_list);
194 g_clear_error(&error);
196 str_list = g_key_file_get_string_list(config, "General",
197 "FallbackNameservers", &len, &error);
200 connman_settings.fallback_nameservers =
201 parse_fallback_nameservers(str_list, len);
203 g_strfreev(str_list);
205 g_clear_error(&error);
207 timeout = g_key_file_get_integer(config, "General",
208 "InputRequestTimeout", &error);
209 if (error == NULL && timeout >= 0)
210 connman_settings.timeout_inputreq = timeout * 1000;
212 g_clear_error(&error);
214 timeout = g_key_file_get_integer(config, "General",
215 "BrowserLaunchTimeout", &error);
216 if (error == NULL && timeout >= 0)
217 connman_settings.timeout_browserlaunch = timeout * 1000;
219 g_clear_error(&error);
222 static GMainLoop *main_loop = NULL;
224 static unsigned int __terminated = 0;
226 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
229 struct signalfd_siginfo si;
233 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
236 fd = g_io_channel_unix_get_fd(channel);
238 result = read(fd, &si, sizeof(si));
239 if (result != sizeof(si))
242 switch (si.ssi_signo) {
245 if (__terminated == 0) {
246 connman_info("Terminating");
247 g_main_loop_quit(main_loop);
257 static guint setup_signalfd(void)
265 sigaddset(&mask, SIGINT);
266 sigaddset(&mask, SIGTERM);
268 if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
269 perror("Failed to set signal mask");
273 fd = signalfd(-1, &mask, 0);
275 perror("Failed to create signal descriptor");
279 channel = g_io_channel_unix_new(fd);
281 g_io_channel_set_close_on_unref(channel, TRUE);
282 g_io_channel_set_encoding(channel, NULL, NULL);
283 g_io_channel_set_buffered(channel, FALSE);
285 source = g_io_add_watch(channel,
286 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
287 signal_handler, NULL);
289 g_io_channel_unref(channel);
294 static void disconnect_callback(DBusConnection *conn, void *user_data)
296 connman_error("D-Bus disconnect");
298 g_main_loop_quit(main_loop);
301 static gchar *option_debug = NULL;
302 static gchar *option_device = NULL;
303 static gchar *option_plugin = NULL;
304 static gchar *option_nodevice = NULL;
305 static gchar *option_noplugin = NULL;
306 static gchar *option_wifi = NULL;
307 static gboolean option_detach = TRUE;
308 static gboolean option_dnsproxy = TRUE;
309 static gboolean option_compat = FALSE;
310 static gboolean option_version = FALSE;
312 static gboolean parse_debug(const char *key, const char *value,
313 gpointer user_data, GError **error)
316 option_debug = g_strdup(value);
318 option_debug = g_strdup("*");
323 static GOptionEntry options[] = {
324 { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
325 G_OPTION_ARG_CALLBACK, parse_debug,
326 "Specify debug options to enable", "DEBUG" },
327 { "device", 'i', 0, G_OPTION_ARG_STRING, &option_device,
328 "Specify networking device or interface", "DEV" },
329 { "nodevice", 'I', 0, G_OPTION_ARG_STRING, &option_nodevice,
330 "Specify networking interface to ignore", "DEV" },
331 { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
332 "Specify plugins to load", "NAME,..." },
333 { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
334 "Specify plugins not to load", "NAME,..." },
335 { "wifi", 'W', 0, G_OPTION_ARG_STRING, &option_wifi,
336 "Specify driver for WiFi/Supplicant", "NAME" },
337 { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
338 G_OPTION_ARG_NONE, &option_detach,
339 "Don't fork daemon to background" },
340 { "nodnsproxy", 'r', G_OPTION_FLAG_REVERSE,
341 G_OPTION_ARG_NONE, &option_dnsproxy,
342 "Don't enable DNS Proxy" },
343 { "compat", 'c', 0, G_OPTION_ARG_NONE, &option_compat,
345 { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
346 "Show version information and exit" },
350 const char *connman_option_get_string(const char *key)
352 if (g_strcmp0(key, "wifi") == 0) {
353 if (option_wifi == NULL)
354 return "nl80211,wext";
362 connman_bool_t connman_setting_get_bool(const char *key)
364 if (g_str_equal(key, "BackgroundScanning") == TRUE)
365 return connman_settings.bg_scan;
370 char **connman_setting_get_string_list(const char *key)
372 if (g_str_equal(key, "FallbackTimeservers") == TRUE)
373 return connman_settings.pref_timeservers;
375 if (g_str_equal(key, "FallbackNameservers") == TRUE)
376 return connman_settings.fallback_nameservers;
381 unsigned int *connman_setting_get_uint_list(const char *key)
383 if (g_str_equal(key, "DefaultAutoConnectTechnologies") == TRUE)
384 return connman_settings.auto_connect;
386 if (g_str_equal(key, "PreferredTechnologies") == TRUE)
387 return connman_settings.preferred_techs;
392 unsigned int connman_timeout_input_request(void) {
393 return connman_settings.timeout_inputreq;
396 unsigned int connman_timeout_browser_launch(void) {
397 return connman_settings.timeout_browserlaunch;
400 int main(int argc, char *argv[])
402 GOptionContext *context;
403 GError *error = NULL;
404 DBusConnection *conn;
410 if (g_thread_supported() == FALSE)
414 context = g_option_context_new(NULL);
415 g_option_context_add_main_entries(context, options, NULL);
417 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
419 g_printerr("%s\n", error->message);
422 g_printerr("An unknown error occurred\n");
426 g_option_context_free(context);
428 if (option_version == TRUE) {
429 printf("%s\n", VERSION);
433 if (option_detach == TRUE) {
435 perror("Can't start daemon");
440 if (mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
441 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
443 perror("Failed to create state directory");
446 if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
447 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
449 perror("Failed to create storage directory");
454 main_loop = g_main_loop_new(NULL, FALSE);
457 if (dbus_threads_init_default() == FALSE) {
458 fprintf(stderr, "Can't init usage of threads\n");
463 signal = setup_signalfd();
465 dbus_error_init(&err);
467 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
469 if (dbus_error_is_set(&err) == TRUE) {
470 fprintf(stderr, "%s\n", err.message);
471 dbus_error_free(&err);
473 fprintf(stderr, "Can't register with system bus\n");
477 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
479 __connman_log_init(argv[0], option_debug, option_detach);
481 __connman_dbus_init(conn);
483 config = load_config(CONFIGDIR "/main.conf");
484 parse_config(config);
486 g_key_file_free(config);
488 __connman_storage_migrate();
489 __connman_technology_init();
490 __connman_notifier_init();
491 __connman_service_init();
492 __connman_provider_init();
493 __connman_network_init();
494 __connman_device_init(option_device, option_nodevice);
496 __connman_agent_init();
497 __connman_ippool_init();
498 __connman_iptables_init();
499 __connman_nat_init();
500 __connman_tethering_init();
501 __connman_counter_init();
502 __connman_manager_init();
503 __connman_config_init();
504 __connman_stats_init();
505 __connman_clock_init();
507 __connman_resolver_init(option_dnsproxy);
508 __connman_ipconfig_init();
509 __connman_rtnl_init();
510 __connman_task_init();
511 __connman_proxy_init();
512 __connman_detect_init();
513 __connman_session_init();
514 __connman_timeserver_init();
515 __connman_connection_init();
517 __connman_plugin_init(option_plugin, option_noplugin);
519 __connman_rtnl_start();
520 __connman_dhcp_init();
521 __connman_dhcpv6_init();
522 __connman_wpad_init();
523 __connman_wispr_init();
524 __connman_rfkill_init();
526 g_free(option_device);
527 g_free(option_plugin);
528 g_free(option_nodevice);
529 g_free(option_noplugin);
531 g_main_loop_run(main_loop);
533 g_source_remove(signal);
535 __connman_rfkill_cleanup();
536 __connman_wispr_cleanup();
537 __connman_wpad_cleanup();
538 __connman_dhcpv6_cleanup();
539 __connman_dhcp_cleanup();
540 __connman_provider_cleanup();
541 __connman_plugin_cleanup();
542 __connman_connection_cleanup();
543 __connman_timeserver_cleanup();
544 __connman_session_cleanup();
545 __connman_detect_cleanup();
546 __connman_proxy_cleanup();
547 __connman_task_cleanup();
548 __connman_rtnl_cleanup();
549 __connman_resolver_cleanup();
551 __connman_clock_cleanup();
552 __connman_stats_cleanup();
553 __connman_config_cleanup();
554 __connman_manager_cleanup();
555 __connman_counter_cleanup();
556 __connman_agent_cleanup();
557 __connman_tethering_cleanup();
558 __connman_nat_cleanup();
559 __connman_iptables_cleanup();
560 __connman_ippool_cleanup();
561 __connman_device_cleanup();
562 __connman_network_cleanup();
563 __connman_service_cleanup();
564 __connman_ipconfig_cleanup();
565 __connman_notifier_cleanup();
566 __connman_technology_cleanup();
568 __connman_dbus_cleanup();
570 __connman_log_cleanup();
572 dbus_connection_unref(conn);
574 g_main_loop_unref(main_loop);
576 if (connman_settings.pref_timeservers != NULL)
577 g_strfreev(connman_settings.pref_timeservers);
579 g_free(connman_settings.auto_connect);
580 g_free(connman_settings.preferred_techs);
581 g_strfreev(connman_settings.fallback_nameservers);
583 g_free(option_debug);