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