-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.c
310 lines (249 loc) · 7.43 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
*
* OBEX Server
*
* Copyright (C) 2007-2010 Marcel Holtmann <[email protected]>
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <termios.h>
#include <getopt.h>
#include <syslog.h>
#include <glib.h>
#include <gdbus.h>
#include <openobex/obex.h>
#include <openobex/obex_const.h>
#include "log.h"
#include "obexd.h"
#include "obex.h"
#include "obex-priv.h"
#include "server.h"
#include "service.h"
#define DEFAULT_ROOT_PATH "/tmp"
#define DEFAULT_CAP_FILE CONFIGDIR "/capability.xml"
static GMainLoop *main_loop = NULL;
static void sig_term(int sig)
{
info("Terminating due to signal %d", sig);
g_main_loop_quit(main_loop);
}
static void sig_debug(int sig)
{
__obex_log_enable_debug();
}
static gboolean option_detach = TRUE;
static char *option_debug = NULL;
static char *option_root = NULL;
static char *option_root_setup = NULL;
static char *option_capability = NULL;
static gboolean option_autoaccept = FALSE;
static gboolean option_opp = FALSE;
static gboolean option_ftp = FALSE;
static gboolean option_pbap = FALSE;
static gboolean option_irmc = FALSE;
static gboolean option_pcsuite = FALSE;
static gboolean option_symlinks = FALSE;
static gboolean option_syncevolution = FALSE;
static gboolean parse_debug(const char *key, const char *value,
gpointer user_data, GError **error)
{
if (value)
option_debug = g_strdup(value);
else
option_debug = g_strdup("*");
return TRUE;
}
static GOptionEntry options[] = {
{ "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
G_OPTION_ARG_NONE, &option_detach,
"Don't run as daemon in background" },
{ "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
G_OPTION_ARG_CALLBACK, parse_debug,
"Enable debug information output", "DEBUG" },
{ "root", 'r', 0, G_OPTION_ARG_STRING, &option_root,
"Specify root folder location", "PATH" },
{ "root-setup", 'S', 0, G_OPTION_ARG_STRING, &option_root_setup,
"Root folder setup script", "SCRIPT" },
{ "symlinks", 'l', 0, G_OPTION_ARG_NONE, &option_symlinks,
"Enable symlinks on root folder" },
{ "capability", 'c', 0, G_OPTION_ARG_STRING, &option_capability,
"Specify capability file", "FILE" },
{ "auto-accept", 'a', 0, G_OPTION_ARG_NONE, &option_autoaccept,
"Automatically accept push requests" },
{ "opp", 'o', 0, G_OPTION_ARG_NONE, &option_opp,
"Enable Object Push server" },
{ "ftp", 'f', 0, G_OPTION_ARG_NONE, &option_ftp,
"Enable File Transfer server" },
{ "pbap", 'p', 0, G_OPTION_ARG_NONE, &option_pbap,
"Enable Phonebook Access server" },
{ "irmc", 'i', 0, G_OPTION_ARG_NONE, &option_irmc,
"Enable IrMC Sync server" },
{ "pcsuite", 's', 0, G_OPTION_ARG_NONE, &option_pcsuite,
"Enable PC Suite Services server" },
{ "syncevolution", 'e', 0, G_OPTION_ARG_NONE, &option_syncevolution,
"Enable OBEX server for SyncEvolution" },
{ NULL },
};
const char *obex_option_root_folder(void)
{
return option_root;
}
gboolean obex_option_symlinks(void)
{
return option_symlinks;
}
static gboolean is_dir(const char *dir) {
struct stat st;
if (stat(dir, &st) < 0) {
error("stat(%s): %s (%d)", dir, strerror(errno), errno);
return FALSE;
}
return S_ISDIR(st.st_mode);
}
static gboolean root_folder_setup(char *root, char *root_setup)
{
int status;
char *argv[3] = { root_setup, root, NULL };
if (is_dir(root))
return TRUE;
if (root_setup == NULL)
return FALSE;
DBG("Setting up %s using %s", root, root_setup);
if (!g_spawn_sync(NULL, argv, NULL, 0, NULL, NULL, NULL, NULL,
&status, NULL)) {
error("Unable to execute %s", root_setup);
return FALSE;
}
if (WEXITSTATUS(status) != EXIT_SUCCESS) {
error("%s exited with status %d", root_setup,
WEXITSTATUS(status));
return FALSE;
}
return is_dir(root);
}
int main(int argc, char *argv[])
{
GOptionContext *context;
GError *err = NULL;
struct sigaction sa;
#ifdef NEED_THREADS
if (g_thread_supported() == FALSE)
g_thread_init(NULL);
#endif
context = g_option_context_new(NULL);
g_option_context_add_main_entries(context, options, NULL);
if (g_option_context_parse(context, &argc, &argv, &err) == FALSE) {
if (err != NULL) {
g_printerr("%s\n", err->message);
g_error_free(err);
} else
g_printerr("An unknown error occurred\n");
exit(EXIT_FAILURE);
}
g_option_context_free(context);
if (option_detach == TRUE) {
if (daemon(0, 0)) {
perror("Can't start daemon");
exit(1);
}
}
if (option_opp == FALSE && option_ftp == FALSE &&
option_pbap == FALSE &&
option_irmc == FALSE &&
option_syncevolution == FALSE) {
fprintf(stderr, "No server selected (use either "
"--opp, --ftp, --pbap, --irmc or --syncevolution)\n");
exit(EXIT_FAILURE);
}
__obex_log_init("obexd", option_debug, option_detach);
DBG("Entering main loop");
main_loop = g_main_loop_new(NULL, FALSE);
#ifdef NEED_THREADS
if (dbus_threads_init_default() == FALSE) {
fprintf(stderr, "Can't init usage of threads\n");
exit(EXIT_FAILURE);
}
#endif
if (manager_init() == FALSE) {
error("manager_init failed");
exit(EXIT_FAILURE);
}
if (option_root == NULL)
option_root = g_strdup(DEFAULT_ROOT_PATH);
if (option_root[0] != '/') {
char *old_root = option_root, *home = getenv("HOME");
if (home) {
option_root = g_strdup_printf("%s/%s", home, old_root);
g_free(old_root);
}
}
plugin_init();
if (option_capability == NULL)
option_capability = g_strdup(DEFAULT_CAP_FILE);
if (option_opp == TRUE)
obex_server_init(OBEX_OPP, option_root, FALSE,
option_autoaccept, option_symlinks,
NULL);
if (option_ftp == TRUE)
obex_server_init(OBEX_FTP, option_root, TRUE,
option_autoaccept, option_symlinks,
option_capability);
if (option_pbap == TRUE)
obex_server_init(OBEX_PBAP, NULL, TRUE, FALSE, FALSE, NULL);
if (option_pcsuite == TRUE)
obex_server_init(OBEX_PCSUITE, option_root, TRUE,
option_autoaccept, option_symlinks,
option_capability);
if (option_irmc == TRUE)
obex_server_init(OBEX_IRMC, NULL, TRUE, FALSE, FALSE,
option_capability);
if (option_syncevolution == TRUE)
obex_server_init(OBEX_SYNCEVOLUTION, NULL, TRUE, FALSE,
FALSE, NULL);
if (!root_folder_setup(option_root, option_root_setup)) {
error("Unable to setup root folder %s", option_root);
exit(EXIT_FAILURE);
}
memset(&sa, 0, sizeof(sa));
sa.sa_handler = sig_term;
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
sa.sa_handler = sig_debug;
sigaction(SIGUSR2, &sa, NULL);
g_main_loop_run(main_loop);
obex_server_exit();
plugin_cleanup();
manager_cleanup();
g_main_loop_unref(main_loop);
g_free(option_capability);
g_free(option_root);
__obex_log_cleanup();
return 0;
}