Remove selftest file and option
[framework/connectivity/connman.git] / src / main.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  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 #ifdef HAVE_CAPNG
38 #include <cap-ng.h>
39 #endif
40
41 #include "connman.h"
42
43 static GMainLoop *main_loop = NULL;
44
45 static void sig_term(int sig)
46 {
47         connman_info("Terminating");
48
49         g_main_loop_quit(main_loop);
50 }
51
52 static void disconnect_callback(DBusConnection *conn, void *user_data)
53 {
54         connman_error("D-Bus disconnect");
55
56         g_main_loop_quit(main_loop);
57 }
58
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_version = FALSE;
68
69 static gboolean parse_debug(const char *key, const char *value,
70                                         gpointer user_data, GError **error)
71 {
72         if (value)
73                 option_debug = g_strdup(value);
74         else
75                 option_debug = g_strdup("*");
76
77         return TRUE;
78 }
79
80 static GOptionEntry options[] = {
81         { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
82                                 G_OPTION_ARG_CALLBACK, parse_debug,
83                                 "Specify debug options to enable", "DEBUG" },
84         { "device", 'i', 0, G_OPTION_ARG_STRING, &option_device,
85                         "Specify networking device or interface", "DEV" },
86         { "nodevice", 'I', 0, G_OPTION_ARG_STRING, &option_nodevice,
87                         "Specify networking interface to ignore", "DEV" },
88         { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
89                                 "Specify plugins to load", "NAME,..." },
90         { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
91                                 "Specify plugins not to load", "NAME,..." },
92         { "wifi", 'W', 0, G_OPTION_ARG_STRING, &option_wifi,
93                                 "Specify driver for WiFi/Supplicant", "NAME" },
94         { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
95                                 G_OPTION_ARG_NONE, &option_detach,
96                                 "Don't fork daemon to background" },
97         { "compat", 'c', 0, G_OPTION_ARG_NONE, &option_compat,
98                                 "Enable Network Manager compatibility" },
99         { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
100                                 "Show version information and exit" },
101         { NULL },
102 };
103
104 const char *connman_option_get_string(const char *key)
105 {
106         if (g_strcmp0(key, "wifi") == 0) {
107                 if (option_wifi == NULL)
108                         return "nl80211,wext";
109                 else
110                         return option_wifi;
111         }
112
113         return NULL;
114 }
115
116 int main(int argc, char *argv[])
117 {
118         GOptionContext *context;
119         GError *error = NULL;
120         DBusConnection *conn;
121         DBusError err;
122         struct sigaction sa;
123         mode_t old_umask;
124
125 #ifdef HAVE_CAPNG
126         /* Drop capabilities */
127 #endif
128
129 #ifdef NEED_THREADS
130         if (g_thread_supported() == FALSE)
131                 g_thread_init(NULL);
132 #endif
133
134         context = g_option_context_new(NULL);
135         g_option_context_add_main_entries(context, options, NULL);
136
137         if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
138                 if (error != NULL) {
139                         g_printerr("%s\n", error->message);
140                         g_error_free(error);
141                 } else
142                         g_printerr("An unknown error occurred\n");
143                 exit(1);
144         }
145
146         g_option_context_free(context);
147
148         if (option_version == TRUE) {
149                 printf("%s\n", VERSION);
150                 exit(0);
151         }
152
153         if (option_detach == TRUE) {
154                 if (daemon(0, 0)) {
155                         perror("Can't start daemon");
156                         exit(1);
157                 }
158         }
159
160         if (mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
161                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
162                 if (errno != EEXIST)
163                         perror("Failed to create state directory");
164         }
165
166         if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
167                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
168                 if (errno != EEXIST)
169                         perror("Failed to create storage directory");
170         }
171
172         if (mkdir(STORAGEDIR "/stats", S_IRUSR | S_IWUSR | S_IXUSR |
173                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
174                 if (errno != EEXIST)
175                         perror("Failed to create statistics directory");
176         }
177
178         old_umask = umask(077);
179
180         main_loop = g_main_loop_new(NULL, FALSE);
181
182 #ifdef NEED_THREADS
183         if (dbus_threads_init_default() == FALSE) {
184                 fprintf(stderr, "Can't init usage of threads\n");
185                 exit(1);
186         }
187 #endif
188
189         dbus_error_init(&err);
190
191         conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
192         if (conn == NULL) {
193                 if (dbus_error_is_set(&err) == TRUE) {
194                         fprintf(stderr, "%s\n", err.message);
195                         dbus_error_free(&err);
196                 } else
197                         fprintf(stderr, "Can't register with system bus\n");
198                 exit(1);
199         }
200
201         g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
202
203         if (option_compat == TRUE) {
204                 if (g_dbus_request_name(conn, NM_SERVICE, NULL) == FALSE) {
205                         fprintf(stderr, "Can't register compat service\n");
206                         option_compat = FALSE;
207                 }
208         }
209
210         __connman_log_init(option_debug, option_detach);
211
212         __connman_dbus_init(conn);
213
214         __connman_storage_init();
215         __connman_element_init(option_device, option_nodevice);
216
217         __connman_agent_init();
218         __connman_iptables_init();
219         __connman_tethering_init();
220         __connman_counter_init();
221         __connman_ondemand_init();
222         __connman_manager_init(option_compat);
223         __connman_profile_init();
224         __connman_config_init();
225         __connman_stats_init();
226
227         __connman_resolver_init();
228         __connman_ipconfig_init();
229         __connman_rtnl_init();
230         __connman_task_init();
231         __connman_proxy_init();
232         __connman_detect_init();
233         __connman_session_init();
234         __connman_timeserver_init();
235
236         __connman_plugin_init(option_plugin, option_noplugin);
237
238         __connman_element_start();
239
240         g_free(option_device);
241         g_free(option_plugin);
242         g_free(option_nodevice);
243         g_free(option_noplugin);
244
245         memset(&sa, 0, sizeof(sa));
246         sa.sa_handler = sig_term;
247         sigaction(SIGINT, &sa, NULL);
248         sigaction(SIGTERM, &sa, NULL);
249
250         g_main_loop_run(main_loop);
251
252         __connman_element_stop();
253
254         __connman_plugin_cleanup();
255
256         __connman_timeserver_cleanup();
257         __connman_session_cleanup();
258         __connman_detect_cleanup();
259         __connman_proxy_cleanup();
260         __connman_task_cleanup();
261         __connman_rtnl_cleanup();
262         __connman_ipconfig_cleanup();
263         __connman_resolver_cleanup();
264
265         __connman_stats_cleanup();
266         __connman_config_cleanup();
267         __connman_profile_cleanup();
268         __connman_manager_cleanup();
269         __connman_ondemand_cleanup();
270         __connman_counter_cleanup();
271         __connman_agent_cleanup();
272         __connman_tethering_cleanup();
273         __connman_iptables_cleanup();
274
275         __connman_element_cleanup();
276         __connman_storage_cleanup();
277
278         __connman_dbus_cleanup();
279
280         __connman_log_cleanup();
281
282         dbus_connection_unref(conn);
283
284         g_main_loop_unref(main_loop);
285
286         return 0;
287 }