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