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