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 } connman_settings = {
51 .pref_timeservers = NULL,
55 static GKeyFile *load_config(const char *file)
60 keyfile = g_key_file_new();
62 g_key_file_set_list_separator(keyfile, ',');
64 if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
65 if (err->code != G_FILE_ERROR_NOENT) {
66 connman_error("Parsing %s failed: %s", file,
71 g_key_file_free(keyfile);
78 static uint *parse_service_types(char **str_list, gsize len)
80 unsigned int *type_list;
82 enum connman_service_type type;
84 type_list = g_try_new0(unsigned int, len + 1);
85 if (type_list == NULL)
90 while (str_list[i] != NULL)
92 type = __connman_service_string2type(str_list[i]);
94 if (type != CONNMAN_SERVICE_TYPE_UNKNOWN) {
104 static void parse_config(GKeyFile *config)
106 GError *error = NULL;
111 char *default_auto_connect[] = {
120 DBG("parsing main.conf");
122 boolean = g_key_file_get_boolean(config, "General",
123 "BackgroundScanning", &error);
125 connman_settings.bg_scan = boolean;
127 g_clear_error(&error);
129 timeservers = g_key_file_get_string_list(config, "General",
130 "FallbackTimeservers", NULL, &error);
132 connman_settings.pref_timeservers = timeservers;
134 g_clear_error(&error);
136 str_list = g_key_file_get_string_list(config, "General",
137 "DefaultAutoConnectTechnologies", &len, &error);
140 connman_settings.auto_connect =
141 parse_service_types(str_list, len);
143 connman_settings.auto_connect =
144 parse_service_types(default_auto_connect, 3);
146 g_strfreev(str_list);
148 g_clear_error(&error);
151 static GMainLoop *main_loop = NULL;
153 static unsigned int __terminated = 0;
155 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
158 struct signalfd_siginfo si;
162 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
165 fd = g_io_channel_unix_get_fd(channel);
167 result = read(fd, &si, sizeof(si));
168 if (result != sizeof(si))
171 switch (si.ssi_signo) {
174 if (__terminated == 0) {
175 connman_info("Terminating");
176 g_main_loop_quit(main_loop);
186 static guint setup_signalfd(void)
194 sigaddset(&mask, SIGINT);
195 sigaddset(&mask, SIGTERM);
197 if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
198 perror("Failed to set signal mask");
202 fd = signalfd(-1, &mask, 0);
204 perror("Failed to create signal descriptor");
208 channel = g_io_channel_unix_new(fd);
210 g_io_channel_set_close_on_unref(channel, TRUE);
211 g_io_channel_set_encoding(channel, NULL, NULL);
212 g_io_channel_set_buffered(channel, FALSE);
214 source = g_io_add_watch(channel,
215 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
216 signal_handler, NULL);
218 g_io_channel_unref(channel);
223 static void disconnect_callback(DBusConnection *conn, void *user_data)
225 connman_error("D-Bus disconnect");
227 g_main_loop_quit(main_loop);
230 static gchar *option_debug = NULL;
231 static gchar *option_device = NULL;
232 static gchar *option_plugin = NULL;
233 static gchar *option_nodevice = NULL;
234 static gchar *option_noplugin = NULL;
235 static gchar *option_wifi = NULL;
236 static gboolean option_detach = TRUE;
237 static gboolean option_dnsproxy = TRUE;
238 static gboolean option_compat = FALSE;
239 static gboolean option_version = FALSE;
241 static gboolean parse_debug(const char *key, const char *value,
242 gpointer user_data, GError **error)
245 option_debug = g_strdup(value);
247 option_debug = g_strdup("*");
252 static GOptionEntry options[] = {
253 { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
254 G_OPTION_ARG_CALLBACK, parse_debug,
255 "Specify debug options to enable", "DEBUG" },
256 { "device", 'i', 0, G_OPTION_ARG_STRING, &option_device,
257 "Specify networking device or interface", "DEV" },
258 { "nodevice", 'I', 0, G_OPTION_ARG_STRING, &option_nodevice,
259 "Specify networking interface to ignore", "DEV" },
260 { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
261 "Specify plugins to load", "NAME,..." },
262 { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
263 "Specify plugins not to load", "NAME,..." },
264 { "wifi", 'W', 0, G_OPTION_ARG_STRING, &option_wifi,
265 "Specify driver for WiFi/Supplicant", "NAME" },
266 { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
267 G_OPTION_ARG_NONE, &option_detach,
268 "Don't fork daemon to background" },
269 { "nodnsproxy", 'r', G_OPTION_FLAG_REVERSE,
270 G_OPTION_ARG_NONE, &option_dnsproxy,
271 "Don't enable DNS Proxy" },
272 { "compat", 'c', 0, G_OPTION_ARG_NONE, &option_compat,
274 { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
275 "Show version information and exit" },
279 const char *connman_option_get_string(const char *key)
281 if (g_strcmp0(key, "wifi") == 0) {
282 if (option_wifi == NULL)
283 return "nl80211,wext";
291 connman_bool_t connman_setting_get_bool(const char *key)
293 if (g_str_equal(key, "BackgroundScanning") == TRUE)
294 return connman_settings.bg_scan;
299 char **connman_setting_get_string_list(const char *key)
301 if (g_str_equal(key, "FallbackTimeservers") == TRUE)
302 return connman_settings.pref_timeservers;
307 unsigned int *connman_setting_get_uint_list(const char *key)
309 if (g_str_equal(key, "DefaultAutoConnectTechnologies") == TRUE)
310 return connman_settings.auto_connect;
315 int main(int argc, char *argv[])
317 GOptionContext *context;
318 GError *error = NULL;
319 DBusConnection *conn;
325 /* Drop capabilities */
329 if (g_thread_supported() == FALSE)
333 context = g_option_context_new(NULL);
334 g_option_context_add_main_entries(context, options, NULL);
336 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
338 g_printerr("%s\n", error->message);
341 g_printerr("An unknown error occurred\n");
345 g_option_context_free(context);
347 if (option_version == TRUE) {
348 printf("%s\n", VERSION);
352 if (option_detach == TRUE) {
354 perror("Can't start daemon");
359 if (mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
360 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
362 perror("Failed to create state directory");
365 if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
366 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
368 perror("Failed to create storage directory");
373 main_loop = g_main_loop_new(NULL, FALSE);
376 if (dbus_threads_init_default() == FALSE) {
377 fprintf(stderr, "Can't init usage of threads\n");
382 signal = setup_signalfd();
384 dbus_error_init(&err);
386 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
388 if (dbus_error_is_set(&err) == TRUE) {
389 fprintf(stderr, "%s\n", err.message);
390 dbus_error_free(&err);
392 fprintf(stderr, "Can't register with system bus\n");
396 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
398 __connman_log_init(argv[0], option_debug, option_detach);
400 __connman_dbus_init(conn);
402 config = load_config(CONFIGDIR "/main.conf");
403 if (config != NULL) {
404 parse_config(config);
405 g_key_file_free(config);
408 __connman_storage_migrate();
409 __connman_technology_init();
410 __connman_notifier_init();
411 __connman_service_init();
412 __connman_provider_init();
413 __connman_network_init();
414 __connman_device_init(option_device, option_nodevice);
416 __connman_agent_init();
417 __connman_ippool_init();
418 __connman_iptables_init();
419 __connman_nat_init();
420 __connman_tethering_init();
421 __connman_counter_init();
422 __connman_manager_init();
423 __connman_config_init();
424 __connman_stats_init();
425 __connman_clock_init();
427 __connman_resolver_init(option_dnsproxy);
428 __connman_ipconfig_init();
429 __connman_rtnl_init();
430 __connman_task_init();
431 __connman_proxy_init();
432 __connman_detect_init();
433 __connman_session_init();
434 __connman_timeserver_init();
435 __connman_connection_init();
437 __connman_plugin_init(option_plugin, option_noplugin);
439 __connman_rtnl_start();
440 __connman_dhcp_init();
441 __connman_dhcpv6_init();
442 __connman_wpad_init();
443 __connman_wispr_init();
444 __connman_rfkill_init();
446 g_free(option_device);
447 g_free(option_plugin);
448 g_free(option_nodevice);
449 g_free(option_noplugin);
451 g_main_loop_run(main_loop);
453 g_source_remove(signal);
455 __connman_rfkill_cleanup();
456 __connman_wispr_cleanup();
457 __connman_wpad_cleanup();
458 __connman_dhcpv6_cleanup();
459 __connman_dhcp_cleanup();
460 __connman_provider_cleanup();
461 __connman_plugin_cleanup();
462 __connman_connection_cleanup();
463 __connman_timeserver_cleanup();
464 __connman_session_cleanup();
465 __connman_detect_cleanup();
466 __connman_proxy_cleanup();
467 __connman_task_cleanup();
468 __connman_rtnl_cleanup();
469 __connman_resolver_cleanup();
471 __connman_clock_cleanup();
472 __connman_stats_cleanup();
473 __connman_config_cleanup();
474 __connman_manager_cleanup();
475 __connman_counter_cleanup();
476 __connman_agent_cleanup();
477 __connman_tethering_cleanup();
478 __connman_nat_cleanup();
479 __connman_iptables_cleanup();
480 __connman_ippool_cleanup();
481 __connman_device_cleanup();
482 __connman_network_cleanup();
483 __connman_service_cleanup();
484 __connman_ipconfig_cleanup();
485 __connman_notifier_cleanup();
486 __connman_technology_cleanup();
488 __connman_dbus_cleanup();
490 __connman_log_cleanup();
492 dbus_connection_unref(conn);
494 g_main_loop_unref(main_loop);
496 if (connman_settings.pref_timeservers != NULL)
497 g_strfreev(connman_settings.pref_timeservers);
499 g_free(connman_settings.auto_connect);
501 g_free(option_debug);