c0e87b3a9689efc5ed02beb1df1f0af64c4554af
[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 #include <netdb.h>
37
38 #include <gdbus.h>
39
40 #ifdef HAVE_CAPNG
41 #include <cap-ng.h>
42 #endif
43
44 #include "connman.h"
45
46 static struct {
47         connman_bool_t bg_scan;
48         char **pref_timeservers;
49         unsigned int *auto_connect;
50         unsigned int *preferred_techs;
51         char **fallback_nameservers;
52 } connman_settings  = {
53         .bg_scan = TRUE,
54         .pref_timeservers = NULL,
55         .auto_connect = NULL,
56         .preferred_techs = NULL,
57         .fallback_nameservers = NULL,
58 };
59
60 static GKeyFile *load_config(const char *file)
61 {
62         GError *err = NULL;
63         GKeyFile *keyfile;
64
65         keyfile = g_key_file_new();
66
67         g_key_file_set_list_separator(keyfile, ',');
68
69         if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
70                 if (err->code != G_FILE_ERROR_NOENT) {
71                         connman_error("Parsing %s failed: %s", file,
72                                                                 err->message);
73                 }
74
75                 g_error_free(err);
76                 g_key_file_free(keyfile);
77                 return NULL;
78         }
79
80         return keyfile;
81 }
82
83 static uint *parse_service_types(char **str_list, gsize len)
84 {
85         unsigned int *type_list;
86         int i, j;
87         enum connman_service_type type;
88
89         type_list = g_try_new0(unsigned int, len + 1);
90         if (type_list == NULL)
91                 return NULL;
92
93         i = 0;
94         j = 0;
95         while (str_list[i] != NULL)
96         {
97                 type = __connman_service_string2type(str_list[i]);
98
99                 if (type != CONNMAN_SERVICE_TYPE_UNKNOWN) {
100                         type_list[j] = type;
101                         j += 1;
102                 }
103                 i += 1;
104         }
105
106         return type_list;
107 }
108
109 static char **parse_fallback_nameservers(char **nameservers, gsize len)
110 {
111         char **servers;
112         int i, j;
113         struct addrinfo hints;
114         struct addrinfo *addr;
115
116         servers = g_try_new0(char *, len + 1);
117         if (servers == NULL)
118                 return NULL;
119
120         i = 0;
121         j = 0;
122         while (nameservers[i] != NULL) {
123                 memset(&hints, 0, sizeof(struct addrinfo));
124                 hints.ai_flags = AI_NUMERICHOST;
125                 addr = NULL;
126                 if (getaddrinfo(nameservers[i], NULL, &hints, &addr) == 0) {
127                         servers[j] = g_strdup(nameservers[i]);
128                         j += 1;
129                 }
130
131                 freeaddrinfo(addr);
132                 i += 1;
133         }
134
135         return servers;
136 }
137
138 static void parse_config(GKeyFile *config)
139 {
140         GError *error = NULL;
141         gboolean boolean;
142         char **timeservers;
143         char **str_list;
144         gsize len;
145         char *default_auto_connect[] = {
146                 "wifi",
147                 "ethernet",
148                 "cellular",
149         };
150
151         if (config == NULL) {
152                 connman_settings.auto_connect =
153                         parse_service_types(default_auto_connect, 3);
154                 return;
155         }
156
157         DBG("parsing main.conf");
158
159         boolean = g_key_file_get_boolean(config, "General",
160                                                 "BackgroundScanning", &error);
161         if (error == NULL)
162                 connman_settings.bg_scan = boolean;
163
164         g_clear_error(&error);
165
166         timeservers = g_key_file_get_string_list(config, "General",
167                                                 "FallbackTimeservers", NULL, &error);
168         if (error == NULL)
169                 connman_settings.pref_timeservers = timeservers;
170
171         g_clear_error(&error);
172
173         str_list = g_key_file_get_string_list(config, "General",
174                         "DefaultAutoConnectTechnologies", &len, &error);
175
176         if (error == NULL)
177                 connman_settings.auto_connect =
178                         parse_service_types(str_list, len);
179         else
180                 connman_settings.auto_connect =
181                         parse_service_types(default_auto_connect, 3);
182
183         g_strfreev(str_list);
184
185         g_clear_error(&error);
186
187         str_list = g_key_file_get_string_list(config, "General",
188                         "PreferredTechnologies", &len, &error);
189
190         if (error == NULL)
191                 connman_settings.preferred_techs =
192                         parse_service_types(str_list, len);
193
194         g_strfreev(str_list);
195
196         g_clear_error(&error);
197
198         str_list = g_key_file_get_string_list(config, "General",
199                         "FallbackNameservers", &len, &error);
200
201         if (error == NULL)
202                 connman_settings.fallback_nameservers =
203                         parse_fallback_nameservers(str_list, len);
204
205         g_strfreev(str_list);
206
207         g_clear_error(&error);
208 }
209
210 static GMainLoop *main_loop = NULL;
211
212 static unsigned int __terminated = 0;
213
214 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
215                                                         gpointer user_data)
216 {
217         struct signalfd_siginfo si;
218         ssize_t result;
219         int fd;
220
221         if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
222                 return FALSE;
223
224         fd = g_io_channel_unix_get_fd(channel);
225
226         result = read(fd, &si, sizeof(si));
227         if (result != sizeof(si))
228                 return FALSE;
229
230         switch (si.ssi_signo) {
231         case SIGINT:
232         case SIGTERM:
233                 if (__terminated == 0) {
234                         connman_info("Terminating");
235                         g_main_loop_quit(main_loop);
236                 }
237
238                 __terminated = 1;
239                 break;
240         }
241
242         return TRUE;
243 }
244
245 static guint setup_signalfd(void)
246 {
247         GIOChannel *channel;
248         guint source;
249         sigset_t mask;
250         int fd;
251
252         sigemptyset(&mask);
253         sigaddset(&mask, SIGINT);
254         sigaddset(&mask, SIGTERM);
255
256         if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
257                 perror("Failed to set signal mask");
258                 return 0;
259         }
260
261         fd = signalfd(-1, &mask, 0);
262         if (fd < 0) {
263                 perror("Failed to create signal descriptor");
264                 return 0;
265         }
266
267         channel = g_io_channel_unix_new(fd);
268
269         g_io_channel_set_close_on_unref(channel, TRUE);
270         g_io_channel_set_encoding(channel, NULL, NULL);
271         g_io_channel_set_buffered(channel, FALSE);
272
273         source = g_io_add_watch(channel,
274                                 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
275                                 signal_handler, NULL);
276
277         g_io_channel_unref(channel);
278
279         return source;
280 }
281
282 static void disconnect_callback(DBusConnection *conn, void *user_data)
283 {
284         connman_error("D-Bus disconnect");
285
286         g_main_loop_quit(main_loop);
287 }
288
289 static gchar *option_debug = NULL;
290 static gchar *option_device = NULL;
291 static gchar *option_plugin = NULL;
292 static gchar *option_nodevice = NULL;
293 static gchar *option_noplugin = NULL;
294 static gchar *option_wifi = NULL;
295 static gboolean option_detach = TRUE;
296 static gboolean option_dnsproxy = TRUE;
297 static gboolean option_compat = FALSE;
298 static gboolean option_version = FALSE;
299
300 static gboolean parse_debug(const char *key, const char *value,
301                                         gpointer user_data, GError **error)
302 {
303         if (value)
304                 option_debug = g_strdup(value);
305         else
306                 option_debug = g_strdup("*");
307
308         return TRUE;
309 }
310
311 static GOptionEntry options[] = {
312         { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
313                                 G_OPTION_ARG_CALLBACK, parse_debug,
314                                 "Specify debug options to enable", "DEBUG" },
315         { "device", 'i', 0, G_OPTION_ARG_STRING, &option_device,
316                         "Specify networking device or interface", "DEV" },
317         { "nodevice", 'I', 0, G_OPTION_ARG_STRING, &option_nodevice,
318                         "Specify networking interface to ignore", "DEV" },
319         { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
320                                 "Specify plugins to load", "NAME,..." },
321         { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
322                                 "Specify plugins not to load", "NAME,..." },
323         { "wifi", 'W', 0, G_OPTION_ARG_STRING, &option_wifi,
324                                 "Specify driver for WiFi/Supplicant", "NAME" },
325         { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
326                                 G_OPTION_ARG_NONE, &option_detach,
327                                 "Don't fork daemon to background" },
328         { "nodnsproxy", 'r', G_OPTION_FLAG_REVERSE,
329                                 G_OPTION_ARG_NONE, &option_dnsproxy,
330                                 "Don't enable DNS Proxy" },
331         { "compat", 'c', 0, G_OPTION_ARG_NONE, &option_compat,
332                                 "(obsolete)" },
333         { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
334                                 "Show version information and exit" },
335         { NULL },
336 };
337
338 const char *connman_option_get_string(const char *key)
339 {
340         if (g_strcmp0(key, "wifi") == 0) {
341                 if (option_wifi == NULL)
342                         return "nl80211,wext";
343                 else
344                         return option_wifi;
345         }
346
347         return NULL;
348 }
349
350 connman_bool_t connman_setting_get_bool(const char *key)
351 {
352         if (g_str_equal(key, "BackgroundScanning") == TRUE)
353                 return connman_settings.bg_scan;
354
355         return FALSE;
356 }
357
358 char **connman_setting_get_string_list(const char *key)
359 {
360         if (g_str_equal(key, "FallbackTimeservers") == TRUE)
361                 return connman_settings.pref_timeservers;
362
363         if (g_str_equal(key, "FallbackNameservers") == TRUE)
364                 return connman_settings.fallback_nameservers;
365
366         return NULL;
367 }
368
369 unsigned int *connman_setting_get_uint_list(const char *key)
370 {
371         if (g_str_equal(key, "DefaultAutoConnectTechnologies") == TRUE)
372                 return connman_settings.auto_connect;
373
374         if (g_str_equal(key, "PreferredTechnologies") == TRUE)
375                 return connman_settings.preferred_techs;
376
377         return NULL;
378 }
379
380 int main(int argc, char *argv[])
381 {
382         GOptionContext *context;
383         GError *error = NULL;
384         DBusConnection *conn;
385         DBusError err;
386         GKeyFile *config;
387         guint signal;
388
389 #ifdef HAVE_CAPNG
390         /* Drop capabilities */
391 #endif
392
393 #ifdef NEED_THREADS
394         if (g_thread_supported() == FALSE)
395                 g_thread_init(NULL);
396 #endif
397
398         context = g_option_context_new(NULL);
399         g_option_context_add_main_entries(context, options, NULL);
400
401         if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
402                 if (error != NULL) {
403                         g_printerr("%s\n", error->message);
404                         g_error_free(error);
405                 } else
406                         g_printerr("An unknown error occurred\n");
407                 exit(1);
408         }
409
410         g_option_context_free(context);
411
412         if (option_version == TRUE) {
413                 printf("%s\n", VERSION);
414                 exit(0);
415         }
416
417         if (option_detach == TRUE) {
418                 if (daemon(0, 0)) {
419                         perror("Can't start daemon");
420                         exit(1);
421                 }
422         }
423
424         if (mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
425                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
426                 if (errno != EEXIST)
427                         perror("Failed to create state directory");
428         }
429
430         if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
431                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
432                 if (errno != EEXIST)
433                         perror("Failed to create storage directory");
434         }
435
436         umask(0077);
437
438         main_loop = g_main_loop_new(NULL, FALSE);
439
440 #ifdef NEED_THREADS
441         if (dbus_threads_init_default() == FALSE) {
442                 fprintf(stderr, "Can't init usage of threads\n");
443                 exit(1);
444         }
445 #endif
446
447         signal = setup_signalfd();
448
449         dbus_error_init(&err);
450
451         conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
452         if (conn == NULL) {
453                 if (dbus_error_is_set(&err) == TRUE) {
454                         fprintf(stderr, "%s\n", err.message);
455                         dbus_error_free(&err);
456                 } else
457                         fprintf(stderr, "Can't register with system bus\n");
458                 exit(1);
459         }
460
461         g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
462
463         __connman_log_init(argv[0], option_debug, option_detach);
464
465         __connman_dbus_init(conn);
466
467         config = load_config(CONFIGDIR "/main.conf");
468         parse_config(config);
469         if (config != NULL)
470                 g_key_file_free(config);
471
472         __connman_storage_migrate();
473         __connman_technology_init();
474         __connman_notifier_init();
475         __connman_service_init();
476         __connman_provider_init();
477         __connman_network_init();
478         __connman_device_init(option_device, option_nodevice);
479
480         __connman_agent_init();
481         __connman_ippool_init();
482         __connman_iptables_init();
483         __connman_nat_init();
484         __connman_tethering_init();
485         __connman_counter_init();
486         __connman_manager_init();
487         __connman_config_init();
488         __connman_stats_init();
489         __connman_clock_init();
490
491         __connman_resolver_init(option_dnsproxy);
492         __connman_ipconfig_init();
493         __connman_rtnl_init();
494         __connman_task_init();
495         __connman_proxy_init();
496         __connman_detect_init();
497         __connman_session_init();
498         __connman_timeserver_init();
499         __connman_connection_init();
500
501         __connman_plugin_init(option_plugin, option_noplugin);
502
503         __connman_rtnl_start();
504         __connman_dhcp_init();
505         __connman_dhcpv6_init();
506         __connman_wpad_init();
507         __connman_wispr_init();
508         __connman_rfkill_init();
509
510         g_free(option_device);
511         g_free(option_plugin);
512         g_free(option_nodevice);
513         g_free(option_noplugin);
514
515         g_main_loop_run(main_loop);
516
517         g_source_remove(signal);
518
519         __connman_rfkill_cleanup();
520         __connman_wispr_cleanup();
521         __connman_wpad_cleanup();
522         __connman_dhcpv6_cleanup();
523         __connman_dhcp_cleanup();
524         __connman_provider_cleanup();
525         __connman_plugin_cleanup();
526         __connman_connection_cleanup();
527         __connman_timeserver_cleanup();
528         __connman_session_cleanup();
529         __connman_detect_cleanup();
530         __connman_proxy_cleanup();
531         __connman_task_cleanup();
532         __connman_rtnl_cleanup();
533         __connman_resolver_cleanup();
534
535         __connman_clock_cleanup();
536         __connman_stats_cleanup();
537         __connman_config_cleanup();
538         __connman_manager_cleanup();
539         __connman_counter_cleanup();
540         __connman_agent_cleanup();
541         __connman_tethering_cleanup();
542         __connman_nat_cleanup();
543         __connman_iptables_cleanup();
544         __connman_ippool_cleanup();
545         __connman_device_cleanup();
546         __connman_network_cleanup();
547         __connman_service_cleanup();
548         __connman_ipconfig_cleanup();
549         __connman_notifier_cleanup();
550         __connman_technology_cleanup();
551
552         __connman_dbus_cleanup();
553
554         __connman_log_cleanup();
555
556         dbus_connection_unref(conn);
557
558         g_main_loop_unref(main_loop);
559
560         if (connman_settings.pref_timeservers != NULL)
561                 g_strfreev(connman_settings.pref_timeservers);
562
563         g_free(connman_settings.auto_connect);
564         g_free(connman_settings.preferred_techs);
565         g_strfreev(connman_settings.fallback_nameservers);
566
567         g_free(option_debug);
568
569         return 0;
570 }