main: Be quiet when config file isn't found
[platform/upstream/connman.git] / src / main.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  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 <errno.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <signal.h>
32 #include <getopt.h>
33 #include <sys/stat.h>
34 #include <net/if.h>
35
36 #include <gdbus.h>
37
38 #ifdef HAVE_CAPNG
39 #include <cap-ng.h>
40 #endif
41
42 #include "connman.h"
43
44 static struct {
45         connman_bool_t bg_scan;
46 } connman_settings  = {
47         .bg_scan = TRUE,
48 };
49
50 static GKeyFile *load_config(const char *file)
51 {
52         GError *err = NULL;
53         GKeyFile *keyfile;
54
55         keyfile = g_key_file_new();
56
57         g_key_file_set_list_separator(keyfile, ',');
58
59         if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
60                 if (err->code != G_FILE_ERROR_NOENT) {
61                         connman_error("Parsing %s failed: %s", file,
62                                                                 err->message);
63                 }
64
65                 g_error_free(err);
66                 g_key_file_free(keyfile);
67                 return NULL;
68         }
69
70         return keyfile;
71 }
72
73 static void parse_config(GKeyFile *config)
74 {
75         GError *error = NULL;
76         gboolean boolean;
77
78         if (config == NULL)
79                 return;
80
81         DBG("parsing main.conf");
82
83         boolean = g_key_file_get_boolean(config, "General",
84                                                 "BackgroundScanning", &error);
85         if (error == NULL)
86                 connman_settings.bg_scan = boolean;
87
88         g_clear_error(&error);
89 }
90
91 static GMainLoop *main_loop = NULL;
92
93 static void sig_term(int sig)
94 {
95         connman_info("Terminating");
96
97         g_main_loop_quit(main_loop);
98 }
99
100 static void disconnect_callback(DBusConnection *conn, void *user_data)
101 {
102         connman_error("D-Bus disconnect");
103
104         g_main_loop_quit(main_loop);
105 }
106
107 static gchar *option_debug = NULL;
108 static gchar *option_device = NULL;
109 static gchar *option_plugin = NULL;
110 static gchar *option_nodevice = NULL;
111 static gchar *option_noplugin = NULL;
112 static gchar *option_wifi = NULL;
113 static gboolean option_detach = TRUE;
114 static gboolean option_dnsproxy = TRUE;
115 static gboolean option_compat = FALSE;
116 static gboolean option_version = FALSE;
117
118 static gboolean parse_debug(const char *key, const char *value,
119                                         gpointer user_data, GError **error)
120 {
121         if (value)
122                 option_debug = g_strdup(value);
123         else
124                 option_debug = g_strdup("*");
125
126         return TRUE;
127 }
128
129 static GOptionEntry options[] = {
130         { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
131                                 G_OPTION_ARG_CALLBACK, parse_debug,
132                                 "Specify debug options to enable", "DEBUG" },
133         { "device", 'i', 0, G_OPTION_ARG_STRING, &option_device,
134                         "Specify networking device or interface", "DEV" },
135         { "nodevice", 'I', 0, G_OPTION_ARG_STRING, &option_nodevice,
136                         "Specify networking interface to ignore", "DEV" },
137         { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
138                                 "Specify plugins to load", "NAME,..." },
139         { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
140                                 "Specify plugins not to load", "NAME,..." },
141         { "wifi", 'W', 0, G_OPTION_ARG_STRING, &option_wifi,
142                                 "Specify driver for WiFi/Supplicant", "NAME" },
143         { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
144                                 G_OPTION_ARG_NONE, &option_detach,
145                                 "Don't fork daemon to background" },
146         { "nodnsproxy", 'r', G_OPTION_FLAG_REVERSE,
147                                 G_OPTION_ARG_NONE, &option_dnsproxy,
148                                 "Don't enable DNS Proxy" },
149         { "compat", 'c', 0, G_OPTION_ARG_NONE, &option_compat,
150                                 "(obsolete)" },
151         { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
152                                 "Show version information and exit" },
153         { NULL },
154 };
155
156 const char *connman_option_get_string(const char *key)
157 {
158         if (g_strcmp0(key, "wifi") == 0) {
159                 if (option_wifi == NULL)
160                         return "nl80211,wext";
161                 else
162                         return option_wifi;
163         }
164
165         return NULL;
166 }
167
168 connman_bool_t connman_setting_get_bool(const char *key)
169 {
170         if (g_str_equal(key, "BackgroundScanning") == TRUE)
171                 return connman_settings.bg_scan;
172
173         return FALSE;
174 }
175
176 int main(int argc, char *argv[])
177 {
178         GOptionContext *context;
179         GError *error = NULL;
180         DBusConnection *conn;
181         DBusError err;
182         struct sigaction sa;
183         GKeyFile *config;
184
185 #ifdef HAVE_CAPNG
186         /* Drop capabilities */
187 #endif
188
189 #ifdef NEED_THREADS
190         if (g_thread_supported() == FALSE)
191                 g_thread_init(NULL);
192 #endif
193
194         context = g_option_context_new(NULL);
195         g_option_context_add_main_entries(context, options, NULL);
196
197         if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
198                 if (error != NULL) {
199                         g_printerr("%s\n", error->message);
200                         g_error_free(error);
201                 } else
202                         g_printerr("An unknown error occurred\n");
203                 exit(1);
204         }
205
206         g_option_context_free(context);
207
208         if (option_version == TRUE) {
209                 printf("%s\n", VERSION);
210                 exit(0);
211         }
212
213         if (option_detach == TRUE) {
214                 if (daemon(0, 0)) {
215                         perror("Can't start daemon");
216                         exit(1);
217                 }
218         }
219
220         if (mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
221                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
222                 if (errno != EEXIST)
223                         perror("Failed to create state directory");
224         }
225
226         if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
227                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
228                 if (errno != EEXIST)
229                         perror("Failed to create storage directory");
230         }
231
232         if (mkdir(STORAGEDIR "/stats", S_IRUSR | S_IWUSR | S_IXUSR |
233                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
234                 if (errno != EEXIST)
235                         perror("Failed to create statistics directory");
236         }
237
238         umask(0077);
239
240         main_loop = g_main_loop_new(NULL, FALSE);
241
242 #ifdef NEED_THREADS
243         if (dbus_threads_init_default() == FALSE) {
244                 fprintf(stderr, "Can't init usage of threads\n");
245                 exit(1);
246         }
247 #endif
248
249         dbus_error_init(&err);
250
251         conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
252         if (conn == NULL) {
253                 if (dbus_error_is_set(&err) == TRUE) {
254                         fprintf(stderr, "%s\n", err.message);
255                         dbus_error_free(&err);
256                 } else
257                         fprintf(stderr, "Can't register with system bus\n");
258                 exit(1);
259         }
260
261         g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
262
263         __connman_log_init(option_debug, option_detach);
264
265         __connman_dbus_init(conn);
266
267         config = load_config(CONFIGDIR "/main.conf");
268
269         parse_config(config);
270
271         __connman_storage_init();
272         __connman_technology_init();
273         __connman_notifier_init();
274         __connman_location_init();
275         __connman_service_init();
276         __connman_provider_init();
277         __connman_network_init();
278         __connman_device_init(option_device, option_nodevice);
279
280         __connman_agent_init();
281         __connman_iptables_init();
282         __connman_tethering_init();
283         __connman_counter_init();
284         __connman_manager_init();
285         __connman_profile_init();
286         __connman_config_init();
287         __connman_stats_init();
288         __connman_clock_init();
289
290         __connman_resolver_init(option_dnsproxy);
291         __connman_ipconfig_init();
292         __connman_rtnl_init();
293         __connman_task_init();
294         __connman_proxy_init();
295         __connman_detect_init();
296         __connman_session_init();
297         __connman_timeserver_init();
298         __connman_connection_init();
299
300         __connman_plugin_init(option_plugin, option_noplugin);
301
302         __connman_storage_init_profile();
303
304         __connman_rtnl_start();
305         __connman_dhcp_init();
306         __connman_wpad_init();
307         __connman_wispr_init();
308         __connman_rfkill_init();
309
310         g_free(option_device);
311         g_free(option_plugin);
312         g_free(option_nodevice);
313         g_free(option_noplugin);
314
315         memset(&sa, 0, sizeof(sa));
316         sa.sa_handler = sig_term;
317         sigaction(SIGINT, &sa, NULL);
318         sigaction(SIGTERM, &sa, NULL);
319
320         g_main_loop_run(main_loop);
321
322         __connman_rfkill_cleanup();
323         __connman_wispr_cleanup();
324         __connman_wpad_cleanup();
325         __connman_dhcp_cleanup();
326         __connman_provider_cleanup();
327         __connman_plugin_cleanup();
328         __connman_connection_cleanup();
329         __connman_timeserver_cleanup();
330         __connman_session_cleanup();
331         __connman_detect_cleanup();
332         __connman_proxy_cleanup();
333         __connman_task_cleanup();
334         __connman_rtnl_cleanup();
335         __connman_ipconfig_cleanup();
336         __connman_resolver_cleanup();
337
338         __connman_clock_cleanup();
339         __connman_stats_cleanup();
340         __connman_config_cleanup();
341         __connman_profile_cleanup();
342         __connman_manager_cleanup();
343         __connman_counter_cleanup();
344         __connman_agent_cleanup();
345         __connman_tethering_cleanup();
346         __connman_iptables_cleanup();
347         __connman_device_cleanup();
348         __connman_network_cleanup();
349         __connman_service_cleanup();
350         __connman_location_cleanup();
351         __connman_notifier_cleanup();
352         __connman_technology_cleanup();
353         __connman_storage_cleanup();
354
355         __connman_dbus_cleanup();
356
357         __connman_log_cleanup();
358
359         dbus_connection_unref(conn);
360
361         g_main_loop_unref(main_loop);
362
363         if (config)
364                 g_key_file_free(config);
365
366         return 0;
367 }