Bring back -d option without need for argument
[platform/upstream/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_selftest = FALSE;
68 static gboolean option_version = FALSE;
69
70 static gboolean parse_debug(const char *key, const char *value, 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         { "selftest", 't', 0, G_OPTION_ARG_NONE, &option_selftest,
100                                 "Run self testing routines" },
101         { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
102                                 "Show version information and exit" },
103         { NULL },
104 };
105
106 const char *connman_option_get_string(const char *key)
107 {
108         if (g_strcmp0(key, "wifi") == 0) {
109                 if (option_wifi == NULL)
110                         return "wext";
111                 else
112                         return option_wifi;
113         }
114
115         return NULL;
116 }
117
118 int main(int argc, char *argv[])
119 {
120         GOptionContext *context;
121         GError *error = NULL;
122         DBusConnection *conn;
123         DBusError err;
124         struct sigaction sa;
125         mode_t old_umask;
126
127 #ifdef HAVE_CAPNG
128         /* Drop capabilities */
129 #endif
130
131 #ifdef NEED_THREADS
132         if (g_thread_supported() == FALSE)
133                 g_thread_init(NULL);
134 #endif
135
136         context = g_option_context_new(NULL);
137         g_option_context_add_main_entries(context, options, NULL);
138
139         if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
140                 if (error != NULL) {
141                         g_printerr("%s\n", error->message);
142                         g_error_free(error);
143                 } else
144                         g_printerr("An unknown error occurred\n");
145                 exit(1);
146         }
147
148         g_option_context_free(context);
149
150         if (option_version == TRUE) {
151                 printf("%s\n", VERSION);
152                 exit(0);
153         }
154
155         if (option_detach == TRUE) {
156                 if (daemon(0, 0)) {
157                         perror("Can't start daemon");
158                         exit(1);
159                 }
160         }
161
162         if (mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
163                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
164                 if (errno != EEXIST)
165                         perror("Failed to create state directory");
166         }
167
168         if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
169                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
170                 if (errno != EEXIST)
171                         perror("Failed to create storage directory");
172         }
173
174         old_umask = umask(077);
175
176         main_loop = g_main_loop_new(NULL, FALSE);
177
178 #ifdef NEED_THREADS
179         if (dbus_threads_init_default() == FALSE) {
180                 fprintf(stderr, "Can't init usage of threads\n");
181                 exit(1);
182         }
183 #endif
184
185         dbus_error_init(&err);
186
187         conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
188         if (conn == NULL) {
189                 if (dbus_error_is_set(&err) == TRUE) {
190                         fprintf(stderr, "%s\n", err.message);
191                         dbus_error_free(&err);
192                 } else
193                         fprintf(stderr, "Can't register with system bus\n");
194                 exit(1);
195         }
196
197         g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
198
199         if (option_compat == TRUE) {
200                 if (g_dbus_request_name(conn, NM_SERVICE, NULL) == FALSE) {
201                         fprintf(stderr, "Can't register compat service\n");
202                         option_compat = FALSE;
203                 }
204         }
205
206         __connman_log_init(option_debug, option_detach);
207
208         if (option_selftest == TRUE) {
209                 if (__connman_selftest() < 0) {
210                         connman_error("Self testing routines failed");
211                         goto selftest;
212                 }
213         }
214
215         __connman_dbus_init(conn);
216
217         __connman_storage_init();
218         __connman_element_init(option_device, option_nodevice);
219
220         __connman_agent_init();
221         __connman_counter_init();
222         __connman_manager_init(option_compat);
223         __connman_profile_init();
224         __connman_config_init();
225
226         __connman_resolver_init();
227         __connman_ipconfig_init();
228         __connman_rtnl_init();
229         __connman_udev_init();
230         __connman_task_init();
231         __connman_session_init();
232
233         __connman_plugin_init(option_plugin, option_noplugin);
234
235         __connman_element_start();
236
237         g_free(option_device);
238         g_free(option_plugin);
239         g_free(option_nodevice);
240         g_free(option_noplugin);
241
242         memset(&sa, 0, sizeof(sa));
243         sa.sa_handler = sig_term;
244         sigaction(SIGINT, &sa, NULL);
245         sigaction(SIGTERM, &sa, NULL);
246
247         g_main_loop_run(main_loop);
248
249         __connman_element_stop();
250
251         __connman_plugin_cleanup();
252
253         __connman_session_cleanup();
254         __connman_task_cleanup();
255         __connman_udev_cleanup();
256         __connman_rtnl_cleanup();
257         __connman_ipconfig_cleanup();
258         __connman_resolver_cleanup();
259
260         __connman_config_cleanup();
261         __connman_profile_cleanup();
262         __connman_manager_cleanup();
263         __connman_counter_cleanup();
264         __connman_agent_cleanup();
265
266         __connman_element_cleanup();
267         __connman_storage_cleanup();
268
269         __connman_dbus_cleanup();
270
271 selftest:
272         __connman_log_cleanup();
273
274         dbus_connection_unref(conn);
275
276         g_main_loop_unref(main_loop);
277
278         return 0;
279 }