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 disconnect_callback(DBusConnection *conn, void *user_data)
50 connman_error("D-Bus disconnect");
52 g_main_loop_quit(main_loop);
55 static gchar *option_debug = NULL;
56 static gchar *option_device = NULL;
57 static gchar *option_plugin = NULL;
58 static gchar *option_nodevice = NULL;
59 static gchar *option_noplugin = NULL;
60 static gchar *option_wifi = NULL;
61 static gboolean option_detach = TRUE;
62 static gboolean option_compat = FALSE;
63 static gboolean option_selftest = FALSE;
64 static gboolean option_version = FALSE;
66 static GOptionEntry options[] = {
67 { "debug", 'd', 0, G_OPTION_ARG_STRING, &option_debug,
68 "Specify debug options to enable", "DEBUG" },
69 { "device", 'i', 0, G_OPTION_ARG_STRING, &option_device,
70 "Specify networking device or interface", "DEV" },
71 { "nodevice", 'I', 0, G_OPTION_ARG_STRING, &option_nodevice,
72 "Specify networking interface to ignore", "DEV" },
73 { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
74 "Specify plugins to load", "NAME" },
75 { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
76 "Specify plugins not to load", "NAME" },
77 { "wifi", 'W', 0, G_OPTION_ARG_STRING, &option_wifi,
78 "Specify driver for WiFi/Supplicant", "NAME" },
79 { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
80 G_OPTION_ARG_NONE, &option_detach,
81 "Don't fork daemon to background" },
82 { "compat", 'c', 0, G_OPTION_ARG_NONE, &option_compat,
83 "Enable Network Manager compatibility" },
84 { "selftest", 't', 0, G_OPTION_ARG_NONE, &option_selftest,
85 "Run self testing routines" },
86 { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
87 "Show version information and exit" },
91 const char *connman_option_get_string(const char *key)
93 if (g_strcmp0(key, "wifi") == 0) {
94 if (option_wifi == NULL)
103 int main(int argc, char *argv[])
105 GOptionContext *context;
106 GError *error = NULL;
107 DBusConnection *conn;
113 if (g_thread_supported() == FALSE)
117 context = g_option_context_new(NULL);
118 g_option_context_add_main_entries(context, options, NULL);
120 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
122 g_printerr("%s\n", error->message);
125 g_printerr("An unknown error occurred\n");
129 g_option_context_free(context);
131 if (option_version == TRUE) {
132 printf("%s\n", VERSION);
136 if (option_detach == TRUE) {
138 perror("Can't start daemon");
143 if (mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
144 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
146 perror("Failed to create state directory");
149 if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
150 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
152 perror("Failed to create storage directory");
155 old_umask = umask(077);
157 main_loop = g_main_loop_new(NULL, FALSE);
160 if (dbus_threads_init_default() == FALSE) {
161 fprintf(stderr, "Can't init usage of threads\n");
166 dbus_error_init(&err);
168 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
170 if (dbus_error_is_set(&err) == TRUE) {
171 fprintf(stderr, "%s\n", err.message);
172 dbus_error_free(&err);
174 fprintf(stderr, "Can't register with system bus\n");
178 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
180 if (option_compat == TRUE) {
181 if (g_dbus_request_name(conn, NM_SERVICE, NULL) == FALSE) {
182 fprintf(stderr, "Can't register compat service\n");
183 option_compat = FALSE;
187 __connman_log_init(option_debug, option_detach);
189 if (option_selftest == TRUE) {
190 if (__connman_selftest() < 0) {
191 connman_error("Self testing routines failed");
196 __connman_dbus_init(conn);
198 __connman_storage_init();
199 __connman_element_init(option_device, option_nodevice);
201 __connman_agent_init();
202 __connman_manager_init(option_compat);
203 __connman_profile_init();
205 __connman_resolver_init();
206 __connman_ipconfig_init();
207 __connman_rtnl_init();
208 __connman_udev_init();
209 __connman_task_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 g_main_loop_run(main_loop);
227 __connman_element_stop();
229 __connman_plugin_cleanup();
231 __connman_task_cleanup();
232 __connman_udev_cleanup();
233 __connman_rtnl_cleanup();
234 __connman_ipconfig_cleanup();
235 __connman_resolver_cleanup();
237 __connman_profile_cleanup();
238 __connman_manager_cleanup();
239 __connman_agent_cleanup();
241 __connman_element_cleanup();
242 __connman_storage_cleanup();
244 __connman_dbus_cleanup();
247 __connman_log_cleanup();
249 dbus_connection_unref(conn);
251 g_main_loop_unref(main_loop);