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 #if defined TIZEN_EXT
236 unsigned int connman_setting_get_uint(const char *key)
241 int connman_setting_get_int(const char *key)
247 char **connman_setting_get_string_list(const char *key)
252 unsigned int *connman_setting_get_uint_list(const char *key)
258 * This function will be called from generic src/agent.c code so we have
259 * to use connman_ prefix instead of vpn_ one.
261 unsigned int connman_timeout_input_request(void)
263 return __vpn_settings_get_timeout_inputreq();
266 unsigned int connman_timeout_browser_launch(void)
268 return connman_vpn_settings.timeout_browserlaunch;
271 const char *connman_option_get_string(const char *key)
276 int main(int argc, char *argv[])
278 GOptionContext *context;
279 GError *error = NULL;
280 DBusConnection *conn;
284 context = g_option_context_new(NULL);
285 g_option_context_add_main_entries(context, options, NULL);
287 if (!g_option_context_parse(context, &argc, &argv, &error)) {
289 g_printerr("%s\n", error->message);
292 g_printerr("An unknown error occurred\n");
296 g_option_context_free(context);
298 if (option_version) {
299 printf("%s\n", VERSION);
305 perror("Can't start daemon");
310 if (mkdir(VPN_STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
311 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
313 perror("Failed to create state directory");
317 * At some point the VPN stuff is migrated into VPN_STORAGEDIR
318 * and this mkdir() call can be removed.
320 if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
321 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
323 perror("Failed to create storage directory");
326 if (mkdir(VPN_STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
327 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
329 perror("Failed to create VPN storage directory");
334 main_loop = g_main_loop_new(NULL, FALSE);
336 signal = setup_signalfd();
338 dbus_error_init(&err);
340 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, VPN_SERVICE, &err);
342 if (dbus_error_is_set(&err)) {
343 fprintf(stderr, "%s\n", err.message);
344 dbus_error_free(&err);
346 fprintf(stderr, "Can't register with system bus\n");
350 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
352 __connman_log_init(argv[0], option_debug, option_detach, false,
353 "Connection Manager VPN daemon", VERSION);
354 __connman_dbus_init(conn);
357 __vpn_settings_init(CONFIGMAINFILE);
359 __vpn_settings_init(option_config);
361 __connman_inotify_init();
362 __connman_agent_init();
363 __vpn_provider_init(option_routes);
364 __vpn_manager_init();
365 __vpn_ipconfig_init();
367 __connman_task_init();
368 __connman_plugin_init(option_plugin, option_noplugin);
373 g_free(option_plugin);
374 g_free(option_noplugin);
376 g_main_loop_run(main_loop);
378 g_source_remove(signal);
380 __vpn_config_cleanup();
381 __connman_plugin_cleanup();
382 __connman_task_cleanup();
383 __vpn_rtnl_cleanup();
384 __vpn_ipconfig_cleanup();
385 __vpn_manager_cleanup();
386 __vpn_provider_cleanup();
387 __connman_agent_cleanup();
388 __connman_inotify_cleanup();
389 __connman_dbus_cleanup();
390 __connman_log_cleanup(false);
391 __vpn_settings_cleanup();
393 dbus_connection_unref(conn);
395 g_main_loop_unref(main_loop);
397 g_free(option_debug);