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 static GMainLoop *main_loop = NULL;
47 static unsigned int __terminated = 0;
49 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
52 struct signalfd_siginfo si;
56 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
59 fd = g_io_channel_unix_get_fd(channel);
61 result = read(fd, &si, sizeof(si));
62 if (result != sizeof(si))
65 switch (si.ssi_signo) {
68 if (__terminated == 0) {
69 connman_info("Terminating");
70 g_main_loop_quit(main_loop);
80 static guint setup_signalfd(void)
88 sigaddset(&mask, SIGINT);
89 sigaddset(&mask, SIGTERM);
91 if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
92 perror("Failed to set signal mask");
96 fd = signalfd(-1, &mask, 0);
98 perror("Failed to create signal descriptor");
102 channel = g_io_channel_unix_new(fd);
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);
108 source = g_io_add_watch(channel,
109 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
110 signal_handler, NULL);
112 g_io_channel_unref(channel);
117 static void disconnect_callback(DBusConnection *conn, void *user_data)
119 connman_error("D-Bus disconnect");
121 g_main_loop_quit(main_loop);
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 static gboolean option_routes = FALSE;
131 static gboolean parse_debug(const char *key, const char *value,
132 gpointer user_data, GError **error)
135 option_debug = g_strdup(value);
137 option_debug = g_strdup("*");
142 static GOptionEntry options[] = {
143 { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
144 G_OPTION_ARG_CALLBACK, parse_debug,
145 "Specify debug options to enable", "DEBUG" },
146 { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
147 "Specify plugins to load", "NAME,..." },
148 { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
149 "Specify plugins not to load", "NAME,..." },
150 { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
151 G_OPTION_ARG_NONE, &option_detach,
152 "Don't fork daemon to background" },
153 { "routes", 'r', 0, G_OPTION_ARG_NONE, &option_routes,
154 "Create/delete VPN routes" },
155 { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
156 "Show version information and exit" },
160 int main(int argc, char *argv[])
162 GOptionContext *context;
163 GError *error = NULL;
164 DBusConnection *conn;
168 context = g_option_context_new(NULL);
169 g_option_context_add_main_entries(context, options, NULL);
171 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
173 g_printerr("%s\n", error->message);
176 g_printerr("An unknown error occurred\n");
180 g_option_context_free(context);
182 if (option_version == TRUE) {
183 printf("%s\n", VERSION);
187 if (option_detach == TRUE) {
189 perror("Can't start daemon");
194 if (mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
195 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
197 perror("Failed to create state directory");
200 if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
201 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
203 perror("Failed to create storage directory");
208 main_loop = g_main_loop_new(NULL, FALSE);
210 signal = setup_signalfd();
212 dbus_error_init(&err);
214 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, VPN_SERVICE, &err);
216 if (dbus_error_is_set(&err) == TRUE) {
217 fprintf(stderr, "%s\n", err.message);
218 dbus_error_free(&err);
220 fprintf(stderr, "Can't register with system bus\n");
224 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
226 __connman_log_init(argv[0], option_debug, option_detach, FALSE,
227 "Connection Manager VPN daemon", VERSION);
228 __connman_dbus_init(conn);
229 __vpn_provider_init(option_routes);
230 __vpn_manager_init();
231 __vpn_ipconfig_init();
233 __connman_task_init();
234 __connman_plugin_init(option_plugin, option_noplugin);
238 g_free(option_plugin);
239 g_free(option_noplugin);
241 g_main_loop_run(main_loop);
243 g_source_remove(signal);
245 __connman_task_cleanup();
246 __vpn_rtnl_cleanup();
247 __vpn_ipconfig_cleanup();
248 __vpn_manager_cleanup();
249 __vpn_provider_cleanup();
250 __connman_dbus_cleanup();
251 __connman_log_cleanup(FALSE);
253 dbus_connection_unref(conn);
255 g_main_loop_unref(main_loop);
257 g_free(option_debug);