5 * Copyright (C) 2007-2009 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
39 static GMainLoop *main_loop = NULL;
41 static void sig_term(int sig)
43 connman_info("Terminating");
45 g_main_loop_quit(main_loop);
48 static void sig_debug(int sig)
50 __connman_toggle_debug();
53 static void disconnect_callback(DBusConnection *conn, void *user_data)
55 connman_error("D-Bus disconnect");
57 g_main_loop_quit(main_loop);
60 static gchar *option_device = NULL;
61 static gchar *option_plugin = NULL;
62 static gchar *option_nodevice = NULL;
63 static gchar *option_noplugin = NULL;
64 static gchar *option_wifi = NULL;
65 static gboolean option_detach = TRUE;
66 static gboolean option_compat = FALSE;
67 static gboolean option_debug = FALSE;
68 static gboolean option_selftest = FALSE;
69 static gboolean option_version = FALSE;
71 static GOptionEntry options[] = {
72 { "device", 'i', 0, G_OPTION_ARG_STRING, &option_device,
73 "Specify networking device or interface", "DEV" },
74 { "nodevice", 'I', 0, G_OPTION_ARG_STRING, &option_nodevice,
75 "Specify networking interface to ignore", "DEV" },
76 { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
77 "Specify plugins to load", "NAME" },
78 { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
79 "Specify plugins not to load", "NAME" },
80 { "wifi", 'W', 0, G_OPTION_ARG_STRING, &option_wifi,
81 "Specify driver for WiFi/Supplicant", "NAME" },
82 { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
83 G_OPTION_ARG_NONE, &option_detach,
84 "Don't fork daemon to background" },
85 { "compat", 'c', 0, G_OPTION_ARG_NONE, &option_compat,
86 "Enable Network Manager compatibility" },
87 { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug,
88 "Enable debug information output" },
89 { "selftest", 't', 0, G_OPTION_ARG_NONE, &option_selftest,
90 "Run self testing routines" },
91 { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
92 "Show version information and exit" },
96 const char *connman_option_get_string(const char *key)
98 if (g_strcmp0(key, "wifi") == 0) {
99 if (option_wifi == NULL)
108 int main(int argc, char *argv[])
110 GOptionContext *context;
111 GError *error = NULL;
112 DBusConnection *conn;
117 if (g_thread_supported() == FALSE)
121 context = g_option_context_new(NULL);
122 g_option_context_add_main_entries(context, options, NULL);
124 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
126 g_printerr("%s\n", error->message);
129 g_printerr("An unknown error occurred\n");
133 g_option_context_free(context);
135 if (option_version == TRUE) {
136 printf("%s\n", VERSION);
140 if (option_detach == TRUE) {
142 perror("Can't start daemon");
147 if (mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
148 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
150 perror("Failed to create state directory");
153 if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
154 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
156 perror("Failed to create storage directory");
159 main_loop = g_main_loop_new(NULL, FALSE);
162 if (dbus_threads_init_default() == FALSE) {
163 fprintf(stderr, "Can't init usage of threads\n");
168 dbus_error_init(&err);
170 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
172 if (dbus_error_is_set(&err) == TRUE) {
173 fprintf(stderr, "%s\n", err.message);
174 dbus_error_free(&err);
176 fprintf(stderr, "Can't register with system bus\n");
180 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
182 if (option_compat == TRUE) {
183 if (g_dbus_request_name(conn, NM_SERVICE, NULL) == FALSE) {
184 fprintf(stderr, "Can't register compat service\n");
185 option_compat = FALSE;
189 __connman_log_init(option_detach, option_debug);
191 if (option_selftest == TRUE) {
192 if (__connman_selftest() < 0) {
193 connman_error("Self testing routines failed");
198 __connman_dbus_init(conn);
200 __connman_storage_init();
201 __connman_element_init(conn, option_device, option_nodevice);
203 __connman_agent_init(conn);
204 __connman_manager_init(conn, option_compat);
205 __connman_profile_init(conn);
207 __connman_resolver_init();
208 __connman_rtnl_init();
209 __connman_udev_init();
211 __connman_plugin_init(option_plugin, option_noplugin);
213 __connman_element_start();
215 g_free(option_device);
216 g_free(option_plugin);
217 g_free(option_nodevice);
218 g_free(option_noplugin);
220 memset(&sa, 0, sizeof(sa));
221 sa.sa_handler = sig_term;
222 sigaction(SIGINT, &sa, NULL);
223 sigaction(SIGTERM, &sa, NULL);
225 sa.sa_handler = sig_debug;
226 sigaction(SIGUSR2, &sa, NULL);
228 g_main_loop_run(main_loop);
230 __connman_element_stop();
232 __connman_plugin_cleanup();
234 __connman_udev_cleanup();
235 __connman_rtnl_cleanup();
236 __connman_resolver_cleanup();
238 __connman_profile_cleanup();
239 __connman_manager_cleanup();
240 __connman_agent_cleanup();
242 __connman_element_cleanup();
243 __connman_storage_cleanup();
245 __connman_dbus_cleanup();
248 __connman_log_cleanup();
250 dbus_connection_unref(conn);
252 g_main_loop_unref(main_loop);