5 * Copyright (C) 2007-2010 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>
46 connman_bool_t bg_scan;
47 char **pref_timeservers;
48 unsigned int *auto_connect;
49 unsigned int *preferred_techs;
50 } connman_settings = {
52 .pref_timeservers = NULL,
54 .preferred_techs = NULL,
57 static GKeyFile *load_config(const char *file)
62 keyfile = g_key_file_new();
64 g_key_file_set_list_separator(keyfile, ',');
66 if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
67 if (err->code != G_FILE_ERROR_NOENT) {
68 connman_error("Parsing %s failed: %s", file,
73 g_key_file_free(keyfile);
80 static uint *parse_service_types(char **str_list, gsize len)
82 unsigned int *type_list;
84 enum connman_service_type type;
86 type_list = g_try_new0(unsigned int, len + 1);
87 if (type_list == NULL)
92 while (str_list[i] != NULL)
94 type = __connman_service_string2type(str_list[i]);
96 if (type != CONNMAN_SERVICE_TYPE_UNKNOWN) {
106 static void parse_config(GKeyFile *config)
108 GError *error = NULL;
113 char *default_auto_connect[] = {
122 DBG("parsing main.conf");
124 boolean = g_key_file_get_boolean(config, "General",
125 "BackgroundScanning", &error);
127 connman_settings.bg_scan = boolean;
129 g_clear_error(&error);
131 timeservers = g_key_file_get_string_list(config, "General",
132 "FallbackTimeservers", NULL, &error);
134 connman_settings.pref_timeservers = timeservers;
136 g_clear_error(&error);
138 str_list = g_key_file_get_string_list(config, "General",
139 "DefaultAutoConnectTechnologies", &len, &error);
142 connman_settings.auto_connect =
143 parse_service_types(str_list, len);
145 connman_settings.auto_connect =
146 parse_service_types(default_auto_connect, 3);
148 g_strfreev(str_list);
150 g_clear_error(&error);
152 str_list = g_key_file_get_string_list(config, "General",
153 "PreferredTechnologies", &len, &error);
156 connman_settings.preferred_techs =
157 parse_service_types(str_list, len);
159 g_strfreev(str_list);
161 g_clear_error(&error);
164 static GMainLoop *main_loop = NULL;
166 static unsigned int __terminated = 0;
168 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
171 struct signalfd_siginfo si;
175 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
178 fd = g_io_channel_unix_get_fd(channel);
180 result = read(fd, &si, sizeof(si));
181 if (result != sizeof(si))
184 switch (si.ssi_signo) {
187 if (__terminated == 0) {
188 connman_info("Terminating");
189 g_main_loop_quit(main_loop);
199 static guint setup_signalfd(void)
207 sigaddset(&mask, SIGINT);
208 sigaddset(&mask, SIGTERM);
210 if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
211 perror("Failed to set signal mask");
215 fd = signalfd(-1, &mask, 0);
217 perror("Failed to create signal descriptor");
221 channel = g_io_channel_unix_new(fd);
223 g_io_channel_set_close_on_unref(channel, TRUE);
224 g_io_channel_set_encoding(channel, NULL, NULL);
225 g_io_channel_set_buffered(channel, FALSE);
227 source = g_io_add_watch(channel,
228 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
229 signal_handler, NULL);
231 g_io_channel_unref(channel);
236 static void disconnect_callback(DBusConnection *conn, void *user_data)
238 connman_error("D-Bus disconnect");
240 g_main_loop_quit(main_loop);
243 static gchar *option_debug = NULL;
244 static gchar *option_device = NULL;
245 static gchar *option_plugin = NULL;
246 static gchar *option_nodevice = NULL;
247 static gchar *option_noplugin = NULL;
248 static gchar *option_wifi = NULL;
249 static gboolean option_detach = TRUE;
250 static gboolean option_dnsproxy = TRUE;
251 static gboolean option_compat = FALSE;
252 static gboolean option_version = FALSE;
254 static gboolean parse_debug(const char *key, const char *value,
255 gpointer user_data, GError **error)
258 option_debug = g_strdup(value);
260 option_debug = g_strdup("*");
265 static GOptionEntry options[] = {
266 { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
267 G_OPTION_ARG_CALLBACK, parse_debug,
268 "Specify debug options to enable", "DEBUG" },
269 { "device", 'i', 0, G_OPTION_ARG_STRING, &option_device,
270 "Specify networking device or interface", "DEV" },
271 { "nodevice", 'I', 0, G_OPTION_ARG_STRING, &option_nodevice,
272 "Specify networking interface to ignore", "DEV" },
273 { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
274 "Specify plugins to load", "NAME,..." },
275 { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
276 "Specify plugins not to load", "NAME,..." },
277 { "wifi", 'W', 0, G_OPTION_ARG_STRING, &option_wifi,
278 "Specify driver for WiFi/Supplicant", "NAME" },
279 { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
280 G_OPTION_ARG_NONE, &option_detach,
281 "Don't fork daemon to background" },
282 { "nodnsproxy", 'r', G_OPTION_FLAG_REVERSE,
283 G_OPTION_ARG_NONE, &option_dnsproxy,
284 "Don't enable DNS Proxy" },
285 { "compat", 'c', 0, G_OPTION_ARG_NONE, &option_compat,
287 { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
288 "Show version information and exit" },
292 const char *connman_option_get_string(const char *key)
294 if (g_strcmp0(key, "wifi") == 0) {
295 if (option_wifi == NULL)
296 return "nl80211,wext";
304 connman_bool_t connman_setting_get_bool(const char *key)
306 if (g_str_equal(key, "BackgroundScanning") == TRUE)
307 return connman_settings.bg_scan;
312 char **connman_setting_get_string_list(const char *key)
314 if (g_str_equal(key, "FallbackTimeservers") == TRUE)
315 return connman_settings.pref_timeservers;
320 unsigned int *connman_setting_get_uint_list(const char *key)
322 if (g_str_equal(key, "DefaultAutoConnectTechnologies") == TRUE)
323 return connman_settings.auto_connect;
325 if (g_str_equal(key, "PreferredTechnologies") == TRUE)
326 return connman_settings.preferred_techs;
331 int main(int argc, char *argv[])
333 GOptionContext *context;
334 GError *error = NULL;
335 DBusConnection *conn;
341 /* Drop capabilities */
345 if (g_thread_supported() == FALSE)
349 context = g_option_context_new(NULL);
350 g_option_context_add_main_entries(context, options, NULL);
352 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
354 g_printerr("%s\n", error->message);
357 g_printerr("An unknown error occurred\n");
361 g_option_context_free(context);
363 if (option_version == TRUE) {
364 printf("%s\n", VERSION);
368 if (option_detach == TRUE) {
370 perror("Can't start daemon");
375 if (mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
376 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
378 perror("Failed to create state directory");
381 if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
382 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
384 perror("Failed to create storage directory");
389 main_loop = g_main_loop_new(NULL, FALSE);
392 if (dbus_threads_init_default() == FALSE) {
393 fprintf(stderr, "Can't init usage of threads\n");
398 signal = setup_signalfd();
400 dbus_error_init(&err);
402 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
404 if (dbus_error_is_set(&err) == TRUE) {
405 fprintf(stderr, "%s\n", err.message);
406 dbus_error_free(&err);
408 fprintf(stderr, "Can't register with system bus\n");
412 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
414 __connman_log_init(argv[0], option_debug, option_detach);
416 __connman_dbus_init(conn);
418 config = load_config(CONFIGDIR "/main.conf");
419 if (config != NULL) {
420 parse_config(config);
421 g_key_file_free(config);
424 __connman_storage_migrate();
425 __connman_technology_init();
426 __connman_notifier_init();
427 __connman_service_init();
428 __connman_provider_init();
429 __connman_network_init();
430 __connman_device_init(option_device, option_nodevice);
432 __connman_agent_init();
433 __connman_ippool_init();
434 __connman_iptables_init();
435 __connman_nat_init();
436 __connman_tethering_init();
437 __connman_counter_init();
438 __connman_manager_init();
439 __connman_config_init();
440 __connman_stats_init();
441 __connman_clock_init();
443 __connman_resolver_init(option_dnsproxy);
444 __connman_ipconfig_init();
445 __connman_rtnl_init();
446 __connman_task_init();
447 __connman_proxy_init();
448 __connman_detect_init();
449 __connman_session_init();
450 __connman_timeserver_init();
451 __connman_connection_init();
453 __connman_plugin_init(option_plugin, option_noplugin);
455 __connman_rtnl_start();
456 __connman_dhcp_init();
457 __connman_dhcpv6_init();
458 __connman_wpad_init();
459 __connman_wispr_init();
460 __connman_rfkill_init();
462 g_free(option_device);
463 g_free(option_plugin);
464 g_free(option_nodevice);
465 g_free(option_noplugin);
467 g_main_loop_run(main_loop);
469 g_source_remove(signal);
471 __connman_rfkill_cleanup();
472 __connman_wispr_cleanup();
473 __connman_wpad_cleanup();
474 __connman_dhcpv6_cleanup();
475 __connman_dhcp_cleanup();
476 __connman_provider_cleanup();
477 __connman_plugin_cleanup();
478 __connman_connection_cleanup();
479 __connman_timeserver_cleanup();
480 __connman_session_cleanup();
481 __connman_detect_cleanup();
482 __connman_proxy_cleanup();
483 __connman_task_cleanup();
484 __connman_rtnl_cleanup();
485 __connman_resolver_cleanup();
487 __connman_clock_cleanup();
488 __connman_stats_cleanup();
489 __connman_config_cleanup();
490 __connman_manager_cleanup();
491 __connman_counter_cleanup();
492 __connman_agent_cleanup();
493 __connman_tethering_cleanup();
494 __connman_nat_cleanup();
495 __connman_iptables_cleanup();
496 __connman_ippool_cleanup();
497 __connman_device_cleanup();
498 __connman_network_cleanup();
499 __connman_service_cleanup();
500 __connman_ipconfig_cleanup();
501 __connman_notifier_cleanup();
502 __connman_technology_cleanup();
504 __connman_dbus_cleanup();
506 __connman_log_cleanup();
508 dbus_connection_unref(conn);
510 g_main_loop_unref(main_loop);
512 if (connman_settings.pref_timeservers != NULL)
513 g_strfreev(connman_settings.pref_timeservers);
515 g_free(connman_settings.auto_connect);
516 g_free(connman_settings.preferred_techs);
518 g_free(option_debug);