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