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
31 #if !defined TIZEN_EXT
33 #include <sys/signalfd.h>
48 connman_bool_t bg_scan;
49 connman_bool_t single_connection;
50 } connman_settings = {
52 .single_connection = FALSE,
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 void parse_config(GKeyFile *config)
86 DBG("parsing main.conf");
88 boolean = g_key_file_get_boolean(config, "General",
89 "BackgroundScanning", &error);
91 connman_settings.bg_scan = boolean;
93 g_clear_error(&error);
95 boolean = g_key_file_get_boolean(config, "General",
96 "SingleConnection", &error);
98 connman_settings.single_connection = boolean;
100 g_clear_error(&error);
103 static GMainLoop *main_loop = NULL;
105 #if !defined TIZEN_EXT
106 static unsigned int __terminated = 0;
108 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
111 struct signalfd_siginfo si;
115 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
118 fd = g_io_channel_unix_get_fd(channel);
120 result = read(fd, &si, sizeof(si));
121 if (result != sizeof(si))
124 switch (si.ssi_signo) {
127 if (__terminated == 0) {
128 connman_info("Terminating");
129 g_main_loop_quit(main_loop);
139 static guint setup_signalfd(void)
147 sigaddset(&mask, SIGINT);
148 sigaddset(&mask, SIGTERM);
150 if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
151 perror("Failed to set signal mask");
155 fd = signalfd(-1, &mask, 0);
157 perror("Failed to create signal descriptor");
161 channel = g_io_channel_unix_new(fd);
163 g_io_channel_set_close_on_unref(channel, TRUE);
164 g_io_channel_set_encoding(channel, NULL, NULL);
165 g_io_channel_set_buffered(channel, FALSE);
167 source = g_io_add_watch(channel,
168 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
169 signal_handler, NULL);
171 g_io_channel_unref(channel);
177 static void disconnect_callback(DBusConnection *conn, void *user_data)
179 connman_error("D-Bus disconnect");
181 g_main_loop_quit(main_loop);
184 static gchar *option_debug = NULL;
185 static gchar *option_device = NULL;
186 static gchar *option_plugin = NULL;
187 static gchar *option_nodevice = NULL;
188 static gchar *option_noplugin = NULL;
189 static gchar *option_wifi = NULL;
190 static gboolean option_detach = TRUE;
191 static gboolean option_dnsproxy = TRUE;
192 static gboolean option_compat = FALSE;
193 static gboolean option_version = FALSE;
195 static gboolean parse_debug(const char *key, const char *value,
196 gpointer user_data, GError **error)
199 option_debug = g_strdup(value);
201 option_debug = g_strdup("*");
206 static GOptionEntry options[] = {
207 { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
208 G_OPTION_ARG_CALLBACK, parse_debug,
209 "Specify debug options to enable", "DEBUG" },
210 { "device", 'i', 0, G_OPTION_ARG_STRING, &option_device,
211 "Specify networking device or interface", "DEV" },
212 { "nodevice", 'I', 0, G_OPTION_ARG_STRING, &option_nodevice,
213 "Specify networking interface to ignore", "DEV" },
214 { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
215 "Specify plugins to load", "NAME,..." },
216 { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
217 "Specify plugins not to load", "NAME,..." },
218 { "wifi", 'W', 0, G_OPTION_ARG_STRING, &option_wifi,
219 "Specify driver for WiFi/Supplicant", "NAME" },
220 { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
221 G_OPTION_ARG_NONE, &option_detach,
222 "Don't fork daemon to background" },
223 { "nodnsproxy", 'r', G_OPTION_FLAG_REVERSE,
224 G_OPTION_ARG_NONE, &option_dnsproxy,
225 "Don't enable DNS Proxy" },
226 { "compat", 'c', 0, G_OPTION_ARG_NONE, &option_compat,
228 { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
229 "Show version information and exit" },
233 const char *connman_option_get_string(const char *key)
235 if (g_strcmp0(key, "wifi") == 0) {
236 if (option_wifi == NULL)
237 return "nl80211,wext";
245 connman_bool_t connman_setting_get_bool(const char *key)
247 if (g_str_equal(key, "BackgroundScanning") == TRUE)
248 return connman_settings.bg_scan;
250 if (g_str_equal(key, "SingleConnection") == TRUE)
251 return connman_settings.single_connection;
256 int main(int argc, char *argv[])
258 GOptionContext *context;
259 GError *error = NULL;
260 DBusConnection *conn;
263 #if !defined TIZEN_EXT
268 /* Drop capabilities */
272 if (g_thread_supported() == FALSE)
276 context = g_option_context_new(NULL);
277 g_option_context_add_main_entries(context, options, NULL);
279 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
281 g_printerr("%s\n", error->message);
284 g_printerr("An unknown error occurred\n");
288 g_option_context_free(context);
290 if (option_version == TRUE) {
291 printf("%s\n", VERSION);
295 if (option_detach == TRUE) {
297 perror("Can't start daemon");
302 if (mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
303 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
305 perror("Failed to create state directory");
308 if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
309 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
311 perror("Failed to create storage directory");
316 main_loop = g_main_loop_new(NULL, FALSE);
319 if (dbus_threads_init_default() == FALSE) {
320 fprintf(stderr, "Can't init usage of threads\n");
325 #if !defined TIZEN_EXT
326 signal = setup_signalfd();
329 dbus_error_init(&err);
331 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
333 if (dbus_error_is_set(&err) == TRUE) {
334 fprintf(stderr, "%s\n", err.message);
335 dbus_error_free(&err);
337 fprintf(stderr, "Can't register with system bus\n");
341 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
343 __connman_log_init(argv[0], option_debug, option_detach);
345 __connman_dbus_init(conn);
347 config = load_config(CONFIGDIR "/main.conf");
349 parse_config(config);
351 __connman_storage_migrate();
352 __connman_technology_init();
353 __connman_notifier_init();
354 __connman_service_init();
355 __connman_provider_init();
356 __connman_network_init();
357 __connman_device_init(option_device, option_nodevice);
359 __connman_agent_init();
360 __connman_iptables_init();
361 __connman_tethering_init();
362 __connman_counter_init();
363 __connman_manager_init();
364 __connman_config_init();
365 __connman_stats_init();
366 __connman_clock_init();
368 __connman_resolver_init(option_dnsproxy);
369 __connman_ipconfig_init();
370 __connman_rtnl_init();
371 __connman_task_init();
372 __connman_proxy_init();
373 __connman_detect_init();
374 __connman_session_init();
375 __connman_timeserver_init();
376 __connman_connection_init();
378 __connman_plugin_init(option_plugin, option_noplugin);
380 __connman_rtnl_start();
381 __connman_dhcp_init();
382 __connman_wpad_init();
383 __connman_wispr_init();
384 __connman_rfkill_init();
386 g_free(option_device);
387 g_free(option_plugin);
388 g_free(option_nodevice);
389 g_free(option_noplugin);
391 g_main_loop_run(main_loop);
393 #if !defined TIZEN_EXT
394 g_source_remove(signal);
397 __connman_rfkill_cleanup();
398 __connman_wispr_cleanup();
399 __connman_wpad_cleanup();
400 __connman_dhcp_cleanup();
401 __connman_provider_cleanup();
402 __connman_plugin_cleanup();
403 __connman_connection_cleanup();
404 __connman_timeserver_cleanup();
405 __connman_session_cleanup();
406 __connman_detect_cleanup();
407 __connman_proxy_cleanup();
408 __connman_task_cleanup();
409 __connman_rtnl_cleanup();
410 __connman_resolver_cleanup();
412 __connman_clock_cleanup();
413 __connman_stats_cleanup();
414 __connman_config_cleanup();
415 __connman_manager_cleanup();
416 __connman_counter_cleanup();
417 __connman_agent_cleanup();
418 __connman_tethering_cleanup();
419 __connman_iptables_cleanup();
420 __connman_device_cleanup();
421 __connman_network_cleanup();
422 __connman_service_cleanup();
423 __connman_ipconfig_cleanup();
424 __connman_notifier_cleanup();
425 __connman_technology_cleanup();
427 __connman_dbus_cleanup();
429 __connman_log_cleanup();
431 dbus_connection_unref(conn);
433 g_main_loop_unref(main_loop);
436 g_key_file_free(config);
438 g_free(option_debug);