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 } connman_settings = {
51 static GKeyFile *load_config(const char *file)
56 keyfile = g_key_file_new();
58 g_key_file_set_list_separator(keyfile, ',');
60 if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
61 if (err->code != G_FILE_ERROR_NOENT) {
62 connman_error("Parsing %s failed: %s", file,
67 g_key_file_free(keyfile);
74 static void parse_config(GKeyFile *config)
82 DBG("parsing main.conf");
84 boolean = g_key_file_get_boolean(config, "General",
85 "BackgroundScanning", &error);
87 connman_settings.bg_scan = boolean;
89 g_clear_error(&error);
92 static GMainLoop *main_loop = NULL;
94 static unsigned int __terminated = 0;
96 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
99 struct signalfd_siginfo si;
103 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
106 fd = g_io_channel_unix_get_fd(channel);
108 result = read(fd, &si, sizeof(si));
109 if (result != sizeof(si))
112 switch (si.ssi_signo) {
115 if (__terminated == 0) {
116 connman_info("Terminating");
117 g_main_loop_quit(main_loop);
127 static guint setup_signalfd(void)
135 sigaddset(&mask, SIGINT);
136 sigaddset(&mask, SIGTERM);
138 if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
139 perror("Failed to set signal mask");
143 fd = signalfd(-1, &mask, 0);
145 perror("Failed to create signal descriptor");
149 channel = g_io_channel_unix_new(fd);
151 g_io_channel_set_close_on_unref(channel, TRUE);
152 g_io_channel_set_encoding(channel, NULL, NULL);
153 g_io_channel_set_buffered(channel, FALSE);
155 source = g_io_add_watch(channel,
156 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
157 signal_handler, NULL);
159 g_io_channel_unref(channel);
164 static void disconnect_callback(DBusConnection *conn, void *user_data)
166 connman_error("D-Bus disconnect");
168 g_main_loop_quit(main_loop);
171 static gchar *option_debug = NULL;
172 static gchar *option_device = NULL;
173 static gchar *option_plugin = NULL;
174 static gchar *option_nodevice = NULL;
175 static gchar *option_noplugin = NULL;
176 static gchar *option_wifi = NULL;
177 static gboolean option_detach = TRUE;
178 static gboolean option_dnsproxy = TRUE;
179 static gboolean option_compat = FALSE;
180 static gboolean option_version = FALSE;
182 static gboolean parse_debug(const char *key, const char *value,
183 gpointer user_data, GError **error)
186 option_debug = g_strdup(value);
188 option_debug = g_strdup("*");
193 static GOptionEntry options[] = {
194 { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
195 G_OPTION_ARG_CALLBACK, parse_debug,
196 "Specify debug options to enable", "DEBUG" },
197 { "device", 'i', 0, G_OPTION_ARG_STRING, &option_device,
198 "Specify networking device or interface", "DEV" },
199 { "nodevice", 'I', 0, G_OPTION_ARG_STRING, &option_nodevice,
200 "Specify networking interface to ignore", "DEV" },
201 { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
202 "Specify plugins to load", "NAME,..." },
203 { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
204 "Specify plugins not to load", "NAME,..." },
205 { "wifi", 'W', 0, G_OPTION_ARG_STRING, &option_wifi,
206 "Specify driver for WiFi/Supplicant", "NAME" },
207 { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
208 G_OPTION_ARG_NONE, &option_detach,
209 "Don't fork daemon to background" },
210 { "nodnsproxy", 'r', G_OPTION_FLAG_REVERSE,
211 G_OPTION_ARG_NONE, &option_dnsproxy,
212 "Don't enable DNS Proxy" },
213 { "compat", 'c', 0, G_OPTION_ARG_NONE, &option_compat,
215 { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
216 "Show version information and exit" },
220 const char *connman_option_get_string(const char *key)
222 if (g_strcmp0(key, "wifi") == 0) {
223 if (option_wifi == NULL)
224 return "nl80211,wext";
232 connman_bool_t connman_setting_get_bool(const char *key)
234 if (g_str_equal(key, "BackgroundScanning") == TRUE)
235 return connman_settings.bg_scan;
240 int main(int argc, char *argv[])
242 GOptionContext *context;
243 GError *error = NULL;
244 DBusConnection *conn;
250 /* Drop capabilities */
254 if (g_thread_supported() == FALSE)
258 context = g_option_context_new(NULL);
259 g_option_context_add_main_entries(context, options, NULL);
261 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
263 g_printerr("%s\n", error->message);
266 g_printerr("An unknown error occurred\n");
270 g_option_context_free(context);
272 if (option_version == TRUE) {
273 printf("%s\n", VERSION);
277 if (option_detach == TRUE) {
279 perror("Can't start daemon");
284 if (mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
285 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
287 perror("Failed to create state directory");
290 if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
291 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
293 perror("Failed to create storage directory");
298 main_loop = g_main_loop_new(NULL, FALSE);
301 if (dbus_threads_init_default() == FALSE) {
302 fprintf(stderr, "Can't init usage of threads\n");
307 signal = setup_signalfd();
309 dbus_error_init(&err);
311 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
313 if (dbus_error_is_set(&err) == TRUE) {
314 fprintf(stderr, "%s\n", err.message);
315 dbus_error_free(&err);
317 fprintf(stderr, "Can't register with system bus\n");
321 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
323 __connman_log_init(option_debug, option_detach);
325 __connman_dbus_init(conn);
327 config = load_config(CONFIGDIR "/main.conf");
329 parse_config(config);
331 __connman_storage_migrate();
332 __connman_technology_init();
333 __connman_notifier_init();
334 __connman_location_init();
335 __connman_service_init();
336 __connman_provider_init();
337 __connman_network_init();
338 __connman_device_init(option_device, option_nodevice);
340 __connman_agent_init();
341 __connman_iptables_init();
342 __connman_tethering_init();
343 __connman_counter_init();
344 __connman_manager_init();
345 __connman_config_init();
346 __connman_stats_init();
347 __connman_clock_init();
349 __connman_resolver_init(option_dnsproxy);
350 __connman_ipconfig_init();
351 __connman_rtnl_init();
352 __connman_task_init();
353 __connman_proxy_init();
354 __connman_detect_init();
355 __connman_session_init();
356 __connman_timeserver_init();
357 __connman_connection_init();
359 __connman_plugin_init(option_plugin, option_noplugin);
361 __connman_rtnl_start();
362 __connman_dhcp_init();
363 __connman_wpad_init();
364 __connman_wispr_init();
365 __connman_rfkill_init();
367 g_free(option_device);
368 g_free(option_plugin);
369 g_free(option_nodevice);
370 g_free(option_noplugin);
372 g_main_loop_run(main_loop);
374 g_source_remove(signal);
376 __connman_rfkill_cleanup();
377 __connman_wispr_cleanup();
378 __connman_wpad_cleanup();
379 __connman_dhcp_cleanup();
380 __connman_provider_cleanup();
381 __connman_plugin_cleanup();
382 __connman_connection_cleanup();
383 __connman_timeserver_cleanup();
384 __connman_session_cleanup();
385 __connman_detect_cleanup();
386 __connman_proxy_cleanup();
387 __connman_task_cleanup();
388 __connman_rtnl_cleanup();
389 __connman_ipconfig_cleanup();
390 __connman_resolver_cleanup();
392 __connman_clock_cleanup();
393 __connman_stats_cleanup();
394 __connman_config_cleanup();
395 __connman_manager_cleanup();
396 __connman_counter_cleanup();
397 __connman_agent_cleanup();
398 __connman_tethering_cleanup();
399 __connman_iptables_cleanup();
400 __connman_device_cleanup();
401 __connman_network_cleanup();
402 __connman_service_cleanup();
403 __connman_location_cleanup();
404 __connman_notifier_cleanup();
405 __connman_technology_cleanup();
407 __connman_dbus_cleanup();
409 __connman_log_cleanup();
411 dbus_connection_unref(conn);
413 g_main_loop_unref(main_loop);
416 g_key_file_free(config);