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