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 g_main_loop_quit(main_loop);
46 static void disconnect_callback(DBusConnection *conn, void *user_data)
48 DBG("D-Bus disconnect");
50 g_main_loop_quit(main_loop);
53 static gchar *option_device = NULL;
54 static gchar *option_plugin = NULL;
55 static gboolean option_detach = TRUE;
56 static gboolean option_selftest = FALSE;
57 static gboolean option_compat = FALSE;
58 static gboolean option_debug = FALSE;
60 static GOptionEntry options[] = {
61 { "device", 'i', 0, G_OPTION_ARG_STRING, &option_device,
62 "Specify network device/interface", "DEV" },
63 { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
64 "Specify plugins to load", "NAME" },
65 { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
66 G_OPTION_ARG_NONE, &option_detach,
67 "Don't fork daemon to background" },
68 { "selftest", 't', 0, G_OPTION_ARG_NONE, &option_selftest,
69 "Run self testing routines" },
70 { "compat", 'c', 0, G_OPTION_ARG_NONE, &option_compat,
71 "Enable Network Manager compatibility" },
72 { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug,
73 "Enable debug information output" },
77 int main(int argc, char *argv[])
79 GOptionContext *context;
86 if (g_thread_supported() == FALSE)
90 context = g_option_context_new(NULL);
91 g_option_context_add_main_entries(context, options, NULL);
93 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
95 g_printerr("%s\n", error->message);
98 g_printerr("An unknown error occurred\n");
102 g_option_context_free(context);
104 if (option_detach == TRUE) {
106 perror("Can't start daemon");
111 mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
112 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
114 mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
115 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
117 main_loop = g_main_loop_new(NULL, FALSE);
120 if (dbus_threads_init_default() == FALSE) {
121 fprintf(stderr, "Can't init usage of threads\n");
126 dbus_error_init(&err);
128 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
130 if (dbus_error_is_set(&err) == TRUE) {
131 fprintf(stderr, "%s\n", err.message);
132 dbus_error_free(&err);
134 fprintf(stderr, "Can't register with system bus\n");
138 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
140 if (option_compat == TRUE) {
141 if (g_dbus_request_name(conn, NM_SERVICE, NULL) == FALSE) {
142 fprintf(stderr, "Can't register compat service\n");
143 option_compat = FALSE;
147 __connman_log_init(option_detach, option_debug);
149 if (option_selftest == TRUE) {
150 if (__connman_selftest() < 0) {
151 connman_error("Self testing routines failed");
156 __connman_dbus_init(conn);
158 __connman_storage_init();
159 __connman_element_init(conn, option_device);
161 __connman_agent_init(conn);
162 __connman_manager_init(conn, option_compat);
163 __connman_profile_init(conn);
165 __connman_resolver_init();
166 __connman_rtnl_init();
167 __connman_udev_init();
169 __connman_plugin_init(option_plugin);
171 __connman_element_start();
173 g_free(option_device);
174 g_free(option_plugin);
176 memset(&sa, 0, sizeof(sa));
177 sa.sa_handler = sig_term;
178 sigaction(SIGINT, &sa, NULL);
179 sigaction(SIGTERM, &sa, NULL);
181 g_main_loop_run(main_loop);
183 __connman_element_stop();
185 __connman_plugin_cleanup();
187 __connman_udev_cleanup();
188 __connman_rtnl_cleanup();
189 __connman_resolver_cleanup();
191 __connman_profile_cleanup();
192 __connman_manager_cleanup();
193 __connman_agent_cleanup();
195 __connman_element_cleanup();
196 __connman_storage_cleanup();
198 __connman_dbus_cleanup();
201 __connman_log_cleanup();
203 dbus_connection_unref(conn);
205 g_main_loop_unref(main_loop);