Add support for dynamic debug framework
[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 disconnect_callback(DBusConnection *conn, void *user_data)
49 {
50         connman_error("D-Bus disconnect");
51
52         g_main_loop_quit(main_loop);
53 }
54
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;
65
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" },
88         { NULL },
89 };
90
91 const char *connman_option_get_string(const char *key)
92 {
93         if (g_strcmp0(key, "wifi") == 0) {
94                 if (option_wifi == NULL)
95                         return "wext";
96                 else
97                         return option_wifi;
98         }
99
100         return NULL;
101 }
102
103 int main(int argc, char *argv[])
104 {
105         GOptionContext *context;
106         GError *error = NULL;
107         DBusConnection *conn;
108         DBusError err;
109         struct sigaction sa;
110         mode_t old_umask;
111
112 #ifdef NEED_THREADS
113         if (g_thread_supported() == FALSE)
114                 g_thread_init(NULL);
115 #endif
116
117         context = g_option_context_new(NULL);
118         g_option_context_add_main_entries(context, options, NULL);
119
120         if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
121                 if (error != NULL) {
122                         g_printerr("%s\n", error->message);
123                         g_error_free(error);
124                 } else
125                         g_printerr("An unknown error occurred\n");
126                 exit(1);
127         }
128
129         g_option_context_free(context);
130
131         if (option_version == TRUE) {
132                 printf("%s\n", VERSION);
133                 exit(0);
134         }
135
136         if (option_detach == TRUE) {
137                 if (daemon(0, 0)) {
138                         perror("Can't start daemon");
139                         exit(1);
140                 }
141         }
142
143         if (mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
144                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
145                 if (errno != EEXIST)
146                         perror("Failed to create state directory");
147         }
148
149         if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
150                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
151                 if (errno != EEXIST)
152                         perror("Failed to create storage directory");
153         }
154
155         old_umask = umask(077);
156
157         main_loop = g_main_loop_new(NULL, FALSE);
158
159 #ifdef NEED_THREADS
160         if (dbus_threads_init_default() == FALSE) {
161                 fprintf(stderr, "Can't init usage of threads\n");
162                 exit(1);
163         }
164 #endif
165
166         dbus_error_init(&err);
167
168         conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
169         if (conn == NULL) {
170                 if (dbus_error_is_set(&err) == TRUE) {
171                         fprintf(stderr, "%s\n", err.message);
172                         dbus_error_free(&err);
173                 } else
174                         fprintf(stderr, "Can't register with system bus\n");
175                 exit(1);
176         }
177
178         g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
179
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;
184                 }
185         }
186
187         __connman_log_init(option_debug, option_detach);
188
189         if (option_selftest == TRUE) {
190                 if (__connman_selftest() < 0) {
191                         connman_error("Self testing routines failed");
192                         goto selftest;
193                 }
194         }
195
196         __connman_dbus_init(conn);
197
198         __connman_storage_init();
199         __connman_element_init(option_device, option_nodevice);
200
201         __connman_agent_init();
202         __connman_manager_init(option_compat);
203         __connman_profile_init();
204
205         __connman_resolver_init();
206         __connman_ipconfig_init();
207         __connman_rtnl_init();
208         __connman_udev_init();
209         __connman_task_init();
210
211         __connman_plugin_init(option_plugin, option_noplugin);
212
213         __connman_element_start();
214
215         g_free(option_device);
216         g_free(option_plugin);
217         g_free(option_nodevice);
218         g_free(option_noplugin);
219
220         memset(&sa, 0, sizeof(sa));
221         sa.sa_handler = sig_term;
222         sigaction(SIGINT, &sa, NULL);
223         sigaction(SIGTERM, &sa, NULL);
224
225         g_main_loop_run(main_loop);
226
227         __connman_element_stop();
228
229         __connman_plugin_cleanup();
230
231         __connman_task_cleanup();
232         __connman_udev_cleanup();
233         __connman_rtnl_cleanup();
234         __connman_ipconfig_cleanup();
235         __connman_resolver_cleanup();
236
237         __connman_profile_cleanup();
238         __connman_manager_cleanup();
239         __connman_agent_cleanup();
240
241         __connman_element_cleanup();
242         __connman_storage_cleanup();
243
244         __connman_dbus_cleanup();
245
246 selftest:
247         __connman_log_cleanup();
248
249         dbus_connection_unref(conn);
250
251         g_main_loop_unref(main_loop);
252
253         return 0;
254 }