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;
130 static gboolean parse_debug(const char *key, const char *value,
131 gpointer user_data, GError **error)
134 option_debug = g_strdup(value);
136 option_debug = g_strdup("*");
141 static GOptionEntry options[] = {
142 { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
143 G_OPTION_ARG_CALLBACK, parse_debug,
144 "Specify debug options to enable", "DEBUG" },
145 { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
146 "Specify plugins to load", "NAME,..." },
147 { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
148 "Specify plugins not to load", "NAME,..." },
149 { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
150 G_OPTION_ARG_NONE, &option_detach,
151 "Don't fork daemon to background" },
152 { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
153 "Show version information and exit" },
157 int main(int argc, char *argv[])
159 GOptionContext *context;
160 GError *error = NULL;
161 DBusConnection *conn;
165 context = g_option_context_new(NULL);
166 g_option_context_add_main_entries(context, options, NULL);
168 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
170 g_printerr("%s\n", error->message);
173 g_printerr("An unknown error occurred\n");
177 g_option_context_free(context);
179 if (option_version == TRUE) {
180 printf("%s\n", VERSION);
184 if (option_detach == TRUE) {
186 perror("Can't start daemon");
191 if (mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
192 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
194 perror("Failed to create state directory");
197 if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
198 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
200 perror("Failed to create storage directory");
205 main_loop = g_main_loop_new(NULL, FALSE);
207 signal = setup_signalfd();
209 dbus_error_init(&err);
211 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, VPN_SERVICE, &err);
213 if (dbus_error_is_set(&err) == TRUE) {
214 fprintf(stderr, "%s\n", err.message);
215 dbus_error_free(&err);
217 fprintf(stderr, "Can't register with system bus\n");
221 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
223 __connman_log_init(argv[0], option_debug, option_detach, FALSE,
224 "Connection Manager VPN daemon", VERSION);
225 __connman_dbus_init(conn);
226 __vpn_provider_init();
227 __vpn_manager_init();
228 __vpn_ipconfig_init();
230 __connman_task_init();
231 __connman_plugin_init(option_plugin, option_noplugin);
235 g_free(option_plugin);
236 g_free(option_noplugin);
238 g_main_loop_run(main_loop);
240 g_source_remove(signal);
242 __connman_task_cleanup();
243 __vpn_rtnl_cleanup();
244 __vpn_ipconfig_cleanup();
245 __vpn_manager_cleanup();
246 __vpn_provider_cleanup();
247 __connman_dbus_cleanup();
248 __connman_log_cleanup(FALSE);
250 dbus_connection_unref(conn);
252 g_main_loop_unref(main_loop);
254 g_free(option_debug);