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>
47 connman_bool_t bg_scan;
48 char **pref_timeservers;
49 unsigned int *auto_connect;
50 unsigned int *preferred_techs;
51 char **fallback_nameservers;
52 } connman_settings = {
54 .pref_timeservers = NULL,
56 .preferred_techs = NULL,
57 .fallback_nameservers = NULL,
60 static GKeyFile *load_config(const char *file)
65 keyfile = g_key_file_new();
67 g_key_file_set_list_separator(keyfile, ',');
69 if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
70 if (err->code != G_FILE_ERROR_NOENT) {
71 connman_error("Parsing %s failed: %s", file,
76 g_key_file_free(keyfile);
83 static uint *parse_service_types(char **str_list, gsize len)
85 unsigned int *type_list;
87 enum connman_service_type type;
89 type_list = g_try_new0(unsigned int, len + 1);
90 if (type_list == NULL)
95 while (str_list[i] != NULL)
97 type = __connman_service_string2type(str_list[i]);
99 if (type != CONNMAN_SERVICE_TYPE_UNKNOWN) {
109 static char **parse_fallback_nameservers(char **nameservers, gsize len)
113 struct addrinfo hints;
114 struct addrinfo *addr;
116 servers = g_try_new0(char *, len + 1);
122 while (nameservers[i] != NULL) {
123 memset(&hints, 0, sizeof(struct addrinfo));
124 hints.ai_flags = AI_NUMERICHOST;
126 if (getaddrinfo(nameservers[i], NULL, &hints, &addr) == 0) {
127 servers[j] = g_strdup(nameservers[i]);
138 static void parse_config(GKeyFile *config)
140 GError *error = NULL;
145 char *default_auto_connect[] = {
152 if (config == NULL) {
153 connman_settings.auto_connect =
154 parse_service_types(default_auto_connect, 3);
158 DBG("parsing main.conf");
160 boolean = g_key_file_get_boolean(config, "General",
161 "BackgroundScanning", &error);
163 connman_settings.bg_scan = boolean;
165 g_clear_error(&error);
167 timeservers = g_key_file_get_string_list(config, "General",
168 "FallbackTimeservers", NULL, &error);
170 connman_settings.pref_timeservers = timeservers;
172 g_clear_error(&error);
174 str_list = g_key_file_get_string_list(config, "General",
175 "DefaultAutoConnectTechnologies", &len, &error);
178 connman_settings.auto_connect =
179 parse_service_types(str_list, len);
181 connman_settings.auto_connect =
182 parse_service_types(default_auto_connect, 3);
184 g_strfreev(str_list);
186 g_clear_error(&error);
188 str_list = g_key_file_get_string_list(config, "General",
189 "PreferredTechnologies", &len, &error);
192 connman_settings.preferred_techs =
193 parse_service_types(str_list, len);
195 g_strfreev(str_list);
197 g_clear_error(&error);
199 str_list = g_key_file_get_string_list(config, "General",
200 "FallbackNameservers", &len, &error);
203 connman_settings.fallback_nameservers =
204 parse_fallback_nameservers(str_list, len);
206 g_strfreev(str_list);
208 g_clear_error(&error);
211 static GMainLoop *main_loop = NULL;
213 static unsigned int __terminated = 0;
215 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
218 struct signalfd_siginfo si;
222 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
225 fd = g_io_channel_unix_get_fd(channel);
227 result = read(fd, &si, sizeof(si));
228 if (result != sizeof(si))
231 switch (si.ssi_signo) {
234 if (__terminated == 0) {
235 connman_info("Terminating");
236 g_main_loop_quit(main_loop);
246 static guint setup_signalfd(void)
254 sigaddset(&mask, SIGINT);
255 sigaddset(&mask, SIGTERM);
257 if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
258 perror("Failed to set signal mask");
262 fd = signalfd(-1, &mask, 0);
264 perror("Failed to create signal descriptor");
268 channel = g_io_channel_unix_new(fd);
270 g_io_channel_set_close_on_unref(channel, TRUE);
271 g_io_channel_set_encoding(channel, NULL, NULL);
272 g_io_channel_set_buffered(channel, FALSE);
274 source = g_io_add_watch(channel,
275 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
276 signal_handler, NULL);
278 g_io_channel_unref(channel);
283 static void disconnect_callback(DBusConnection *conn, void *user_data)
285 connman_error("D-Bus disconnect");
287 g_main_loop_quit(main_loop);
290 static gchar *option_debug = NULL;
291 static gchar *option_device = NULL;
292 static gchar *option_plugin = NULL;
293 static gchar *option_nodevice = NULL;
294 static gchar *option_noplugin = NULL;
295 static gchar *option_wifi = NULL;
296 static gboolean option_detach = TRUE;
297 static gboolean option_dnsproxy = TRUE;
298 static gboolean option_compat = FALSE;
299 static gboolean option_version = FALSE;
301 static gboolean parse_debug(const char *key, const char *value,
302 gpointer user_data, GError **error)
305 option_debug = g_strdup(value);
307 option_debug = g_strdup("*");
312 static GOptionEntry options[] = {
313 { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
314 G_OPTION_ARG_CALLBACK, parse_debug,
315 "Specify debug options to enable", "DEBUG" },
316 { "device", 'i', 0, G_OPTION_ARG_STRING, &option_device,
317 "Specify networking device or interface", "DEV" },
318 { "nodevice", 'I', 0, G_OPTION_ARG_STRING, &option_nodevice,
319 "Specify networking interface to ignore", "DEV" },
320 { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
321 "Specify plugins to load", "NAME,..." },
322 { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
323 "Specify plugins not to load", "NAME,..." },
324 { "wifi", 'W', 0, G_OPTION_ARG_STRING, &option_wifi,
325 "Specify driver for WiFi/Supplicant", "NAME" },
326 { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
327 G_OPTION_ARG_NONE, &option_detach,
328 "Don't fork daemon to background" },
329 { "nodnsproxy", 'r', G_OPTION_FLAG_REVERSE,
330 G_OPTION_ARG_NONE, &option_dnsproxy,
331 "Don't enable DNS Proxy" },
332 { "compat", 'c', 0, G_OPTION_ARG_NONE, &option_compat,
334 { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
335 "Show version information and exit" },
339 const char *connman_option_get_string(const char *key)
341 if (g_strcmp0(key, "wifi") == 0) {
342 if (option_wifi == NULL)
343 return "nl80211,wext";
351 connman_bool_t connman_setting_get_bool(const char *key)
353 if (g_str_equal(key, "BackgroundScanning") == TRUE)
354 return connman_settings.bg_scan;
359 char **connman_setting_get_string_list(const char *key)
361 if (g_str_equal(key, "FallbackTimeservers") == TRUE)
362 return connman_settings.pref_timeservers;
364 if (g_str_equal(key, "FallbackNameservers") == TRUE)
365 return connman_settings.fallback_nameservers;
370 unsigned int *connman_setting_get_uint_list(const char *key)
372 if (g_str_equal(key, "DefaultAutoConnectTechnologies") == TRUE)
373 return connman_settings.auto_connect;
375 if (g_str_equal(key, "PreferredTechnologies") == TRUE)
376 return connman_settings.preferred_techs;
381 int main(int argc, char *argv[])
383 GOptionContext *context;
384 GError *error = NULL;
385 DBusConnection *conn;
391 /* Drop capabilities */
395 if (g_thread_supported() == FALSE)
399 context = g_option_context_new(NULL);
400 g_option_context_add_main_entries(context, options, NULL);
402 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
404 g_printerr("%s\n", error->message);
407 g_printerr("An unknown error occurred\n");
411 g_option_context_free(context);
413 if (option_version == TRUE) {
414 printf("%s\n", VERSION);
418 if (option_detach == TRUE) {
420 perror("Can't start daemon");
425 if (mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
426 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
428 perror("Failed to create state directory");
431 if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
432 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
434 perror("Failed to create storage directory");
439 main_loop = g_main_loop_new(NULL, FALSE);
442 if (dbus_threads_init_default() == FALSE) {
443 fprintf(stderr, "Can't init usage of threads\n");
448 signal = setup_signalfd();
450 dbus_error_init(&err);
452 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
454 if (dbus_error_is_set(&err) == TRUE) {
455 fprintf(stderr, "%s\n", err.message);
456 dbus_error_free(&err);
458 fprintf(stderr, "Can't register with system bus\n");
462 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
464 __connman_log_init(argv[0], option_debug, option_detach);
466 __connman_dbus_init(conn);
468 config = load_config(CONFIGDIR "/main.conf");
469 parse_config(config);
471 g_key_file_free(config);
473 __connman_storage_migrate();
474 __connman_technology_init();
475 __connman_notifier_init();
476 __connman_service_init();
477 __connman_provider_init();
478 __connman_network_init();
479 __connman_device_init(option_device, option_nodevice);
481 __connman_agent_init();
482 __connman_ippool_init();
483 __connman_iptables_init();
484 __connman_nat_init();
485 __connman_tethering_init();
486 __connman_counter_init();
487 __connman_manager_init();
488 __connman_config_init();
489 __connman_stats_init();
490 __connman_clock_init();
492 __connman_resolver_init(option_dnsproxy);
493 __connman_ipconfig_init();
494 __connman_rtnl_init();
495 __connman_task_init();
496 __connman_proxy_init();
497 __connman_detect_init();
498 __connman_session_init();
499 __connman_timeserver_init();
500 __connman_connection_init();
502 __connman_plugin_init(option_plugin, option_noplugin);
504 __connman_rtnl_start();
505 __connman_dhcp_init();
506 __connman_dhcpv6_init();
507 __connman_wpad_init();
508 __connman_wispr_init();
509 __connman_rfkill_init();
511 g_free(option_device);
512 g_free(option_plugin);
513 g_free(option_nodevice);
514 g_free(option_noplugin);
516 g_main_loop_run(main_loop);
518 g_source_remove(signal);
520 __connman_rfkill_cleanup();
521 __connman_wispr_cleanup();
522 __connman_wpad_cleanup();
523 __connman_dhcpv6_cleanup();
524 __connman_dhcp_cleanup();
525 __connman_provider_cleanup();
526 __connman_plugin_cleanup();
527 __connman_connection_cleanup();
528 __connman_timeserver_cleanup();
529 __connman_session_cleanup();
530 __connman_detect_cleanup();
531 __connman_proxy_cleanup();
532 __connman_task_cleanup();
533 __connman_rtnl_cleanup();
534 __connman_resolver_cleanup();
536 __connman_clock_cleanup();
537 __connman_stats_cleanup();
538 __connman_config_cleanup();
539 __connman_manager_cleanup();
540 __connman_counter_cleanup();
541 __connman_agent_cleanup();
542 __connman_tethering_cleanup();
543 __connman_nat_cleanup();
544 __connman_iptables_cleanup();
545 __connman_ippool_cleanup();
546 __connman_device_cleanup();
547 __connman_network_cleanup();
548 __connman_service_cleanup();
549 __connman_ipconfig_cleanup();
550 __connman_notifier_cleanup();
551 __connman_technology_cleanup();
553 __connman_dbus_cleanup();
555 __connman_log_cleanup();
557 dbus_connection_unref(conn);
559 g_main_loop_unref(main_loop);
561 if (connman_settings.pref_timeservers != NULL)
562 g_strfreev(connman_settings.pref_timeservers);
564 g_free(connman_settings.auto_connect);
565 g_free(connman_settings.preferred_techs);
566 g_strfreev(connman_settings.fallback_nameservers);
568 g_free(option_debug);