Remove obsolete file.
[connman] / 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         g_main_loop_quit(main_loop);
44 }
45
46 static void sig_debug(int sig)
47 {
48         __connman_toggle_debug();
49 }
50
51 static void disconnect_callback(DBusConnection *conn, void *user_data)
52 {
53         DBG("D-Bus disconnect");
54
55         g_main_loop_quit(main_loop);
56 }
57
58 static gchar *option_device = NULL;
59 static gchar *option_plugin = NULL;
60 static gchar *option_nodevice = NULL;
61 static gchar *option_noplugin = NULL;
62 static gchar *option_wifi = NULL;
63 static gboolean option_detach = TRUE;
64 static gboolean option_compat = FALSE;
65 static gboolean option_debug = FALSE;
66 static gboolean option_selftest = FALSE;
67 static gboolean option_version = FALSE;
68
69 static GOptionEntry options[] = {
70         { "device", 'i', 0, G_OPTION_ARG_STRING, &option_device,
71                         "Specify networking device or interface", "DEV" },
72         { "nodevice", 'I', 0, G_OPTION_ARG_STRING, &option_nodevice,
73                         "Specify networking interface to ignore", "DEV" },
74         { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
75                                 "Specify plugins to load", "NAME" },
76         { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
77                                 "Specify plugins not to load", "NAME" },
78         { "wifi", 'W', 0, G_OPTION_ARG_STRING, &option_wifi,
79                                 "Specify driver for WiFi/Supplicant", "NAME" },
80         { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
81                                 G_OPTION_ARG_NONE, &option_detach,
82                                 "Don't fork daemon to background" },
83         { "compat", 'c', 0, G_OPTION_ARG_NONE, &option_compat,
84                                 "Enable Network Manager compatibility" },
85         { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug,
86                                 "Enable debug information output" },
87         { "selftest", 't', 0, G_OPTION_ARG_NONE, &option_selftest,
88                                 "Run self testing routines" },
89         { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
90                                 "Show version information and exit" },
91         { NULL },
92 };
93
94 const char *connman_option_get_string(const char *key)
95 {
96         if (g_strcmp0(key, "wifi") == 0) {
97                 if (option_wifi == NULL)
98                         return "wext,nl80211";
99                 else
100                         return option_wifi;
101         }
102
103         return NULL;
104 }
105
106 int main(int argc, char *argv[])
107 {
108         GOptionContext *context;
109         GError *error = NULL;
110         DBusConnection *conn;
111         DBusError err;
112         struct sigaction sa;
113
114 #ifdef NEED_THREADS
115         if (g_thread_supported() == FALSE)
116                 g_thread_init(NULL);
117 #endif
118
119         context = g_option_context_new(NULL);
120         g_option_context_add_main_entries(context, options, NULL);
121
122         if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
123                 if (error != NULL) {
124                         g_printerr("%s\n", error->message);
125                         g_error_free(error);
126                 } else
127                         g_printerr("An unknown error occurred\n");
128                 exit(1);
129         }
130
131         g_option_context_free(context);
132
133         if (option_version == TRUE) {
134                 printf("%s\n", VERSION);
135                 exit(0);
136         }
137
138         if (option_detach == TRUE) {
139                 if (daemon(0, 0)) {
140                         perror("Can't start daemon");
141                         exit(1);
142                 }
143         }
144
145         if (mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
146                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
147                 if (errno != EEXIST)
148                         perror("Failed to create state directory");
149         }
150
151         if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
152                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
153                 if (errno != EEXIST)
154                         perror("Failed to create storage directory");
155         }
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_detach, option_debug);
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(conn, option_device, option_nodevice);
200
201         __connman_agent_init(conn);
202         __connman_manager_init(conn, option_compat);
203         __connman_profile_init(conn);
204
205         __connman_resolver_init();
206         __connman_rtnl_init();
207         __connman_udev_init();
208
209         __connman_plugin_init(option_plugin, option_noplugin);
210
211         __connman_element_start();
212
213         g_free(option_device);
214         g_free(option_plugin);
215         g_free(option_nodevice);
216         g_free(option_noplugin);
217
218         memset(&sa, 0, sizeof(sa));
219         sa.sa_handler = sig_term;
220         sigaction(SIGINT, &sa, NULL);
221         sigaction(SIGTERM, &sa, NULL);
222
223         sa.sa_handler = sig_debug;
224         sigaction(SIGUSR2, &sa, NULL);
225
226         g_main_loop_run(main_loop);
227
228         __connman_element_stop();
229
230         __connman_plugin_cleanup();
231
232         __connman_udev_cleanup();
233         __connman_rtnl_cleanup();
234         __connman_resolver_cleanup();
235
236         __connman_profile_cleanup();
237         __connman_manager_cleanup();
238         __connman_agent_cleanup();
239
240         __connman_element_cleanup();
241         __connman_storage_cleanup();
242
243         __connman_dbus_cleanup();
244
245 selftest:
246         __connman_log_cleanup();
247
248         dbus_connection_unref(conn);
249
250         g_main_loop_unref(main_loop);
251
252         rmdir(STORAGEDIR);
253
254         rmdir(STATEDIR);
255
256         return 0;
257 }