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>
45 #include "gdbus/gdbus.h"
47 #include "../client/manager.h"
53 #define DEFAULT_CAP_FILE CONFIGDIR "/capability.xml"
55 static GMainLoop *main_loop = NULL;
57 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
60 static unsigned int __terminated = 0;
61 struct signalfd_siginfo si;
65 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
68 fd = g_io_channel_unix_get_fd(channel);
70 result = read(fd, &si, sizeof(si));
71 if (result != sizeof(si))
74 switch (si.ssi_signo) {
77 if (__terminated == 0) {
79 g_main_loop_quit(main_loop);
85 __obex_log_enable_debug();
92 static guint setup_signalfd(void)
100 sigaddset(&mask, SIGINT);
101 sigaddset(&mask, SIGTERM);
102 sigaddset(&mask, SIGUSR2);
104 if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
105 perror("Failed to set signal mask");
109 fd = signalfd(-1, &mask, 0);
111 perror("Failed to create signal descriptor");
115 channel = g_io_channel_unix_new(fd);
117 g_io_channel_set_close_on_unref(channel, TRUE);
118 g_io_channel_set_encoding(channel, NULL, NULL);
119 g_io_channel_set_buffered(channel, FALSE);
121 source = g_io_add_watch(channel,
122 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
123 signal_handler, NULL);
125 g_io_channel_unref(channel);
130 static gboolean option_detach = TRUE;
131 static char *option_debug = NULL;
133 static char *option_root = NULL;
134 static char *option_root_setup = NULL;
135 static char *option_capability = NULL;
136 static char *option_plugin = NULL;
137 static char *option_noplugin = NULL;
139 static gboolean option_autoaccept = FALSE;
140 static gboolean option_symlinks = FALSE;
142 static gboolean parse_debug(const char *key, const char *value,
143 gpointer user_data, GError **error)
146 option_debug = g_strdup(value);
148 option_debug = g_strdup("*");
153 static GOptionEntry options[] = {
154 { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
155 G_OPTION_ARG_CALLBACK, parse_debug,
156 "Enable debug information output", "DEBUG" },
157 { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
158 "Specify plugins to load", "NAME,..." },
159 { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
160 "Specify plugins not to load", "NAME,..." },
161 { "nodetach", 'n', G_OPTION_FLAG_REVERSE,
162 G_OPTION_ARG_NONE, &option_detach,
163 "Run with logging in foreground" },
164 { "root", 'r', 0, G_OPTION_ARG_STRING, &option_root,
165 "Specify root folder location. Both absolute "
166 "and relative can be used, but relative paths "
167 "are assumed to be relative to user $HOME "
168 "folder. Default $XDG_CACHE_HOME", "PATH" },
169 { "root-setup", 'S', 0, G_OPTION_ARG_STRING, &option_root_setup,
170 "Root folder setup script", "SCRIPT" },
171 { "symlinks", 'l', 0, G_OPTION_ARG_NONE, &option_symlinks,
172 "Allow symlinks leading outside of the root "
174 { "capability", 'c', 0, G_OPTION_ARG_STRING, &option_capability,
175 "Specify capability file, use '!' mark for "
177 { "auto-accept", 'a', 0, G_OPTION_ARG_NONE, &option_autoaccept,
178 "Automatically accept push requests" },
182 #ifdef __TIZEN_PATCH__
183 void obex_option_set_root_folder(const char *root)
186 option_root = g_strdup(root);
190 gboolean obex_option_auto_accept(void)
192 return option_autoaccept;
195 const char *obex_option_root_folder(void)
200 gboolean obex_option_symlinks(void)
202 return option_symlinks;
205 const char *obex_option_capability(void)
207 return option_capability;
210 static gboolean is_dir(const char *dir)
214 if (stat(dir, &st) < 0) {
215 error("stat(%s): %s (%d)", dir, strerror(errno), errno);
219 return S_ISDIR(st.st_mode);
222 static gboolean root_folder_setup(char *root, char *root_setup)
225 char *argv[3] = { root_setup, root, NULL };
230 if (root_setup == NULL)
233 DBG("Setting up %s using %s", root, root_setup);
235 if (!g_spawn_sync(NULL, argv, NULL, 0, NULL, NULL, NULL, NULL,
237 error("Unable to execute %s", root_setup);
241 if (WEXITSTATUS(status) != EXIT_SUCCESS) {
242 error("%s exited with status %d", root_setup,
243 WEXITSTATUS(status));
250 int main(int argc, char *argv[])
252 GOptionContext *context;
257 if (g_thread_supported() == FALSE)
261 context = g_option_context_new(NULL);
262 g_option_context_add_main_entries(context, options, NULL);
264 if (g_option_context_parse(context, &argc, &argv, &err) == FALSE) {
266 g_printerr("%s\n", err->message);
269 g_printerr("An unknown error occurred\n");
273 g_option_context_free(context);
275 __obex_log_init(option_debug, option_detach);
277 DBG("Entering main loop");
279 main_loop = g_main_loop_new(NULL, FALSE);
281 signal = setup_signalfd();
284 if (dbus_threads_init_default() == FALSE) {
285 fprintf(stderr, "Can't init usage of threads\n");
290 if (manager_init() == FALSE) {
291 error("manager_init failed");
295 if (option_root == NULL) {
296 option_root = g_build_filename(g_get_user_cache_dir(), "obexd",
298 g_mkdir_with_parents(option_root, 0700);
301 if (option_root[0] != '/') {
302 const char *home = getenv("HOME");
304 char *old_root = option_root;
305 option_root = g_strdup_printf("%s/%s", home, old_root);
310 if (option_capability == NULL)
311 option_capability = g_strdup(DEFAULT_CAP_FILE);
313 plugin_init(option_plugin, option_noplugin);
315 if (obex_server_init() < 0) {
316 error("obex_server_init failed");
320 if (!root_folder_setup(option_root, option_root_setup)) {
321 error("Unable to setup root folder %s", option_root);
325 if (client_manager_init() < 0) {
326 error("client_manager_init failed");
330 g_main_loop_run(main_loop);
332 g_source_remove(signal);
334 client_manager_exit();
342 g_main_loop_unref(main_loop);
344 g_free(option_capability);
347 __obex_log_cleanup();