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,..." },
188 void obex_option_set_root_folder(const char *root)
191 option_root = g_strdup(root);
196 gboolean obex_option_auto_accept(void)
198 return option_autoaccept;
201 const char *obex_option_root_folder(void)
206 gboolean obex_option_symlinks(void)
208 return option_symlinks;
211 const char *obex_option_capability(void)
213 return option_capability;
216 static gboolean is_dir(const char *dir) {
219 if (stat(dir, &st) < 0) {
220 error("stat(%s): %s (%d)", dir, strerror(errno), errno);
224 return S_ISDIR(st.st_mode);
227 static gboolean root_folder_setup(char *root, char *root_setup)
230 char *argv[3] = { root_setup, root, NULL };
235 if (root_setup == NULL)
238 DBG("Setting up %s using %s", root, root_setup);
240 if (!g_spawn_sync(NULL, argv, NULL, 0, NULL, NULL, NULL, NULL,
242 error("Unable to execute %s", root_setup);
246 if (WEXITSTATUS(status) != EXIT_SUCCESS) {
247 error("%s exited with status %d", root_setup,
248 WEXITSTATUS(status));
255 int main(int argc, char *argv[])
257 GOptionContext *context;
262 if (g_thread_supported() == FALSE)
266 context = g_option_context_new(NULL);
267 g_option_context_add_main_entries(context, options, NULL);
269 if (g_option_context_parse(context, &argc, &argv, &err) == FALSE) {
271 g_printerr("%s\n", err->message);
274 g_printerr("An unknown error occurred\n");
278 g_option_context_free(context);
280 if (option_detach == TRUE) {
282 perror("Can't start daemon");
287 __obex_log_init("obexd", option_debug, option_detach);
289 DBG("Entering main loop");
291 main_loop = g_main_loop_new(NULL, FALSE);
293 signal = setup_signalfd();
296 if (dbus_threads_init_default() == FALSE) {
297 fprintf(stderr, "Can't init usage of threads\n");
302 if (manager_init() == FALSE) {
303 error("manager_init failed");
307 if (option_root == NULL)
308 option_root = g_strdup(DEFAULT_ROOT_PATH);
310 if (option_root[0] != '/') {
311 char *old_root = option_root, *home = getenv("HOME");
313 option_root = g_strdup_printf("%s/%s", home, old_root);
318 if (option_capability == NULL)
319 option_capability = g_strdup(DEFAULT_CAP_FILE);
321 plugin_init(option_plugin, option_noplugin);
323 if (obex_server_init() < 0) {
324 error("obex_server_init failed");
328 if (!root_folder_setup(option_root, option_root_setup)) {
329 error("Unable to setup root folder %s", option_root);
333 g_main_loop_run(main_loop);
335 g_source_remove(signal);
343 g_main_loop_unref(main_loop);
345 g_free(option_capability);
348 __obex_log_cleanup();