5da7c861f8a8e26236f5edf718e80a6c445a34ff
[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 disconnect_callback(DBusConnection *conn, void *user_data)
47 {
48         DBG("D-Bus disconnect");
49
50         g_main_loop_quit(main_loop);
51 }
52
53 static gchar *option_device = NULL;
54 static gchar *option_plugin = NULL;
55 static gboolean option_detach = TRUE;
56 static gboolean option_compat = FALSE;
57 static gboolean option_debug = FALSE;
58 static gboolean option_selftest = FALSE;
59 static gboolean option_version = FALSE;
60
61 static GOptionEntry options[] = {
62         { "device", 'i', 0, G_OPTION_ARG_STRING, &option_device,
63                                 "Specify network device/interface", "DEV" },
64         { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
65                                 "Specify plugins to load", "NAME" },
66         { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
67                                 G_OPTION_ARG_NONE, &option_detach,
68                                 "Don't fork daemon to background" },
69         { "compat", 'c', 0, G_OPTION_ARG_NONE, &option_compat,
70                                 "Enable Network Manager compatibility" },
71         { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug,
72                                 "Enable debug information output" },
73         { "selftest", 't', 0, G_OPTION_ARG_NONE, &option_selftest,
74                                 "Run self testing routines" },
75         { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
76                                 "Show version information and exit" },
77         { NULL },
78 };
79
80 int main(int argc, char *argv[])
81 {
82         GOptionContext *context;
83         GError *error = NULL;
84         DBusConnection *conn;
85         DBusError err;
86         struct sigaction sa;
87
88 #ifdef NEED_THREADS
89         if (g_thread_supported() == FALSE)
90                 g_thread_init(NULL);
91 #endif
92
93         context = g_option_context_new(NULL);
94         g_option_context_add_main_entries(context, options, NULL);
95
96         if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
97                 if (error != NULL) {
98                         g_printerr("%s\n", error->message);
99                         g_error_free(error);
100                 } else
101                         g_printerr("An unknown error occurred\n");
102                 exit(1);
103         }
104
105         g_option_context_free(context);
106
107         if (option_version == TRUE) {
108                 printf("%s\n", VERSION);
109                 exit(0);
110         }
111
112         if (option_detach == TRUE) {
113                 if (daemon(0, 0)) {
114                         perror("Can't start daemon");
115                         exit(1);
116                 }
117         }
118
119         mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
120                         S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
121
122         mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
123                         S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
124
125         main_loop = g_main_loop_new(NULL, FALSE);
126
127 #ifdef NEED_THREADS
128         if (dbus_threads_init_default() == FALSE) {
129                 fprintf(stderr, "Can't init usage of threads\n");
130                 exit(1);
131         }
132 #endif
133
134         dbus_error_init(&err);
135
136         conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
137         if (conn == NULL) {
138                 if (dbus_error_is_set(&err) == TRUE) {
139                         fprintf(stderr, "%s\n", err.message);
140                         dbus_error_free(&err);
141                 } else
142                         fprintf(stderr, "Can't register with system bus\n");
143                 exit(1);
144         }
145
146         g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
147
148         if (option_compat == TRUE) {
149                 if (g_dbus_request_name(conn, NM_SERVICE, NULL) == FALSE) {
150                         fprintf(stderr, "Can't register compat service\n");
151                         option_compat = FALSE;
152                 }
153         }
154
155         __connman_log_init(option_detach, option_debug);
156
157         if (option_selftest == TRUE) {
158                 if (__connman_selftest() < 0) {
159                         connman_error("Self testing routines failed");
160                         goto selftest;
161                 }
162         }
163
164         __connman_dbus_init(conn);
165
166         __connman_storage_init();
167         __connman_element_init(conn, option_device);
168
169         __connman_agent_init(conn);
170         __connman_manager_init(conn, option_compat);
171         __connman_profile_init(conn);
172
173         __connman_resolver_init();
174         __connman_rtnl_init();
175         __connman_udev_init();
176
177         __connman_plugin_init(option_plugin);
178
179         __connman_element_start();
180
181         g_free(option_device);
182         g_free(option_plugin);
183
184         memset(&sa, 0, sizeof(sa));
185         sa.sa_handler = sig_term;
186         sigaction(SIGINT, &sa, NULL);
187         sigaction(SIGTERM, &sa, NULL);
188
189         g_main_loop_run(main_loop);
190
191         __connman_element_stop();
192
193         __connman_plugin_cleanup();
194
195         __connman_udev_cleanup();
196         __connman_rtnl_cleanup();
197         __connman_resolver_cleanup();
198
199         __connman_profile_cleanup();
200         __connman_manager_cleanup();
201         __connman_agent_cleanup();
202
203         __connman_element_cleanup();
204         __connman_storage_cleanup();
205
206         __connman_dbus_cleanup();
207
208 selftest:
209         __connman_log_cleanup();
210
211         dbus_connection_unref(conn);
212
213         g_main_loop_unref(main_loop);
214
215         rmdir(STORAGEDIR);
216
217         rmdir(STATEDIR);
218
219         return 0;
220 }