Add framework for configuration files
[platform/upstream/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 #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 GOptionEntry options[] = {
71         { "debug", 'd', 0, G_OPTION_ARG_STRING, &option_debug,
72                                 "Specify debug options to enable", "DEBUG" },
73         { "device", 'i', 0, G_OPTION_ARG_STRING, &option_device,
74                         "Specify networking device or interface", "DEV" },
75         { "nodevice", 'I', 0, G_OPTION_ARG_STRING, &option_nodevice,
76                         "Specify networking interface to ignore", "DEV" },
77         { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
78                                 "Specify plugins to load", "NAME" },
79         { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
80                                 "Specify plugins not to load", "NAME" },
81         { "wifi", 'W', 0, G_OPTION_ARG_STRING, &option_wifi,
82                                 "Specify driver for WiFi/Supplicant", "NAME" },
83         { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
84                                 G_OPTION_ARG_NONE, &option_detach,
85                                 "Don't fork daemon to background" },
86         { "compat", 'c', 0, G_OPTION_ARG_NONE, &option_compat,
87                                 "Enable Network Manager compatibility" },
88         { "selftest", 't', 0, G_OPTION_ARG_NONE, &option_selftest,
89                                 "Run self testing routines" },
90         { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
91                                 "Show version information and exit" },
92         { NULL },
93 };
94
95 const char *connman_option_get_string(const char *key)
96 {
97         if (g_strcmp0(key, "wifi") == 0) {
98                 if (option_wifi == NULL)
99                         return "wext";
100                 else
101                         return option_wifi;
102         }
103
104         return NULL;
105 }
106
107 int main(int argc, char *argv[])
108 {
109         GOptionContext *context;
110         GError *error = NULL;
111         DBusConnection *conn;
112         DBusError err;
113         struct sigaction sa;
114         mode_t old_umask;
115
116 #ifdef HAVE_CAPNG
117         /* Drop capabilities */
118 #endif
119
120 #ifdef NEED_THREADS
121         if (g_thread_supported() == FALSE)
122                 g_thread_init(NULL);
123 #endif
124
125         context = g_option_context_new(NULL);
126         g_option_context_add_main_entries(context, options, NULL);
127
128         if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
129                 if (error != NULL) {
130                         g_printerr("%s\n", error->message);
131                         g_error_free(error);
132                 } else
133                         g_printerr("An unknown error occurred\n");
134                 exit(1);
135         }
136
137         g_option_context_free(context);
138
139         if (option_version == TRUE) {
140                 printf("%s\n", VERSION);
141                 exit(0);
142         }
143
144         if (option_detach == TRUE) {
145                 if (daemon(0, 0)) {
146                         perror("Can't start daemon");
147                         exit(1);
148                 }
149         }
150
151         if (mkdir(STATEDIR, 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 state directory");
155         }
156
157         if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
158                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
159                 if (errno != EEXIST)
160                         perror("Failed to create storage directory");
161         }
162
163         old_umask = umask(077);
164
165         main_loop = g_main_loop_new(NULL, FALSE);
166
167 #ifdef NEED_THREADS
168         if (dbus_threads_init_default() == FALSE) {
169                 fprintf(stderr, "Can't init usage of threads\n");
170                 exit(1);
171         }
172 #endif
173
174         dbus_error_init(&err);
175
176         conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
177         if (conn == NULL) {
178                 if (dbus_error_is_set(&err) == TRUE) {
179                         fprintf(stderr, "%s\n", err.message);
180                         dbus_error_free(&err);
181                 } else
182                         fprintf(stderr, "Can't register with system bus\n");
183                 exit(1);
184         }
185
186         g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
187
188         if (option_compat == TRUE) {
189                 if (g_dbus_request_name(conn, NM_SERVICE, NULL) == FALSE) {
190                         fprintf(stderr, "Can't register compat service\n");
191                         option_compat = FALSE;
192                 }
193         }
194
195         __connman_log_init(option_debug, option_detach);
196
197         if (option_selftest == TRUE) {
198                 if (__connman_selftest() < 0) {
199                         connman_error("Self testing routines failed");
200                         goto selftest;
201                 }
202         }
203
204         __connman_dbus_init(conn);
205
206         __connman_storage_init();
207         __connman_element_init(option_device, option_nodevice);
208
209         __connman_agent_init();
210         __connman_manager_init(option_compat);
211         __connman_profile_init();
212         __connman_config_init();
213
214         __connman_resolver_init();
215         __connman_ipconfig_init();
216         __connman_rtnl_init();
217         __connman_udev_init();
218         __connman_task_init();
219
220         __connman_plugin_init(option_plugin, option_noplugin);
221
222         __connman_element_start();
223
224         g_free(option_device);
225         g_free(option_plugin);
226         g_free(option_nodevice);
227         g_free(option_noplugin);
228
229         memset(&sa, 0, sizeof(sa));
230         sa.sa_handler = sig_term;
231         sigaction(SIGINT, &sa, NULL);
232         sigaction(SIGTERM, &sa, NULL);
233
234         g_main_loop_run(main_loop);
235
236         __connman_element_stop();
237
238         __connman_plugin_cleanup();
239
240         __connman_task_cleanup();
241         __connman_udev_cleanup();
242         __connman_rtnl_cleanup();
243         __connman_ipconfig_cleanup();
244         __connman_resolver_cleanup();
245
246         __connman_config_cleanup();
247         __connman_profile_cleanup();
248         __connman_manager_cleanup();
249         __connman_agent_cleanup();
250
251         __connman_element_cleanup();
252         __connman_storage_cleanup();
253
254         __connman_dbus_cleanup();
255
256 selftest:
257         __connman_log_cleanup();
258
259         dbus_connection_unref(conn);
260
261         g_main_loop_unref(main_loop);
262
263         return 0;
264 }