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