upgrade obexd to 0.47
[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 gboolean obex_option_auto_accept(void)
188 {
189         return option_autoaccept;
190 }
191
192 const char *obex_option_root_folder(void)
193 {
194         return option_root;
195 }
196
197 gboolean obex_option_symlinks(void)
198 {
199         return option_symlinks;
200 }
201
202 const char *obex_option_capability(void)
203 {
204         return option_capability;
205 }
206
207 static gboolean is_dir(const char *dir) {
208         struct stat st;
209
210         if (stat(dir, &st) < 0) {
211                 error("stat(%s): %s (%d)", dir, strerror(errno), errno);
212                 return FALSE;
213         }
214
215         return S_ISDIR(st.st_mode);
216 }
217
218 static gboolean root_folder_setup(char *root, char *root_setup)
219 {
220         int status;
221         char *argv[3] = { root_setup, root, NULL };
222
223         if (is_dir(root))
224                 return TRUE;
225
226         if (root_setup == NULL)
227                 return FALSE;
228
229         DBG("Setting up %s using %s", root, root_setup);
230
231         if (!g_spawn_sync(NULL, argv, NULL, 0, NULL, NULL, NULL, NULL,
232                                                         &status, NULL)) {
233                 error("Unable to execute %s", root_setup);
234                 return FALSE;
235         }
236
237         if (WEXITSTATUS(status) != EXIT_SUCCESS) {
238                 error("%s exited with status %d", root_setup,
239                                                         WEXITSTATUS(status));
240                 return FALSE;
241         }
242
243         return is_dir(root);
244 }
245
246 int main(int argc, char *argv[])
247 {
248         GOptionContext *context;
249         GError *err = NULL;
250         guint signal;
251
252 #ifdef NEED_THREADS
253         if (g_thread_supported() == FALSE)
254                 g_thread_init(NULL);
255 #endif
256
257         context = g_option_context_new(NULL);
258         g_option_context_add_main_entries(context, options, NULL);
259
260         if (g_option_context_parse(context, &argc, &argv, &err) == FALSE) {
261                 if (err != NULL) {
262                         g_printerr("%s\n", err->message);
263                         g_error_free(err);
264                 } else
265                         g_printerr("An unknown error occurred\n");
266                 exit(EXIT_FAILURE);
267         }
268
269         g_option_context_free(context);
270
271         if (option_detach == TRUE) {
272                 if (daemon(0, 0)) {
273                         perror("Can't start daemon");
274                         exit(1);
275                 }
276         }
277
278         __obex_log_init("obexd", option_debug, option_detach);
279
280         DBG("Entering main loop");
281
282         main_loop = g_main_loop_new(NULL, FALSE);
283
284         signal = setup_signalfd();
285
286 #ifdef NEED_THREADS
287         if (dbus_threads_init_default() == FALSE) {
288                 fprintf(stderr, "Can't init usage of threads\n");
289                 exit(EXIT_FAILURE);
290         }
291 #endif
292
293         if (manager_init() == FALSE) {
294                 error("manager_init failed");
295                 exit(EXIT_FAILURE);
296         }
297
298         if (option_root == NULL)
299                 option_root = g_strdup(DEFAULT_ROOT_PATH);
300
301         if (option_root[0] != '/') {
302                 char *old_root = option_root, *home = getenv("HOME");
303                 if (home) {
304                         option_root = g_strdup_printf("%s/%s", home, old_root);
305                         g_free(old_root);
306                 }
307         }
308
309         if (option_capability == NULL)
310                 option_capability = g_strdup(DEFAULT_CAP_FILE);
311
312         plugin_init(option_plugin, option_noplugin);
313
314         if (obex_server_init() < 0) {
315                 error("obex_server_init failed");
316                 exit(EXIT_FAILURE);
317         }
318
319         if (!root_folder_setup(option_root, option_root_setup)) {
320                 error("Unable to setup root folder %s", option_root);
321                 exit(EXIT_FAILURE);
322         }
323
324         g_main_loop_run(main_loop);
325
326         g_source_remove(signal);
327
328         obex_server_exit();
329
330         plugin_cleanup();
331
332         manager_cleanup();
333
334         g_main_loop_unref(main_loop);
335
336         g_free(option_capability);
337         g_free(option_root);
338
339         __obex_log_cleanup();
340
341         return 0;
342 }