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