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 static GMainLoop *main_loop = NULL;
49 static unsigned int __terminated = 0;
51 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
54 struct signalfd_siginfo si;
58 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
61 fd = g_io_channel_unix_get_fd(channel);
63 result = read(fd, &si, sizeof(si));
64 if (result != sizeof(si))
67 switch (si.ssi_signo) {
70 if (__terminated == 0) {
71 connman_info("Terminating");
72 g_main_loop_quit(main_loop);
82 static guint setup_signalfd(void)
90 sigaddset(&mask, SIGINT);
91 sigaddset(&mask, SIGTERM);
93 if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
94 perror("Failed to set signal mask");
98 fd = signalfd(-1, &mask, 0);
100 perror("Failed to create signal descriptor");
104 channel = g_io_channel_unix_new(fd);
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);
110 source = g_io_add_watch(channel,
111 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
112 signal_handler, NULL);
114 g_io_channel_unref(channel);
119 static void disconnect_callback(DBusConnection *conn, void *user_data)
121 connman_error("D-Bus disconnect");
123 g_main_loop_quit(main_loop);
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;
133 static bool parse_debug(const char *key, const char *value,
134 gpointer user_data, GError **error)
137 option_debug = g_strdup(value);
139 option_debug = g_strdup("*");
144 static GOptionEntry options[] = {
145 { "config", 'c', 0, G_OPTION_ARG_STRING, &option_config,
146 "Load the specified configuration file "
147 "instead of " CONFIGMAINFILE, "FILE" },
148 { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
149 G_OPTION_ARG_CALLBACK, parse_debug,
150 "Specify debug options to enable", "DEBUG" },
151 { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
152 "Specify plugins to load", "NAME,..." },
153 { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
154 "Specify plugins not to load", "NAME,..." },
155 { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
156 G_OPTION_ARG_NONE, &option_detach,
157 "Don't fork daemon to background" },
158 { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
159 "Show version information and exit" },
164 * This function will be called from generic src/agent.c code so we have
165 * to use connman_ prefix instead of vpn_ one.
167 unsigned int connman_timeout_input_request(void)
169 return __vpn_settings_get_timeout_inputreq();
172 int main(int argc, char *argv[])
174 GOptionContext *context;
175 GError *error = NULL;
176 DBusConnection *conn;
180 context = g_option_context_new(NULL);
181 g_option_context_add_main_entries(context, options, NULL);
183 if (!g_option_context_parse(context, &argc, &argv, &error)) {
185 g_printerr("%s\n", error->message);
188 g_printerr("An unknown error occurred\n");
192 g_option_context_free(context);
194 if (option_version) {
195 printf("%s\n", VERSION);
201 perror("Can't start daemon");
206 if (mkdir(VPN_STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
207 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
209 perror("Failed to create state directory");
213 * At some point the VPN stuff is migrated into VPN_STORAGEDIR
214 * and this mkdir() call can be removed.
216 if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
217 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
219 perror("Failed to create storage directory");
222 if (mkdir(VPN_STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
223 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
225 perror("Failed to create VPN storage directory");
230 main_loop = g_main_loop_new(NULL, FALSE);
232 signal = setup_signalfd();
234 dbus_error_init(&err);
236 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, VPN_SERVICE, &err);
238 if (dbus_error_is_set(&err)) {
239 fprintf(stderr, "%s\n", err.message);
240 dbus_error_free(&err);
242 fprintf(stderr, "Can't register with system bus\n");
246 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
248 __connman_log_init(argv[0], option_debug, option_detach, false,
249 "Connection Manager VPN daemon", VERSION);
250 __connman_dbus_init(conn);
253 __vpn_settings_init(CONFIGMAINFILE);
255 __vpn_settings_init(option_config);
257 __connman_inotify_init();
258 __connman_agent_init();
259 __vpn_provider_init();
260 __vpn_manager_init();
261 __vpn_ipconfig_init();
263 __connman_task_init();
264 __connman_plugin_init(option_plugin, option_noplugin);
269 g_free(option_plugin);
270 g_free(option_noplugin);
272 g_main_loop_run(main_loop);
274 g_source_remove(signal);
276 __vpn_config_cleanup();
277 __connman_plugin_cleanup();
278 __connman_task_cleanup();
279 __vpn_rtnl_cleanup();
280 __vpn_ipconfig_cleanup();
281 __vpn_manager_cleanup();
282 __vpn_provider_cleanup();
283 __connman_agent_cleanup();
284 __connman_inotify_cleanup();
285 __connman_dbus_cleanup();
286 __connman_log_cleanup(false);
287 __vpn_settings_cleanup();
289 dbus_connection_unref(conn);
291 g_main_loop_unref(main_loop);
293 g_free(option_debug);