133acd27991921c530c5bb14364c278e6798dca6
[platform/upstream/connman.git] / vpn / main.c
1 /*
2  *
3  *  ConnMan VPN daemon
4  *
5  *  Copyright (C) 2012-2013  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 "../src/connman.h"
41 #include "vpn.h"
42
43 #include "connman/vpn-dbus.h"
44
45 #define CONFIGMAINFILE CONFIGDIR "/connman-vpn.conf"
46
47 static GMainLoop *main_loop = NULL;
48
49 static unsigned int __terminated = 0;
50
51 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
52                                                         gpointer user_data)
53 {
54         struct signalfd_siginfo si;
55         ssize_t result;
56         int fd;
57
58         if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
59                 return FALSE;
60
61         fd = g_io_channel_unix_get_fd(channel);
62
63         result = read(fd, &si, sizeof(si));
64         if (result != sizeof(si))
65                 return FALSE;
66
67         switch (si.ssi_signo) {
68         case SIGINT:
69         case SIGTERM:
70                 if (__terminated == 0) {
71                         connman_info("Terminating");
72                         g_main_loop_quit(main_loop);
73                 }
74
75                 __terminated = 1;
76                 break;
77         }
78
79         return TRUE;
80 }
81
82 static guint setup_signalfd(void)
83 {
84         GIOChannel *channel;
85         guint source;
86         sigset_t mask;
87         int fd;
88
89         sigemptyset(&mask);
90         sigaddset(&mask, SIGINT);
91         sigaddset(&mask, SIGTERM);
92
93         if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
94                 perror("Failed to set signal mask");
95                 return 0;
96         }
97
98         fd = signalfd(-1, &mask, 0);
99         if (fd < 0) {
100                 perror("Failed to create signal descriptor");
101                 return 0;
102         }
103
104         channel = g_io_channel_unix_new(fd);
105
106         g_io_channel_set_close_on_unref(channel, TRUE);
107         g_io_channel_set_encoding(channel, NULL, NULL);
108         g_io_channel_set_buffered(channel, FALSE);
109
110         source = g_io_add_watch(channel,
111                                 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
112                                 signal_handler, NULL);
113
114         g_io_channel_unref(channel);
115
116         return source;
117 }
118
119 static void disconnect_callback(DBusConnection *conn, void *user_data)
120 {
121         connman_error("D-Bus disconnect");
122
123         g_main_loop_quit(main_loop);
124 }
125
126 static gchar *option_config = NULL;
127 static gchar *option_debug = NULL;
128 static gchar *option_plugin = NULL;
129 static gchar *option_noplugin = NULL;
130 static bool option_detach = true;
131 static bool option_version = false;
132 static bool option_routes = false;
133
134 static bool parse_debug(const char *key, const char *value,
135                                         gpointer user_data, GError **error)
136 {
137         if (value)
138                 option_debug = g_strdup(value);
139         else
140                 option_debug = g_strdup("*");
141
142         return true;
143 }
144
145 static GOptionEntry options[] = {
146         { "config", 'c', 0, G_OPTION_ARG_STRING, &option_config,
147                                 "Load the specified configuration file "
148                                 "instead of " CONFIGMAINFILE, "FILE" },
149         { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
150                                 G_OPTION_ARG_CALLBACK, parse_debug,
151                                 "Specify debug options to enable", "DEBUG" },
152         { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
153                                 "Specify plugins to load", "NAME,..." },
154         { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
155                                 "Specify plugins not to load", "NAME,..." },
156         { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
157                                 G_OPTION_ARG_NONE, &option_detach,
158                                 "Don't fork daemon to background" },
159         { "routes", 'r', 0, G_OPTION_ARG_NONE, &option_routes,
160                                 "Create/delete VPN routes" },
161         { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
162                                 "Show version information and exit" },
163         { NULL },
164 };
165
166 /*
167  * This function will be called from generic src/agent.c code so we have
168  * to use connman_ prefix instead of vpn_ one.
169  */
170 unsigned int connman_timeout_input_request(void)
171 {
172         return __vpn_settings_get_timeout_inputreq();
173 }
174
175 int main(int argc, char *argv[])
176 {
177         GOptionContext *context;
178         GError *error = NULL;
179         DBusConnection *conn;
180         DBusError err;
181         guint signal;
182
183         context = g_option_context_new(NULL);
184         g_option_context_add_main_entries(context, options, NULL);
185
186         if (!g_option_context_parse(context, &argc, &argv, &error)) {
187                 if (error) {
188                         g_printerr("%s\n", error->message);
189                         g_error_free(error);
190                 } else
191                         g_printerr("An unknown error occurred\n");
192                 exit(1);
193         }
194
195         g_option_context_free(context);
196
197         if (option_version) {
198                 printf("%s\n", VERSION);
199                 exit(0);
200         }
201
202         if (option_detach) {
203                 if (daemon(0, 0)) {
204                         perror("Can't start daemon");
205                         exit(1);
206                 }
207         }
208
209         if (mkdir(VPN_STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
210                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
211                 if (errno != EEXIST)
212                         perror("Failed to create state directory");
213         }
214
215         /*
216          * At some point the VPN stuff is migrated into VPN_STORAGEDIR
217          * and this mkdir() call can be removed.
218          */
219         if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
220                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
221                 if (errno != EEXIST)
222                         perror("Failed to create storage directory");
223         }
224
225         if (mkdir(VPN_STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
226                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
227                 if (errno != EEXIST)
228                         perror("Failed to create VPN storage directory");
229         }
230
231         umask(0077);
232
233         main_loop = g_main_loop_new(NULL, FALSE);
234
235         signal = setup_signalfd();
236
237         dbus_error_init(&err);
238
239         conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, VPN_SERVICE, &err);
240         if (!conn) {
241                 if (dbus_error_is_set(&err)) {
242                         fprintf(stderr, "%s\n", err.message);
243                         dbus_error_free(&err);
244                 } else
245                         fprintf(stderr, "Can't register with system bus\n");
246                 exit(1);
247         }
248
249         g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
250
251         __connman_log_init(argv[0], option_debug, option_detach, false,
252                         "Connection Manager VPN daemon", VERSION);
253         __connman_dbus_init(conn);
254
255         if (!option_config)
256                 __vpn_settings_init(CONFIGMAINFILE);
257         else
258                 __vpn_settings_init(option_config);
259
260         __connman_inotify_init();
261         __connman_agent_init();
262         __vpn_provider_init(option_routes);
263         __vpn_manager_init();
264         __vpn_ipconfig_init();
265         __vpn_rtnl_init();
266         __connman_task_init();
267         __connman_plugin_init(option_plugin, option_noplugin);
268         __vpn_config_init();
269
270         __vpn_rtnl_start();
271
272         g_free(option_plugin);
273         g_free(option_noplugin);
274
275         g_main_loop_run(main_loop);
276
277         g_source_remove(signal);
278
279         __vpn_config_cleanup();
280         __connman_plugin_cleanup();
281         __connman_task_cleanup();
282         __vpn_rtnl_cleanup();
283         __vpn_ipconfig_cleanup();
284         __vpn_manager_cleanup();
285         __vpn_provider_cleanup();
286         __connman_agent_cleanup();
287         __connman_inotify_cleanup();
288         __connman_dbus_cleanup();
289         __connman_log_cleanup(false);
290         __vpn_settings_cleanup();
291
292         dbus_connection_unref(conn);
293
294         g_main_loop_unref(main_loop);
295
296         g_free(option_debug);
297
298         return 0;
299 }