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