obexd-test package is needed for testing
[profile/ivi/obexd.git] / src / main.c
1 /*
2  *
3  *  OBEX Server
4  *
5  *  Copyright (C) 2007-2010  Marcel Holtmann <marcel@holtmann.org>
6  *
7  *
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.
12  *
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.
17  *
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
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <signal.h>
34 #include <stdint.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <sys/wait.h>
38 #include <sys/signalfd.h>
39 #include <fcntl.h>
40 #include <termios.h>
41 #include <getopt.h>
42 #include <syslog.h>
43 #include <glib.h>
44
45 #include <gdbus.h>
46
47 #include "log.h"
48 #include "obexd.h"
49 #include "server.h"
50
51 #define DEFAULT_ROOT_PATH "/tmp"
52
53 #define DEFAULT_CAP_FILE CONFIGDIR "/capability.xml"
54
55 static GMainLoop *main_loop = NULL;
56
57 static unsigned int __terminated = 0;
58
59 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
60                                                         gpointer user_data)
61 {
62         struct signalfd_siginfo si;
63         ssize_t result;
64         int fd;
65
66         if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
67                 return FALSE;
68
69         fd = g_io_channel_unix_get_fd(channel);
70
71         result = read(fd, &si, sizeof(si));
72         if (result != sizeof(si))
73                 return FALSE;
74
75         switch (si.ssi_signo) {
76         case SIGINT:
77         case SIGTERM:
78                 if (__terminated == 0) {
79                         info("Terminating");
80                         g_main_loop_quit(main_loop);
81                 }
82
83                 __terminated = 1;
84                 break;
85         case SIGUSR2:
86                 __obex_log_enable_debug();
87                 break;
88         case SIGPIPE:
89                 /* ignore */
90                 break;
91         }
92
93         return TRUE;
94 }
95
96 static guint setup_signalfd(void)
97 {
98         GIOChannel *channel;
99         guint source;
100         sigset_t mask;
101         int fd;
102
103         sigemptyset(&mask);
104         sigaddset(&mask, SIGINT);
105         sigaddset(&mask, SIGTERM);
106         sigaddset(&mask, SIGUSR2);
107         sigaddset(&mask, SIGPIPE);
108
109         if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
110                 perror("Failed to set signal mask");
111                 return 0;
112         }
113
114         fd = signalfd(-1, &mask, 0);
115         if (fd < 0) {
116                 perror("Failed to create signal descriptor");
117                 return 0;
118         }
119
120         channel = g_io_channel_unix_new(fd);
121
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);
125
126         source = g_io_add_watch(channel,
127                                 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
128                                 signal_handler, NULL);
129
130         g_io_channel_unref(channel);
131
132         return source;
133 }
134
135 static gboolean option_detach = TRUE;
136 static char *option_debug = NULL;
137
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;
143
144 static gboolean option_autoaccept = FALSE;
145 static gboolean option_symlinks = FALSE;
146
147 static gboolean parse_debug(const char *key, const char *value,
148                                 gpointer user_data, GError **error)
149 {
150         if (value)
151                 option_debug = g_strdup(value);
152         else
153                 option_debug = g_strdup("*");
154
155         return TRUE;
156 }
157
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 "
169                                 "folder", "PATH" },
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 "
174                                 "folder" },
175         { "capability", 'c', 0, G_OPTION_ARG_STRING, &option_capability,
176                                 "Specify capability file, use '!' mark for "
177                                 "scripts", "FILE" },
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,..." },
184         { NULL },
185 };
186
187 #ifdef TIZEN_PATCH
188 void obex_option_set_root_folder(const char *root)
189 {
190         g_free(option_root);
191         option_root = g_strdup(root);
192
193 }
194 #endif
195
196 gboolean obex_option_auto_accept(void)
197 {
198         return option_autoaccept;
199 }
200
201 const char *obex_option_root_folder(void)
202 {
203         return option_root;
204 }
205
206 gboolean obex_option_symlinks(void)
207 {
208         return option_symlinks;
209 }
210
211 const char *obex_option_capability(void)
212 {
213         return option_capability;
214 }
215
216 static gboolean is_dir(const char *dir) {
217         struct stat st;
218
219         if (stat(dir, &st) < 0) {
220                 error("stat(%s): %s (%d)", dir, strerror(errno), errno);
221                 return FALSE;
222         }
223
224         return S_ISDIR(st.st_mode);
225 }
226
227 static gboolean root_folder_setup(char *root, char *root_setup)
228 {
229         int status;
230         char *argv[3] = { root_setup, root, NULL };
231
232         if (is_dir(root))
233                 return TRUE;
234
235         if (root_setup == NULL)
236                 return FALSE;
237
238         DBG("Setting up %s using %s", root, root_setup);
239
240         if (!g_spawn_sync(NULL, argv, NULL, 0, NULL, NULL, NULL, NULL,
241                                                         &status, NULL)) {
242                 error("Unable to execute %s", root_setup);
243                 return FALSE;
244         }
245
246         if (WEXITSTATUS(status) != EXIT_SUCCESS) {
247                 error("%s exited with status %d", root_setup,
248                                                         WEXITSTATUS(status));
249                 return FALSE;
250         }
251
252         return is_dir(root);
253 }
254
255 int main(int argc, char *argv[])
256 {
257         GOptionContext *context;
258         GError *err = NULL;
259         guint signal;
260
261 #ifdef NEED_THREADS
262         if (g_thread_supported() == FALSE)
263                 g_thread_init(NULL);
264 #endif
265
266         context = g_option_context_new(NULL);
267         g_option_context_add_main_entries(context, options, NULL);
268
269         if (g_option_context_parse(context, &argc, &argv, &err) == FALSE) {
270                 if (err != NULL) {
271                         g_printerr("%s\n", err->message);
272                         g_error_free(err);
273                 } else
274                         g_printerr("An unknown error occurred\n");
275                 exit(EXIT_FAILURE);
276         }
277
278         g_option_context_free(context);
279
280         if (option_detach == TRUE) {
281                 if (daemon(0, 0)) {
282                         perror("Can't start daemon");
283                         exit(1);
284                 }
285         }
286
287         __obex_log_init("obexd", option_debug, option_detach);
288
289         DBG("Entering main loop");
290
291         main_loop = g_main_loop_new(NULL, FALSE);
292
293         signal = setup_signalfd();
294
295 #ifdef NEED_THREADS
296         if (dbus_threads_init_default() == FALSE) {
297                 fprintf(stderr, "Can't init usage of threads\n");
298                 exit(EXIT_FAILURE);
299         }
300 #endif
301
302         if (manager_init() == FALSE) {
303                 error("manager_init failed");
304                 exit(EXIT_FAILURE);
305         }
306
307         if (option_root == NULL)
308                 option_root = g_strdup(DEFAULT_ROOT_PATH);
309
310         if (option_root[0] != '/') {
311                 char *old_root = option_root, *home = getenv("HOME");
312                 if (home) {
313                         option_root = g_strdup_printf("%s/%s", home, old_root);
314                         g_free(old_root);
315                 }
316         }
317
318         if (option_capability == NULL)
319                 option_capability = g_strdup(DEFAULT_CAP_FILE);
320
321         plugin_init(option_plugin, option_noplugin);
322
323         if (obex_server_init() < 0) {
324                 error("obex_server_init failed");
325                 exit(EXIT_FAILURE);
326         }
327
328         if (!root_folder_setup(option_root, option_root_setup)) {
329                 error("Unable to setup root folder %s", option_root);
330                 exit(EXIT_FAILURE);
331         }
332
333         g_main_loop_run(main_loop);
334
335         g_source_remove(signal);
336
337         obex_server_exit();
338
339         plugin_cleanup();
340
341         manager_cleanup();
342
343         g_main_loop_unref(main_loop);
344
345         g_free(option_capability);
346         g_free(option_root);
347
348         __obex_log_cleanup();
349
350         return 0;
351 }