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
43 static GMainLoop *main_loop = NULL;
45 static void sig_term(int sig)
47 connman_info("Terminating");
49 g_main_loop_quit(main_loop);
52 static void disconnect_callback(DBusConnection *conn, void *user_data)
54 connman_error("D-Bus disconnect");
56 g_main_loop_quit(main_loop);
59 static gchar *option_debug = NULL;
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_selftest = FALSE;
68 static gboolean option_version = FALSE;
70 static GOptionEntry options[] = {
71 { "debug", 'd', 0, G_OPTION_ARG_STRING, &option_debug,
72 "Specify debug options to enable", "DEBUG" },
73 { "device", 'i', 0, G_OPTION_ARG_STRING, &option_device,
74 "Specify networking device or interface", "DEV" },
75 { "nodevice", 'I', 0, G_OPTION_ARG_STRING, &option_nodevice,
76 "Specify networking interface to ignore", "DEV" },
77 { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
78 "Specify plugins to load", "NAME" },
79 { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
80 "Specify plugins not to load", "NAME" },
81 { "wifi", 'W', 0, G_OPTION_ARG_STRING, &option_wifi,
82 "Specify driver for WiFi/Supplicant", "NAME" },
83 { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
84 G_OPTION_ARG_NONE, &option_detach,
85 "Don't fork daemon to background" },
86 { "compat", 'c', 0, G_OPTION_ARG_NONE, &option_compat,
87 "Enable Network Manager compatibility" },
88 { "selftest", 't', 0, G_OPTION_ARG_NONE, &option_selftest,
89 "Run self testing routines" },
90 { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
91 "Show version information and exit" },
95 const char *connman_option_get_string(const char *key)
97 if (g_strcmp0(key, "wifi") == 0) {
98 if (option_wifi == NULL)
107 int main(int argc, char *argv[])
109 GOptionContext *context;
110 GError *error = NULL;
111 DBusConnection *conn;
117 /* Drop capabilities */
121 if (g_thread_supported() == FALSE)
125 context = g_option_context_new(NULL);
126 g_option_context_add_main_entries(context, options, NULL);
128 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
130 g_printerr("%s\n", error->message);
133 g_printerr("An unknown error occurred\n");
137 g_option_context_free(context);
139 if (option_version == TRUE) {
140 printf("%s\n", VERSION);
144 if (option_detach == TRUE) {
146 perror("Can't start daemon");
151 if (mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
152 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
154 perror("Failed to create state directory");
157 if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
158 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
160 perror("Failed to create storage directory");
163 old_umask = umask(077);
165 main_loop = g_main_loop_new(NULL, FALSE);
168 if (dbus_threads_init_default() == FALSE) {
169 fprintf(stderr, "Can't init usage of threads\n");
174 dbus_error_init(&err);
176 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
178 if (dbus_error_is_set(&err) == TRUE) {
179 fprintf(stderr, "%s\n", err.message);
180 dbus_error_free(&err);
182 fprintf(stderr, "Can't register with system bus\n");
186 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
188 if (option_compat == TRUE) {
189 if (g_dbus_request_name(conn, NM_SERVICE, NULL) == FALSE) {
190 fprintf(stderr, "Can't register compat service\n");
191 option_compat = FALSE;
195 __connman_log_init(option_debug, option_detach);
197 if (option_selftest == TRUE) {
198 if (__connman_selftest() < 0) {
199 connman_error("Self testing routines failed");
204 __connman_dbus_init(conn);
206 __connman_storage_init();
207 __connman_element_init(option_device, option_nodevice);
209 __connman_agent_init();
210 __connman_manager_init(option_compat);
211 __connman_profile_init();
212 __connman_config_init();
214 __connman_resolver_init();
215 __connman_ipconfig_init();
216 __connman_rtnl_init();
217 __connman_udev_init();
218 __connman_task_init();
220 __connman_plugin_init(option_plugin, option_noplugin);
222 __connman_element_start();
224 g_free(option_device);
225 g_free(option_plugin);
226 g_free(option_nodevice);
227 g_free(option_noplugin);
229 memset(&sa, 0, sizeof(sa));
230 sa.sa_handler = sig_term;
231 sigaction(SIGINT, &sa, NULL);
232 sigaction(SIGTERM, &sa, NULL);
234 g_main_loop_run(main_loop);
236 __connman_element_stop();
238 __connman_plugin_cleanup();
240 __connman_task_cleanup();
241 __connman_udev_cleanup();
242 __connman_rtnl_cleanup();
243 __connman_ipconfig_cleanup();
244 __connman_resolver_cleanup();
246 __connman_config_cleanup();
247 __connman_profile_cleanup();
248 __connman_manager_cleanup();
249 __connman_agent_cleanup();
251 __connman_element_cleanup();
252 __connman_storage_cleanup();
254 __connman_dbus_cleanup();
257 __connman_log_cleanup();
259 dbus_connection_unref(conn);
261 g_main_loop_unref(main_loop);