5 * Copyright (C) 2007-2010 Marcel Holtmann <marcel@holtmann.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
35 #include <sys/types.h>
38 #include <sys/signalfd.h>
51 #define DEFAULT_ROOT_PATH "/tmp"
53 #define DEFAULT_CAP_FILE CONFIGDIR "/capability.xml"
55 static GMainLoop *main_loop = NULL;
57 static unsigned int __terminated = 0;
59 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
62 struct signalfd_siginfo si;
66 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
69 fd = g_io_channel_unix_get_fd(channel);
71 result = read(fd, &si, sizeof(si));
72 if (result != sizeof(si))
75 switch (si.ssi_signo) {
78 if (__terminated == 0) {
80 g_main_loop_quit(main_loop);
86 __obex_log_enable_debug();
96 static guint setup_signalfd(void)
104 sigaddset(&mask, SIGINT);
105 sigaddset(&mask, SIGTERM);
106 sigaddset(&mask, SIGUSR2);
107 sigaddset(&mask, SIGPIPE);
109 if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
110 perror("Failed to set signal mask");
114 fd = signalfd(-1, &mask, 0);
116 perror("Failed to create signal descriptor");
120 channel = g_io_channel_unix_new(fd);
122 g_io_channel_set_close_on_unref(channel, TRUE);
123 g_io_channel_set_encoding(channel, NULL, NULL);
124 g_io_channel_set_buffered(channel, FALSE);
126 source = g_io_add_watch(channel,
127 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
128 signal_handler, NULL);
130 g_io_channel_unref(channel);
135 static gboolean option_detach = TRUE;
136 static char *option_debug = NULL;
138 static char *option_root = NULL;
139 static char *option_root_setup = NULL;
140 static char *option_capability = NULL;
141 static char *option_plugin = NULL;
142 static char *option_noplugin = NULL;
144 static gboolean option_autoaccept = FALSE;
145 static gboolean option_symlinks = FALSE;
147 static gboolean parse_debug(const char *key, const char *value,
148 gpointer user_data, GError **error)
151 option_debug = g_strdup(value);
153 option_debug = g_strdup("*");
158 static GOptionEntry options[] = {
159 { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
160 G_OPTION_ARG_NONE, &option_detach,
161 "Don't run as daemon in background" },
162 { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
163 G_OPTION_ARG_CALLBACK, parse_debug,
164 "Enable debug information output", "DEBUG" },
165 { "root", 'r', 0, G_OPTION_ARG_STRING, &option_root,
166 "Specify root folder location. Both absolute "
167 "and relative can be used, but relative paths "
168 "are assumed to be relative to user $HOME "
170 { "root-setup", 'S', 0, G_OPTION_ARG_STRING, &option_root_setup,
171 "Root folder setup script", "SCRIPT" },
172 { "symlinks", 'l', 0, G_OPTION_ARG_NONE, &option_symlinks,
173 "Allow symlinks leading outside of the root "
175 { "capability", 'c', 0, G_OPTION_ARG_STRING, &option_capability,
176 "Specify capability file, use '!' mark for "
178 { "auto-accept", 'a', 0, G_OPTION_ARG_NONE, &option_autoaccept,
179 "Automatically accept push requests" },
180 { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
181 "Specify plugins to load", "NAME,..." },
182 { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
183 "Specify plugins not to load", "NAME,..." },
187 gboolean obex_option_auto_accept(void)
189 return option_autoaccept;
192 const char *obex_option_root_folder(void)
197 gboolean obex_option_symlinks(void)
199 return option_symlinks;
202 const char *obex_option_capability(void)
204 return option_capability;
207 static gboolean is_dir(const char *dir) {
210 if (stat(dir, &st) < 0) {
211 error("stat(%s): %s (%d)", dir, strerror(errno), errno);
215 return S_ISDIR(st.st_mode);
218 static gboolean root_folder_setup(char *root, char *root_setup)
221 char *argv[3] = { root_setup, root, NULL };
226 if (root_setup == NULL)
229 DBG("Setting up %s using %s", root, root_setup);
231 if (!g_spawn_sync(NULL, argv, NULL, 0, NULL, NULL, NULL, NULL,
233 error("Unable to execute %s", root_setup);
237 if (WEXITSTATUS(status) != EXIT_SUCCESS) {
238 error("%s exited with status %d", root_setup,
239 WEXITSTATUS(status));
246 int main(int argc, char *argv[])
248 GOptionContext *context;
253 if (g_thread_supported() == FALSE)
257 context = g_option_context_new(NULL);
258 g_option_context_add_main_entries(context, options, NULL);
260 if (g_option_context_parse(context, &argc, &argv, &err) == FALSE) {
262 g_printerr("%s\n", err->message);
265 g_printerr("An unknown error occurred\n");
269 g_option_context_free(context);
271 if (option_detach == TRUE) {
273 perror("Can't start daemon");
278 __obex_log_init("obexd", option_debug, option_detach);
280 DBG("Entering main loop");
282 main_loop = g_main_loop_new(NULL, FALSE);
284 signal = setup_signalfd();
287 if (dbus_threads_init_default() == FALSE) {
288 fprintf(stderr, "Can't init usage of threads\n");
293 if (manager_init() == FALSE) {
294 error("manager_init failed");
298 if (option_root == NULL)
299 option_root = g_strdup(DEFAULT_ROOT_PATH);
301 if (option_root[0] != '/') {
302 char *old_root = option_root, *home = getenv("HOME");
304 option_root = g_strdup_printf("%s/%s", home, old_root);
309 if (option_capability == NULL)
310 option_capability = g_strdup(DEFAULT_CAP_FILE);
312 plugin_init(option_plugin, option_noplugin);
314 if (obex_server_init() < 0) {
315 error("obex_server_init failed");
319 if (!root_folder_setup(option_root, option_root_setup)) {
320 error("Unable to setup root folder %s", option_root);
324 g_main_loop_run(main_loop);
326 g_source_remove(signal);
334 g_main_loop_unref(main_loop);
336 g_free(option_capability);
339 __obex_log_cleanup();