Disconnect service on removal if still connected
[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 gboolean option_detach = TRUE;
63 static gboolean option_compat = FALSE;
64 static gboolean option_debug = FALSE;
65 static gboolean option_selftest = FALSE;
66 static gboolean option_version = FALSE;
67
68 static GOptionEntry options[] = {
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         { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
78                                 G_OPTION_ARG_NONE, &option_detach,
79                                 "Don't fork daemon to background" },
80         { "compat", 'c', 0, G_OPTION_ARG_NONE, &option_compat,
81                                 "Enable Network Manager compatibility" },
82         { "debug", 'd', 0, G_OPTION_ARG_NONE, &option_debug,
83                                 "Enable debug information output" },
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 int main(int argc, char *argv[])
92 {
93         GOptionContext *context;
94         GError *error = NULL;
95         DBusConnection *conn;
96         DBusError err;
97         struct sigaction sa;
98
99 #ifdef NEED_THREADS
100         if (g_thread_supported() == FALSE)
101                 g_thread_init(NULL);
102 #endif
103
104         context = g_option_context_new(NULL);
105         g_option_context_add_main_entries(context, options, NULL);
106
107         if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
108                 if (error != NULL) {
109                         g_printerr("%s\n", error->message);
110                         g_error_free(error);
111                 } else
112                         g_printerr("An unknown error occurred\n");
113                 exit(1);
114         }
115
116         g_option_context_free(context);
117
118         if (option_version == TRUE) {
119                 printf("%s\n", VERSION);
120                 exit(0);
121         }
122
123         if (option_detach == TRUE) {
124                 if (daemon(0, 0)) {
125                         perror("Can't start daemon");
126                         exit(1);
127                 }
128         }
129
130         if (mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
131                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
132                 if (errno != EEXIST)
133                         perror("Failed to create state directory");
134         }
135
136         if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
137                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
138                 if (errno != EEXIST)
139                         perror("Failed to create storage directory");
140         }
141
142         main_loop = g_main_loop_new(NULL, FALSE);
143
144 #ifdef NEED_THREADS
145         if (dbus_threads_init_default() == FALSE) {
146                 fprintf(stderr, "Can't init usage of threads\n");
147                 exit(1);
148         }
149 #endif
150
151         dbus_error_init(&err);
152
153         conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
154         if (conn == NULL) {
155                 if (dbus_error_is_set(&err) == TRUE) {
156                         fprintf(stderr, "%s\n", err.message);
157                         dbus_error_free(&err);
158                 } else
159                         fprintf(stderr, "Can't register with system bus\n");
160                 exit(1);
161         }
162
163         g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
164
165         if (option_compat == TRUE) {
166                 if (g_dbus_request_name(conn, NM_SERVICE, NULL) == FALSE) {
167                         fprintf(stderr, "Can't register compat service\n");
168                         option_compat = FALSE;
169                 }
170         }
171
172         __connman_log_init(option_detach, option_debug);
173
174         if (option_selftest == TRUE) {
175                 if (__connman_selftest() < 0) {
176                         connman_error("Self testing routines failed");
177                         goto selftest;
178                 }
179         }
180
181         __connman_dbus_init(conn);
182
183         __connman_storage_init();
184         __connman_element_init(conn, option_device, option_nodevice);
185
186         __connman_agent_init(conn);
187         __connman_manager_init(conn, option_compat);
188         __connman_profile_init(conn);
189
190         __connman_resolver_init();
191         __connman_rtnl_init();
192         __connman_udev_init();
193
194         __connman_plugin_init(option_plugin, option_noplugin);
195
196         __connman_element_start();
197
198         g_free(option_device);
199         g_free(option_plugin);
200         g_free(option_nodevice);
201         g_free(option_noplugin);
202
203         memset(&sa, 0, sizeof(sa));
204         sa.sa_handler = sig_term;
205         sigaction(SIGINT, &sa, NULL);
206         sigaction(SIGTERM, &sa, NULL);
207
208         sa.sa_handler = sig_debug;
209         sigaction(SIGUSR2, &sa, NULL);
210
211         g_main_loop_run(main_loop);
212
213         __connman_element_stop();
214
215         __connman_plugin_cleanup();
216
217         __connman_udev_cleanup();
218         __connman_rtnl_cleanup();
219         __connman_resolver_cleanup();
220
221         __connman_profile_cleanup();
222         __connman_manager_cleanup();
223         __connman_agent_cleanup();
224
225         __connman_element_cleanup();
226         __connman_storage_cleanup();
227
228         __connman_dbus_cleanup();
229
230 selftest:
231         __connman_log_cleanup();
232
233         dbus_connection_unref(conn);
234
235         g_main_loop_unref(main_loop);
236
237         rmdir(STORAGEDIR);
238
239         rmdir(STATEDIR);
240
241         return 0;
242 }