Add more detailed tracking of IP devices
[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
116 #ifdef NEED_THREADS
117         if (g_thread_supported() == FALSE)
118                 g_thread_init(NULL);
119 #endif
120
121         context = g_option_context_new(NULL);
122         g_option_context_add_main_entries(context, options, NULL);
123
124         if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
125                 if (error != NULL) {
126                         g_printerr("%s\n", error->message);
127                         g_error_free(error);
128                 } else
129                         g_printerr("An unknown error occurred\n");
130                 exit(1);
131         }
132
133         g_option_context_free(context);
134
135         if (option_version == TRUE) {
136                 printf("%s\n", VERSION);
137                 exit(0);
138         }
139
140         if (option_detach == TRUE) {
141                 if (daemon(0, 0)) {
142                         perror("Can't start daemon");
143                         exit(1);
144                 }
145         }
146
147         if (mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
148                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
149                 if (errno != EEXIST)
150                         perror("Failed to create state directory");
151         }
152
153         if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
154                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
155                 if (errno != EEXIST)
156                         perror("Failed to create storage directory");
157         }
158
159         main_loop = g_main_loop_new(NULL, FALSE);
160
161 #ifdef NEED_THREADS
162         if (dbus_threads_init_default() == FALSE) {
163                 fprintf(stderr, "Can't init usage of threads\n");
164                 exit(1);
165         }
166 #endif
167
168         dbus_error_init(&err);
169
170         conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
171         if (conn == NULL) {
172                 if (dbus_error_is_set(&err) == TRUE) {
173                         fprintf(stderr, "%s\n", err.message);
174                         dbus_error_free(&err);
175                 } else
176                         fprintf(stderr, "Can't register with system bus\n");
177                 exit(1);
178         }
179
180         g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
181
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;
186                 }
187         }
188
189         __connman_log_init(option_detach, option_debug);
190
191         if (option_selftest == TRUE) {
192                 if (__connman_selftest() < 0) {
193                         connman_error("Self testing routines failed");
194                         goto selftest;
195                 }
196         }
197
198         __connman_dbus_init(conn);
199
200         __connman_storage_init();
201         __connman_element_init(conn, option_device, option_nodevice);
202
203         __connman_agent_init(conn);
204         __connman_manager_init(conn, option_compat);
205         __connman_profile_init(conn);
206
207         __connman_resolver_init();
208         __connman_ipconfig_init();
209         __connman_rtnl_init();
210         __connman_udev_init();
211         __connman_task_init();
212
213         __connman_plugin_init(option_plugin, option_noplugin);
214
215         __connman_element_start();
216
217         g_free(option_device);
218         g_free(option_plugin);
219         g_free(option_nodevice);
220         g_free(option_noplugin);
221
222         memset(&sa, 0, sizeof(sa));
223         sa.sa_handler = sig_term;
224         sigaction(SIGINT, &sa, NULL);
225         sigaction(SIGTERM, &sa, NULL);
226
227         sa.sa_handler = sig_debug;
228         sigaction(SIGUSR2, &sa, NULL);
229
230         g_main_loop_run(main_loop);
231
232         __connman_element_stop();
233
234         __connman_plugin_cleanup();
235
236         __connman_task_cleanup();
237         __connman_udev_cleanup();
238         __connman_rtnl_cleanup();
239         __connman_ipconfig_cleanup();
240         __connman_resolver_cleanup();
241
242         __connman_profile_cleanup();
243         __connman_manager_cleanup();
244         __connman_agent_cleanup();
245
246         __connman_element_cleanup();
247         __connman_storage_cleanup();
248
249         __connman_dbus_cleanup();
250
251 selftest:
252         __connman_log_cleanup();
253
254         dbus_connection_unref(conn);
255
256         g_main_loop_unref(main_loop);
257
258         return 0;
259 }