5 * Copyright (C) 2012-2013 Intel Corporation. All rights reserved.
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.
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.
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
32 #include <sys/signalfd.h>
40 #include "../src/connman.h"
43 #include "connman/vpn-dbus.h"
45 #define CONFIGMAINFILE CONFIGDIR "/connman-vpn.conf"
47 #define DEFAULT_INPUT_REQUEST_TIMEOUT 300 * 1000
48 #define DEFAULT_BROWSER_LAUNCH_TIMEOUT 300 * 1000
50 static GMainLoop *main_loop = NULL;
52 static unsigned int __terminated = 0;
55 unsigned int timeout_inputreq;
56 unsigned int timeout_browserlaunch;
57 } connman_vpn_settings = {
58 .timeout_inputreq = DEFAULT_INPUT_REQUEST_TIMEOUT,
59 .timeout_browserlaunch = DEFAULT_BROWSER_LAUNCH_TIMEOUT,
62 static GKeyFile *load_config(const char *file)
67 keyfile = g_key_file_new();
69 g_key_file_set_list_separator(keyfile, ',');
71 if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
72 if (err->code != G_FILE_ERROR_NOENT) {
73 connman_error("Parsing %s failed: %s", file,
78 g_key_file_free(keyfile);
85 static void parse_config(GKeyFile *config, const char *file)
93 DBG("parsing %s", file);
95 timeout = g_key_file_get_integer(config, "General",
96 "InputRequestTimeout", &error);
97 if (!error && timeout >= 0)
98 connman_vpn_settings.timeout_inputreq = timeout * 1000;
100 g_clear_error(&error);
103 static int config_init(const char *file)
107 config = load_config(file);
108 parse_config(config, file);
110 g_key_file_free(config);
115 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
118 struct signalfd_siginfo si;
122 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
125 fd = g_io_channel_unix_get_fd(channel);
127 result = read(fd, &si, sizeof(si));
128 if (result != sizeof(si))
131 switch (si.ssi_signo) {
134 if (__terminated == 0) {
135 connman_info("Terminating");
136 g_main_loop_quit(main_loop);
146 static guint setup_signalfd(void)
154 sigaddset(&mask, SIGINT);
155 sigaddset(&mask, SIGTERM);
157 if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
158 perror("Failed to set signal mask");
162 fd = signalfd(-1, &mask, 0);
164 perror("Failed to create signal descriptor");
168 channel = g_io_channel_unix_new(fd);
170 g_io_channel_set_close_on_unref(channel, TRUE);
171 g_io_channel_set_encoding(channel, NULL, NULL);
172 g_io_channel_set_buffered(channel, FALSE);
174 source = g_io_add_watch(channel,
175 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
176 signal_handler, NULL);
178 g_io_channel_unref(channel);
183 static void disconnect_callback(DBusConnection *conn, void *user_data)
185 connman_error("D-Bus disconnect");
187 g_main_loop_quit(main_loop);
190 static gchar *option_config = NULL;
191 static gchar *option_debug = NULL;
192 static gchar *option_plugin = NULL;
193 static gchar *option_noplugin = NULL;
194 static bool option_detach = true;
195 static bool option_version = false;
196 static bool option_routes = false;
198 static bool parse_debug(const char *key, const char *value,
199 gpointer user_data, GError **error)
202 option_debug = g_strdup(value);
204 option_debug = g_strdup("*");
209 static GOptionEntry options[] = {
210 { "config", 'c', 0, G_OPTION_ARG_STRING, &option_config,
211 "Load the specified configuration file "
212 "instead of " CONFIGMAINFILE, "FILE" },
213 { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
214 G_OPTION_ARG_CALLBACK, parse_debug,
215 "Specify debug options to enable", "DEBUG" },
216 { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
217 "Specify plugins to load", "NAME,..." },
218 { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
219 "Specify plugins not to load", "NAME,..." },
220 { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
221 G_OPTION_ARG_NONE, &option_detach,
222 "Don't fork daemon to background" },
223 { "routes", 'r', 0, G_OPTION_ARG_NONE, &option_routes,
224 "Create/delete VPN routes" },
225 { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
226 "Show version information and exit" },
230 bool connman_setting_get_bool(const char *key)
235 char **connman_setting_get_string_list(const char *key)
240 unsigned int *connman_setting_get_uint_list(const char *key)
246 * This function will be called from generic src/agent.c code so we have
247 * to use connman_ prefix instead of vpn_ one.
249 unsigned int connman_timeout_input_request(void)
251 return connman_vpn_settings.timeout_inputreq;
254 unsigned int connman_timeout_browser_launch(void)
256 return connman_vpn_settings.timeout_browserlaunch;
259 int main(int argc, char *argv[])
261 GOptionContext *context;
262 GError *error = NULL;
263 DBusConnection *conn;
267 context = g_option_context_new(NULL);
268 g_option_context_add_main_entries(context, options, NULL);
270 if (!g_option_context_parse(context, &argc, &argv, &error)) {
272 g_printerr("%s\n", error->message);
275 g_printerr("An unknown error occurred\n");
279 g_option_context_free(context);
281 if (option_version) {
282 printf("%s\n", VERSION);
288 perror("Can't start daemon");
293 if (mkdir(VPN_STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
294 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
296 perror("Failed to create state directory");
300 * At some point the VPN stuff is migrated into VPN_STORAGEDIR
301 * and this mkdir() call can be removed.
303 if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
304 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
306 perror("Failed to create storage directory");
309 if (mkdir(VPN_STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
310 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
312 perror("Failed to create VPN storage directory");
317 main_loop = g_main_loop_new(NULL, FALSE);
319 signal = setup_signalfd();
321 dbus_error_init(&err);
323 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, VPN_SERVICE, &err);
325 if (dbus_error_is_set(&err)) {
326 fprintf(stderr, "%s\n", err.message);
327 dbus_error_free(&err);
329 fprintf(stderr, "Can't register with system bus\n");
333 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
335 __connman_log_init(argv[0], option_debug, option_detach, false,
336 "Connection Manager VPN daemon", VERSION);
337 __connman_dbus_init(conn);
340 config_init(CONFIGMAINFILE);
342 config_init(option_config);
344 __connman_inotify_init();
345 __connman_agent_init();
346 __vpn_provider_init(option_routes);
347 __vpn_manager_init();
348 __vpn_ipconfig_init();
350 __connman_task_init();
351 __connman_plugin_init(option_plugin, option_noplugin);
356 g_free(option_plugin);
357 g_free(option_noplugin);
359 g_main_loop_run(main_loop);
361 g_source_remove(signal);
363 __vpn_config_cleanup();
364 __connman_plugin_cleanup();
365 __connman_task_cleanup();
366 __vpn_rtnl_cleanup();
367 __vpn_ipconfig_cleanup();
368 __vpn_manager_cleanup();
369 __vpn_provider_cleanup();
370 __connman_agent_cleanup();
371 __connman_inotify_cleanup();
372 __connman_dbus_cleanup();
373 __connman_log_cleanup(false);
375 dbus_connection_unref(conn);
377 g_main_loop_unref(main_loop);
379 g_free(option_debug);