Remove all the useless D-Bus connection passing
[framework/connectivity/connman.git] / src / main.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <signal.h>
31 #include <getopt.h>
32 #include <sys/stat.h>
33 #include <net/if.h>
34
35 #include <gdbus.h>
36
37 #include "connman.h"
38
39 static GMainLoop *main_loop = NULL;
40
41 static void sig_term(int sig)
42 {
43         connman_info("Terminating");
44
45         g_main_loop_quit(main_loop);
46 }
47
48 static void sig_debug(int sig)
49 {
50         __connman_toggle_debug();
51 }
52
53 static void disconnect_callback(DBusConnection *conn, void *user_data)
54 {
55         connman_error("D-Bus disconnect");
56
57         g_main_loop_quit(main_loop);
58 }
59
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;
70
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" },
93         { NULL },
94 };
95
96 const char *connman_option_get_string(const char *key)
97 {
98         if (g_strcmp0(key, "wifi") == 0) {
99                 if (option_wifi == NULL)
100                         return "wext";
101                 else
102                         return option_wifi;
103         }
104
105         return NULL;
106 }
107
108 int main(int argc, char *argv[])
109 {
110         GOptionContext *context;
111         GError *error = NULL;
112         DBusConnection *conn;
113         DBusError err;
114         struct sigaction sa;
115         mode_t old_umask;
116
117 #ifdef NEED_THREADS
118         if (g_thread_supported() == FALSE)
119                 g_thread_init(NULL);
120 #endif
121
122         context = g_option_context_new(NULL);
123         g_option_context_add_main_entries(context, options, NULL);
124
125         if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
126                 if (error != NULL) {
127                         g_printerr("%s\n", error->message);
128                         g_error_free(error);
129                 } else
130                         g_printerr("An unknown error occurred\n");
131                 exit(1);
132         }
133
134         g_option_context_free(context);
135
136         if (option_version == TRUE) {
137                 printf("%s\n", VERSION);
138                 exit(0);
139         }
140
141         if (option_detach == TRUE) {
142                 if (daemon(0, 0)) {
143                         perror("Can't start daemon");
144                         exit(1);
145                 }
146         }
147
148         if (mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
149                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
150                 if (errno != EEXIST)
151                         perror("Failed to create state directory");
152         }
153
154         if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
155                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
156                 if (errno != EEXIST)
157                         perror("Failed to create storage directory");
158         }
159
160         old_umask = umask(077);
161
162         main_loop = g_main_loop_new(NULL, FALSE);
163
164 #ifdef NEED_THREADS
165         if (dbus_threads_init_default() == FALSE) {
166                 fprintf(stderr, "Can't init usage of threads\n");
167                 exit(1);
168         }
169 #endif
170
171         dbus_error_init(&err);
172
173         conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
174         if (conn == NULL) {
175                 if (dbus_error_is_set(&err) == TRUE) {
176                         fprintf(stderr, "%s\n", err.message);
177                         dbus_error_free(&err);
178                 } else
179                         fprintf(stderr, "Can't register with system bus\n");
180                 exit(1);
181         }
182
183         g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
184
185         if (option_compat == TRUE) {
186                 if (g_dbus_request_name(conn, NM_SERVICE, NULL) == FALSE) {
187                         fprintf(stderr, "Can't register compat service\n");
188                         option_compat = FALSE;
189                 }
190         }
191
192         __connman_log_init(option_detach, option_debug);
193
194         if (option_selftest == TRUE) {
195                 if (__connman_selftest() < 0) {
196                         connman_error("Self testing routines failed");
197                         goto selftest;
198                 }
199         }
200
201         __connman_dbus_init(conn);
202
203         __connman_storage_init();
204         __connman_element_init(option_device, option_nodevice);
205
206         __connman_agent_init();
207         __connman_manager_init(option_compat);
208         __connman_profile_init();
209
210         __connman_resolver_init();
211         __connman_ipconfig_init();
212         __connman_rtnl_init();
213         __connman_udev_init();
214         __connman_task_init();
215
216         __connman_plugin_init(option_plugin, option_noplugin);
217
218         __connman_element_start();
219
220         g_free(option_device);
221         g_free(option_plugin);
222         g_free(option_nodevice);
223         g_free(option_noplugin);
224
225         memset(&sa, 0, sizeof(sa));
226         sa.sa_handler = sig_term;
227         sigaction(SIGINT, &sa, NULL);
228         sigaction(SIGTERM, &sa, NULL);
229
230         sa.sa_handler = sig_debug;
231         sigaction(SIGUSR2, &sa, NULL);
232
233         g_main_loop_run(main_loop);
234
235         __connman_element_stop();
236
237         __connman_plugin_cleanup();
238
239         __connman_task_cleanup();
240         __connman_udev_cleanup();
241         __connman_rtnl_cleanup();
242         __connman_ipconfig_cleanup();
243         __connman_resolver_cleanup();
244
245         __connman_profile_cleanup();
246         __connman_manager_cleanup();
247         __connman_agent_cleanup();
248
249         __connman_element_cleanup();
250         __connman_storage_cleanup();
251
252         __connman_dbus_cleanup();
253
254 selftest:
255         __connman_log_cleanup();
256
257         dbus_connection_unref(conn);
258
259         g_main_loop_unref(main_loop);
260
261         return 0;
262 }