5 * Copyright (C) 2012 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
49 static GMainLoop *main_loop = NULL;
51 static unsigned int __terminated = 0;
54 unsigned int timeout_inputreq;
55 } connman_vpn_settings = {
56 .timeout_inputreq = DEFAULT_INPUT_REQUEST_TIMEOUT,
59 static GKeyFile *load_config(const char *file)
64 keyfile = g_key_file_new();
66 g_key_file_set_list_separator(keyfile, ',');
68 if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
69 if (err->code != G_FILE_ERROR_NOENT) {
70 connman_error("Parsing %s failed: %s", file,
75 g_key_file_free(keyfile);
82 static void parse_config(GKeyFile *config, const char *file)
90 DBG("parsing %s", file);
92 timeout = g_key_file_get_integer(config, "General",
93 "InputRequestTimeout", &error);
94 if (error == NULL && timeout >= 0)
95 connman_vpn_settings.timeout_inputreq = timeout * 1000;
97 g_clear_error(&error);
100 static int config_init(const char *file)
104 config = load_config(file);
105 parse_config(config, file);
107 g_key_file_free(config);
112 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
115 struct signalfd_siginfo si;
119 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
122 fd = g_io_channel_unix_get_fd(channel);
124 result = read(fd, &si, sizeof(si));
125 if (result != sizeof(si))
128 switch (si.ssi_signo) {
131 if (__terminated == 0) {
132 connman_info("Terminating");
133 g_main_loop_quit(main_loop);
143 static guint setup_signalfd(void)
151 sigaddset(&mask, SIGINT);
152 sigaddset(&mask, SIGTERM);
154 if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
155 perror("Failed to set signal mask");
159 fd = signalfd(-1, &mask, 0);
161 perror("Failed to create signal descriptor");
165 channel = g_io_channel_unix_new(fd);
167 g_io_channel_set_close_on_unref(channel, TRUE);
168 g_io_channel_set_encoding(channel, NULL, NULL);
169 g_io_channel_set_buffered(channel, FALSE);
171 source = g_io_add_watch(channel,
172 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
173 signal_handler, NULL);
175 g_io_channel_unref(channel);
180 static void disconnect_callback(DBusConnection *conn, void *user_data)
182 connman_error("D-Bus disconnect");
184 g_main_loop_quit(main_loop);
187 static gchar *option_config = NULL;
188 static gchar *option_debug = NULL;
189 static gchar *option_plugin = NULL;
190 static gchar *option_noplugin = NULL;
191 static gboolean option_detach = TRUE;
192 static gboolean option_version = FALSE;
193 static gboolean option_routes = FALSE;
195 static gboolean parse_debug(const char *key, const char *value,
196 gpointer user_data, GError **error)
199 option_debug = g_strdup(value);
201 option_debug = g_strdup("*");
206 static GOptionEntry options[] = {
207 { "config", 'c', 0, G_OPTION_ARG_STRING, &option_config,
208 "Load the specified configuration file "
209 "instead of " CONFIGMAINFILE, "FILE" },
210 { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
211 G_OPTION_ARG_CALLBACK, parse_debug,
212 "Specify debug options to enable", "DEBUG" },
213 { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
214 "Specify plugins to load", "NAME,..." },
215 { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
216 "Specify plugins not to load", "NAME,..." },
217 { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
218 G_OPTION_ARG_NONE, &option_detach,
219 "Don't fork daemon to background" },
220 { "routes", 'r', 0, G_OPTION_ARG_NONE, &option_routes,
221 "Create/delete VPN routes" },
222 { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
223 "Show version information and exit" },
228 * This function will be called from generic src/agent.c code so we have
229 * to use connman_ prefix instead of vpn_ one.
231 unsigned int connman_timeout_input_request(void)
233 return connman_vpn_settings.timeout_inputreq;
236 int main(int argc, char *argv[])
238 GOptionContext *context;
239 GError *error = NULL;
240 DBusConnection *conn;
244 context = g_option_context_new(NULL);
245 g_option_context_add_main_entries(context, options, NULL);
247 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
249 g_printerr("%s\n", error->message);
252 g_printerr("An unknown error occurred\n");
256 g_option_context_free(context);
258 if (option_version == TRUE) {
259 printf("%s\n", VERSION);
263 if (option_detach == TRUE) {
265 perror("Can't start daemon");
270 if (mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
271 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
273 perror("Failed to create state directory");
276 if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
277 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
279 perror("Failed to create storage directory");
284 main_loop = g_main_loop_new(NULL, FALSE);
286 signal = setup_signalfd();
288 dbus_error_init(&err);
290 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, VPN_SERVICE, &err);
292 if (dbus_error_is_set(&err) == TRUE) {
293 fprintf(stderr, "%s\n", err.message);
294 dbus_error_free(&err);
296 fprintf(stderr, "Can't register with system bus\n");
300 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
302 __connman_log_init(argv[0], option_debug, option_detach, FALSE,
303 "Connection Manager VPN daemon", VERSION);
304 __connman_dbus_init(conn);
306 if (option_config == NULL)
307 config_init(CONFIGMAINFILE);
309 config_init(option_config);
311 __connman_agent_init();
312 __vpn_provider_init(option_routes);
313 __vpn_manager_init();
314 __vpn_ipconfig_init();
316 __connman_task_init();
317 __connman_plugin_init(option_plugin, option_noplugin);
321 g_free(option_plugin);
322 g_free(option_noplugin);
324 g_main_loop_run(main_loop);
326 g_source_remove(signal);
328 __connman_task_cleanup();
329 __vpn_rtnl_cleanup();
330 __vpn_ipconfig_cleanup();
331 __vpn_manager_cleanup();
332 __vpn_provider_cleanup();
333 __connman_agent_cleanup();
334 __connman_dbus_cleanup();
335 __connman_log_cleanup(FALSE);
337 dbus_connection_unref(conn);
339 g_main_loop_unref(main_loop);
341 g_free(option_debug);