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 gboolean signal_cb(GIOChannel *chan,
95 GIOCondition cond, gpointer data)
97 int signal_fd = GPOINTER_TO_INT(data);
98 struct signalfd_siginfo si;
101 if (cond & (G_IO_NVAL | G_IO_ERR))
104 res = read(signal_fd, &si, sizeof(si));
105 if (res != sizeof(si))
108 switch (si.ssi_signo) {
111 connman_info("Terminating");
112 g_main_loop_quit(main_loop);
121 static void disconnect_callback(DBusConnection *conn, void *user_data)
123 connman_error("D-Bus disconnect");
125 g_main_loop_quit(main_loop);
128 static gchar *option_debug = NULL;
129 static gchar *option_device = NULL;
130 static gchar *option_plugin = NULL;
131 static gchar *option_nodevice = NULL;
132 static gchar *option_noplugin = NULL;
133 static gchar *option_wifi = NULL;
134 static gboolean option_detach = TRUE;
135 static gboolean option_dnsproxy = TRUE;
136 static gboolean option_compat = FALSE;
137 static gboolean option_version = FALSE;
139 static gboolean parse_debug(const char *key, const char *value,
140 gpointer user_data, GError **error)
143 option_debug = g_strdup(value);
145 option_debug = g_strdup("*");
150 static GOptionEntry options[] = {
151 { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
152 G_OPTION_ARG_CALLBACK, parse_debug,
153 "Specify debug options to enable", "DEBUG" },
154 { "device", 'i', 0, G_OPTION_ARG_STRING, &option_device,
155 "Specify networking device or interface", "DEV" },
156 { "nodevice", 'I', 0, G_OPTION_ARG_STRING, &option_nodevice,
157 "Specify networking interface to ignore", "DEV" },
158 { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
159 "Specify plugins to load", "NAME,..." },
160 { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
161 "Specify plugins not to load", "NAME,..." },
162 { "wifi", 'W', 0, G_OPTION_ARG_STRING, &option_wifi,
163 "Specify driver for WiFi/Supplicant", "NAME" },
164 { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
165 G_OPTION_ARG_NONE, &option_detach,
166 "Don't fork daemon to background" },
167 { "nodnsproxy", 'r', G_OPTION_FLAG_REVERSE,
168 G_OPTION_ARG_NONE, &option_dnsproxy,
169 "Don't enable DNS Proxy" },
170 { "compat", 'c', 0, G_OPTION_ARG_NONE, &option_compat,
172 { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
173 "Show version information and exit" },
177 const char *connman_option_get_string(const char *key)
179 if (g_strcmp0(key, "wifi") == 0) {
180 if (option_wifi == NULL)
181 return "nl80211,wext";
189 connman_bool_t connman_setting_get_bool(const char *key)
191 if (g_str_equal(key, "BackgroundScanning") == TRUE)
192 return connman_settings.bg_scan;
197 int main(int argc, char *argv[])
199 GOptionContext *context;
200 GError *error = NULL;
201 DBusConnection *conn;
205 GIOChannel *signal_io;
210 /* Drop capabilities */
214 if (g_thread_supported() == FALSE)
218 context = g_option_context_new(NULL);
219 g_option_context_add_main_entries(context, options, NULL);
221 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
223 g_printerr("%s\n", error->message);
226 g_printerr("An unknown error occurred\n");
230 g_option_context_free(context);
232 if (option_version == TRUE) {
233 printf("%s\n", VERSION);
237 if (option_detach == TRUE) {
239 perror("Can't start daemon");
244 if (mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
245 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
247 perror("Failed to create state directory");
250 if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
251 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
253 perror("Failed to create storage directory");
256 if (mkdir(STORAGEDIR "/stats", S_IRUSR | S_IWUSR | S_IXUSR |
257 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
259 perror("Failed to create statistics directory");
264 main_loop = g_main_loop_new(NULL, FALSE);
267 if (dbus_threads_init_default() == FALSE) {
268 fprintf(stderr, "Can't init usage of threads\n");
273 dbus_error_init(&err);
275 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
277 if (dbus_error_is_set(&err) == TRUE) {
278 fprintf(stderr, "%s\n", err.message);
279 dbus_error_free(&err);
281 fprintf(stderr, "Can't register with system bus\n");
285 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
287 __connman_log_init(option_debug, option_detach);
289 __connman_dbus_init(conn);
291 config = load_config(CONFIGDIR "/main.conf");
293 parse_config(config);
295 __connman_storage_init();
296 __connman_technology_init();
297 __connman_notifier_init();
298 __connman_location_init();
299 __connman_service_init();
300 __connman_provider_init();
301 __connman_network_init();
302 __connman_device_init(option_device, option_nodevice);
304 __connman_agent_init();
305 __connman_iptables_init();
306 __connman_tethering_init();
307 __connman_counter_init();
308 __connman_manager_init();
309 __connman_profile_init();
310 __connman_config_init();
311 __connman_stats_init();
312 __connman_clock_init();
314 __connman_resolver_init(option_dnsproxy);
315 __connman_ipconfig_init();
316 __connman_rtnl_init();
317 __connman_task_init();
318 __connman_proxy_init();
319 __connman_detect_init();
320 __connman_session_init();
321 __connman_timeserver_init();
322 __connman_connection_init();
324 __connman_plugin_init(option_plugin, option_noplugin);
326 __connman_storage_init_profile();
328 __connman_rtnl_start();
329 __connman_dhcp_init();
330 __connman_wpad_init();
331 __connman_wispr_init();
332 __connman_rfkill_init();
334 g_free(option_device);
335 g_free(option_plugin);
336 g_free(option_nodevice);
337 g_free(option_noplugin);
340 sigaddset(&mask, SIGTERM);
341 sigaddset(&mask, SIGINT);
343 if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
344 fprintf(stderr, "Could not block signals\n");
348 signal_fd = signalfd(-1, &mask, 0);
350 fprintf(stderr, "Could not init signalfd\n");
354 signal_io = g_io_channel_unix_new(signal_fd);
355 g_io_channel_set_close_on_unref(signal_io, TRUE);
356 signal_source = g_io_add_watch(signal_io,
357 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
358 signal_cb, GINT_TO_POINTER(signal_fd));
359 g_io_channel_unref(signal_io);
361 g_main_loop_run(main_loop);
363 g_source_remove(signal_source);
365 __connman_rfkill_cleanup();
366 __connman_wispr_cleanup();
367 __connman_wpad_cleanup();
368 __connman_dhcp_cleanup();
369 __connman_provider_cleanup();
370 __connman_plugin_cleanup();
371 __connman_connection_cleanup();
372 __connman_timeserver_cleanup();
373 __connman_session_cleanup();
374 __connman_detect_cleanup();
375 __connman_proxy_cleanup();
376 __connman_task_cleanup();
377 __connman_rtnl_cleanup();
378 __connman_ipconfig_cleanup();
379 __connman_resolver_cleanup();
381 __connman_clock_cleanup();
382 __connman_stats_cleanup();
383 __connman_config_cleanup();
384 __connman_profile_cleanup();
385 __connman_manager_cleanup();
386 __connman_counter_cleanup();
387 __connman_agent_cleanup();
388 __connman_tethering_cleanup();
389 __connman_iptables_cleanup();
390 __connman_device_cleanup();
391 __connman_network_cleanup();
392 __connman_service_cleanup();
393 __connman_location_cleanup();
394 __connman_notifier_cleanup();
395 __connman_technology_cleanup();
396 __connman_storage_cleanup();
398 __connman_dbus_cleanup();
400 __connman_log_cleanup();
402 dbus_connection_unref(conn);
404 g_main_loop_unref(main_loop);
407 g_key_file_free(config);