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