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