main: Use signalfd instead of plain signals
[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 } connman_settings  = {
48         .bg_scan = TRUE,
49 };
50
51 static GKeyFile *load_config(const char *file)
52 {
53         GError *err = NULL;
54         GKeyFile *keyfile;
55
56         keyfile = g_key_file_new();
57
58         g_key_file_set_list_separator(keyfile, ',');
59
60         if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
61                 if (err->code != G_FILE_ERROR_NOENT) {
62                         connman_error("Parsing %s failed: %s", file,
63                                                                 err->message);
64                 }
65
66                 g_error_free(err);
67                 g_key_file_free(keyfile);
68                 return NULL;
69         }
70
71         return keyfile;
72 }
73
74 static void parse_config(GKeyFile *config)
75 {
76         GError *error = NULL;
77         gboolean boolean;
78
79         if (config == NULL)
80                 return;
81
82         DBG("parsing main.conf");
83
84         boolean = g_key_file_get_boolean(config, "General",
85                                                 "BackgroundScanning", &error);
86         if (error == NULL)
87                 connman_settings.bg_scan = boolean;
88
89         g_clear_error(&error);
90 }
91
92 static GMainLoop *main_loop = NULL;
93
94 static gboolean signal_cb(GIOChannel *chan,
95                                 GIOCondition cond, gpointer data)
96 {
97         int signal_fd = GPOINTER_TO_INT(data);
98         struct signalfd_siginfo si;
99         ssize_t res;
100
101         if (cond & (G_IO_NVAL | G_IO_ERR))
102                 return FALSE;
103
104         res = read(signal_fd, &si, sizeof(si));
105         if (res != sizeof(si))
106                 return FALSE;
107
108         switch (si.ssi_signo) {
109         case SIGINT:
110         case SIGTERM:
111                 connman_info("Terminating");
112                 g_main_loop_quit(main_loop);
113                 break;
114         default:
115                 break;
116         }
117
118         return TRUE;
119 }
120
121 static void disconnect_callback(DBusConnection *conn, void *user_data)
122 {
123         connman_error("D-Bus disconnect");
124
125         g_main_loop_quit(main_loop);
126 }
127
128 static gchar *option_debug = NULL;
129 static gchar *option_device = NULL;
130 static gchar *option_plugin = NULL;
131 static gchar *option_nodevice = NULL;
132 static gchar *option_noplugin = NULL;
133 static gchar *option_wifi = NULL;
134 static gboolean option_detach = TRUE;
135 static gboolean option_dnsproxy = TRUE;
136 static gboolean option_compat = FALSE;
137 static gboolean option_version = FALSE;
138
139 static gboolean parse_debug(const char *key, const char *value,
140                                         gpointer user_data, GError **error)
141 {
142         if (value)
143                 option_debug = g_strdup(value);
144         else
145                 option_debug = g_strdup("*");
146
147         return TRUE;
148 }
149
150 static GOptionEntry options[] = {
151         { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
152                                 G_OPTION_ARG_CALLBACK, parse_debug,
153                                 "Specify debug options to enable", "DEBUG" },
154         { "device", 'i', 0, G_OPTION_ARG_STRING, &option_device,
155                         "Specify networking device or interface", "DEV" },
156         { "nodevice", 'I', 0, G_OPTION_ARG_STRING, &option_nodevice,
157                         "Specify networking interface to ignore", "DEV" },
158         { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
159                                 "Specify plugins to load", "NAME,..." },
160         { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
161                                 "Specify plugins not to load", "NAME,..." },
162         { "wifi", 'W', 0, G_OPTION_ARG_STRING, &option_wifi,
163                                 "Specify driver for WiFi/Supplicant", "NAME" },
164         { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
165                                 G_OPTION_ARG_NONE, &option_detach,
166                                 "Don't fork daemon to background" },
167         { "nodnsproxy", 'r', G_OPTION_FLAG_REVERSE,
168                                 G_OPTION_ARG_NONE, &option_dnsproxy,
169                                 "Don't enable DNS Proxy" },
170         { "compat", 'c', 0, G_OPTION_ARG_NONE, &option_compat,
171                                 "(obsolete)" },
172         { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
173                                 "Show version information and exit" },
174         { NULL },
175 };
176
177 const char *connman_option_get_string(const char *key)
178 {
179         if (g_strcmp0(key, "wifi") == 0) {
180                 if (option_wifi == NULL)
181                         return "nl80211,wext";
182                 else
183                         return option_wifi;
184         }
185
186         return NULL;
187 }
188
189 connman_bool_t connman_setting_get_bool(const char *key)
190 {
191         if (g_str_equal(key, "BackgroundScanning") == TRUE)
192                 return connman_settings.bg_scan;
193
194         return FALSE;
195 }
196
197 int main(int argc, char *argv[])
198 {
199         GOptionContext *context;
200         GError *error = NULL;
201         DBusConnection *conn;
202         DBusError err;
203         int signal_fd;
204         int signal_source;
205         GIOChannel *signal_io;
206         sigset_t mask;
207         GKeyFile *config;
208
209 #ifdef HAVE_CAPNG
210         /* Drop capabilities */
211 #endif
212
213 #ifdef NEED_THREADS
214         if (g_thread_supported() == FALSE)
215                 g_thread_init(NULL);
216 #endif
217
218         context = g_option_context_new(NULL);
219         g_option_context_add_main_entries(context, options, NULL);
220
221         if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
222                 if (error != NULL) {
223                         g_printerr("%s\n", error->message);
224                         g_error_free(error);
225                 } else
226                         g_printerr("An unknown error occurred\n");
227                 exit(1);
228         }
229
230         g_option_context_free(context);
231
232         if (option_version == TRUE) {
233                 printf("%s\n", VERSION);
234                 exit(0);
235         }
236
237         if (option_detach == TRUE) {
238                 if (daemon(0, 0)) {
239                         perror("Can't start daemon");
240                         exit(1);
241                 }
242         }
243
244         if (mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
245                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
246                 if (errno != EEXIST)
247                         perror("Failed to create state directory");
248         }
249
250         if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
251                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
252                 if (errno != EEXIST)
253                         perror("Failed to create storage directory");
254         }
255
256         if (mkdir(STORAGEDIR "/stats", S_IRUSR | S_IWUSR | S_IXUSR |
257                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
258                 if (errno != EEXIST)
259                         perror("Failed to create statistics directory");
260         }
261
262         umask(0077);
263
264         main_loop = g_main_loop_new(NULL, FALSE);
265
266 #ifdef NEED_THREADS
267         if (dbus_threads_init_default() == FALSE) {
268                 fprintf(stderr, "Can't init usage of threads\n");
269                 exit(1);
270         }
271 #endif
272
273         dbus_error_init(&err);
274
275         conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
276         if (conn == NULL) {
277                 if (dbus_error_is_set(&err) == TRUE) {
278                         fprintf(stderr, "%s\n", err.message);
279                         dbus_error_free(&err);
280                 } else
281                         fprintf(stderr, "Can't register with system bus\n");
282                 exit(1);
283         }
284
285         g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
286
287         __connman_log_init(option_debug, option_detach);
288
289         __connman_dbus_init(conn);
290
291         config = load_config(CONFIGDIR "/main.conf");
292
293         parse_config(config);
294
295         __connman_storage_init();
296         __connman_technology_init();
297         __connman_notifier_init();
298         __connman_location_init();
299         __connman_service_init();
300         __connman_provider_init();
301         __connman_network_init();
302         __connman_device_init(option_device, option_nodevice);
303
304         __connman_agent_init();
305         __connman_iptables_init();
306         __connman_tethering_init();
307         __connman_counter_init();
308         __connman_manager_init();
309         __connman_profile_init();
310         __connman_config_init();
311         __connman_stats_init();
312         __connman_clock_init();
313
314         __connman_resolver_init(option_dnsproxy);
315         __connman_ipconfig_init();
316         __connman_rtnl_init();
317         __connman_task_init();
318         __connman_proxy_init();
319         __connman_detect_init();
320         __connman_session_init();
321         __connman_timeserver_init();
322         __connman_connection_init();
323
324         __connman_plugin_init(option_plugin, option_noplugin);
325
326         __connman_storage_init_profile();
327
328         __connman_rtnl_start();
329         __connman_dhcp_init();
330         __connman_wpad_init();
331         __connman_wispr_init();
332         __connman_rfkill_init();
333
334         g_free(option_device);
335         g_free(option_plugin);
336         g_free(option_nodevice);
337         g_free(option_noplugin);
338
339         sigemptyset(&mask);
340         sigaddset(&mask, SIGTERM);
341         sigaddset(&mask, SIGINT);
342
343         if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
344                 fprintf(stderr, "Could not block signals\n");
345                 exit(1);
346         }
347
348         signal_fd = signalfd(-1, &mask, 0);
349         if (signal_fd < 0) {
350                 fprintf(stderr, "Could not init signalfd\n");
351                 exit(1);
352         }
353
354         signal_io = g_io_channel_unix_new(signal_fd);
355         g_io_channel_set_close_on_unref(signal_io, TRUE);
356         signal_source = g_io_add_watch(signal_io,
357                                 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
358                                 signal_cb, GINT_TO_POINTER(signal_fd));
359         g_io_channel_unref(signal_io);
360
361         g_main_loop_run(main_loop);
362
363         g_source_remove(signal_source);
364
365         __connman_rfkill_cleanup();
366         __connman_wispr_cleanup();
367         __connman_wpad_cleanup();
368         __connman_dhcp_cleanup();
369         __connman_provider_cleanup();
370         __connman_plugin_cleanup();
371         __connman_connection_cleanup();
372         __connman_timeserver_cleanup();
373         __connman_session_cleanup();
374         __connman_detect_cleanup();
375         __connman_proxy_cleanup();
376         __connman_task_cleanup();
377         __connman_rtnl_cleanup();
378         __connman_ipconfig_cleanup();
379         __connman_resolver_cleanup();
380
381         __connman_clock_cleanup();
382         __connman_stats_cleanup();
383         __connman_config_cleanup();
384         __connman_profile_cleanup();
385         __connman_manager_cleanup();
386         __connman_counter_cleanup();
387         __connman_agent_cleanup();
388         __connman_tethering_cleanup();
389         __connman_iptables_cleanup();
390         __connman_device_cleanup();
391         __connman_network_cleanup();
392         __connman_service_cleanup();
393         __connman_location_cleanup();
394         __connman_notifier_cleanup();
395         __connman_technology_cleanup();
396         __connman_storage_cleanup();
397
398         __connman_dbus_cleanup();
399
400         __connman_log_cleanup();
401
402         dbus_connection_unref(conn);
403
404         g_main_loop_unref(main_loop);
405
406         if (config)
407                 g_key_file_free(config);
408
409         return 0;
410 }