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 } connman_settings = {
50 .pref_timeservers = NULL,
53 static GKeyFile *load_config(const char *file)
58 keyfile = g_key_file_new();
60 g_key_file_set_list_separator(keyfile, ',');
62 if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
63 if (err->code != G_FILE_ERROR_NOENT) {
64 connman_error("Parsing %s failed: %s", file,
69 g_key_file_free(keyfile);
76 static void parse_config(GKeyFile *config)
85 DBG("parsing main.conf");
87 boolean = g_key_file_get_boolean(config, "General",
88 "BackgroundScanning", &error);
90 connman_settings.bg_scan = boolean;
92 g_clear_error(&error);
94 timeservers = g_key_file_get_string_list(config, "General",
95 "FallbackTimeservers", NULL, &error);
97 connman_settings.pref_timeservers = timeservers;
99 g_clear_error(&error);
102 static GMainLoop *main_loop = NULL;
104 static unsigned int __terminated = 0;
106 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
109 struct signalfd_siginfo si;
113 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
116 fd = g_io_channel_unix_get_fd(channel);
118 result = read(fd, &si, sizeof(si));
119 if (result != sizeof(si))
122 switch (si.ssi_signo) {
125 if (__terminated == 0) {
126 connman_info("Terminating");
127 g_main_loop_quit(main_loop);
137 static guint setup_signalfd(void)
145 sigaddset(&mask, SIGINT);
146 sigaddset(&mask, SIGTERM);
148 if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
149 perror("Failed to set signal mask");
153 fd = signalfd(-1, &mask, 0);
155 perror("Failed to create signal descriptor");
159 channel = g_io_channel_unix_new(fd);
161 g_io_channel_set_close_on_unref(channel, TRUE);
162 g_io_channel_set_encoding(channel, NULL, NULL);
163 g_io_channel_set_buffered(channel, FALSE);
165 source = g_io_add_watch(channel,
166 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
167 signal_handler, NULL);
169 g_io_channel_unref(channel);
174 static void disconnect_callback(DBusConnection *conn, void *user_data)
176 connman_error("D-Bus disconnect");
178 g_main_loop_quit(main_loop);
181 static gchar *option_debug = NULL;
182 static gchar *option_device = NULL;
183 static gchar *option_plugin = NULL;
184 static gchar *option_nodevice = NULL;
185 static gchar *option_noplugin = NULL;
186 static gchar *option_wifi = NULL;
187 static gboolean option_detach = TRUE;
188 static gboolean option_dnsproxy = TRUE;
189 static gboolean option_compat = FALSE;
190 static gboolean option_version = FALSE;
192 static gboolean parse_debug(const char *key, const char *value,
193 gpointer user_data, GError **error)
196 option_debug = g_strdup(value);
198 option_debug = g_strdup("*");
203 static GOptionEntry options[] = {
204 { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
205 G_OPTION_ARG_CALLBACK, parse_debug,
206 "Specify debug options to enable", "DEBUG" },
207 { "device", 'i', 0, G_OPTION_ARG_STRING, &option_device,
208 "Specify networking device or interface", "DEV" },
209 { "nodevice", 'I', 0, G_OPTION_ARG_STRING, &option_nodevice,
210 "Specify networking interface to ignore", "DEV" },
211 { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
212 "Specify plugins to load", "NAME,..." },
213 { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
214 "Specify plugins not to load", "NAME,..." },
215 { "wifi", 'W', 0, G_OPTION_ARG_STRING, &option_wifi,
216 "Specify driver for WiFi/Supplicant", "NAME" },
217 { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
218 G_OPTION_ARG_NONE, &option_detach,
219 "Don't fork daemon to background" },
220 { "nodnsproxy", 'r', G_OPTION_FLAG_REVERSE,
221 G_OPTION_ARG_NONE, &option_dnsproxy,
222 "Don't enable DNS Proxy" },
223 { "compat", 'c', 0, G_OPTION_ARG_NONE, &option_compat,
225 { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
226 "Show version information and exit" },
230 const char *connman_option_get_string(const char *key)
232 if (g_strcmp0(key, "wifi") == 0) {
233 if (option_wifi == NULL)
234 return "nl80211,wext";
242 connman_bool_t connman_setting_get_bool(const char *key)
244 if (g_str_equal(key, "BackgroundScanning") == TRUE)
245 return connman_settings.bg_scan;
250 char **connman_setting_get_string_list(const char *key)
252 if (g_str_equal(key, "FallbackTimeservers") == TRUE)
253 return connman_settings.pref_timeservers;
258 int main(int argc, char *argv[])
260 GOptionContext *context;
261 GError *error = NULL;
262 DBusConnection *conn;
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 signal = setup_signalfd();
327 dbus_error_init(&err);
329 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
331 if (dbus_error_is_set(&err) == TRUE) {
332 fprintf(stderr, "%s\n", err.message);
333 dbus_error_free(&err);
335 fprintf(stderr, "Can't register with system bus\n");
339 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
341 __connman_log_init(argv[0], option_debug, option_detach);
343 __connman_dbus_init(conn);
345 config = load_config(CONFIGDIR "/main.conf");
347 parse_config(config);
349 __connman_storage_migrate();
350 __connman_technology_init();
351 __connman_notifier_init();
352 __connman_service_init();
353 __connman_provider_init();
354 __connman_network_init();
355 __connman_device_init(option_device, option_nodevice);
357 __connman_agent_init();
358 __connman_ippool_init();
359 __connman_iptables_init();
360 __connman_nat_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_dhcpv6_init();
383 __connman_wpad_init();
384 __connman_wispr_init();
385 __connman_rfkill_init();
387 g_free(option_device);
388 g_free(option_plugin);
389 g_free(option_nodevice);
390 g_free(option_noplugin);
392 g_main_loop_run(main_loop);
394 g_source_remove(signal);
396 __connman_rfkill_cleanup();
397 __connman_wispr_cleanup();
398 __connman_wpad_cleanup();
399 __connman_dhcpv6_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_nat_cleanup();
420 __connman_iptables_cleanup();
421 __connman_ippool_cleanup();
422 __connman_device_cleanup();
423 __connman_network_cleanup();
424 __connman_service_cleanup();
425 __connman_ipconfig_cleanup();
426 __connman_notifier_cleanup();
427 __connman_technology_cleanup();
429 __connman_dbus_cleanup();
431 __connman_log_cleanup();
433 dbus_connection_unref(conn);
435 g_main_loop_unref(main_loop);
438 g_key_file_free(config);
440 if (connman_settings.pref_timeservers != NULL)
441 g_strfreev(connman_settings.pref_timeservers);
443 g_free(option_debug);