blob: 7b994fd3b3a28e1c4ab6df2f2e45243cd6c69c98 [file] [log] [blame]
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001/*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
4
5 This program can be distributed under the terms of the GNU LGPLv2.
6 See the file COPYING.LIB.
7*/
8
9#include "config.h"
10#include "fuse_i.h"
11#include "fuse_misc.h"
12#include "fuse_opt.h"
13#include "fuse_lowlevel.h"
14#include "fuse_common_compat.h"
15
16#include <stdio.h>
17#include <stdlib.h>
18#include <stddef.h>
19#include <unistd.h>
20#include <string.h>
21#include <limits.h>
22#include <errno.h>
23#include <sys/param.h>
24
25enum {
26 KEY_HELP,
27 KEY_HELP_NOHEADER,
28 KEY_VERSION,
29};
30
31struct helper_opts {
32 int singlethread;
33 int foreground;
34 int nodefault_subtype;
35 char *mountpoint;
36};
37
38#define FUSE_HELPER_OPT(t, p) { t, offsetof(struct helper_opts, p), 1 }
39
40static const struct fuse_opt fuse_helper_opts[] = {
41 FUSE_HELPER_OPT("-d", foreground),
42 FUSE_HELPER_OPT("debug", foreground),
43 FUSE_HELPER_OPT("-f", foreground),
44 FUSE_HELPER_OPT("-s", singlethread),
45 FUSE_HELPER_OPT("fsname=", nodefault_subtype),
46 FUSE_HELPER_OPT("subtype=", nodefault_subtype),
47
48 FUSE_OPT_KEY("-h", KEY_HELP),
49 FUSE_OPT_KEY("--help", KEY_HELP),
50 FUSE_OPT_KEY("-ho", KEY_HELP_NOHEADER),
51 FUSE_OPT_KEY("-V", KEY_VERSION),
52 FUSE_OPT_KEY("--version", KEY_VERSION),
53 FUSE_OPT_KEY("-d", FUSE_OPT_KEY_KEEP),
54 FUSE_OPT_KEY("debug", FUSE_OPT_KEY_KEEP),
55 FUSE_OPT_KEY("fsname=", FUSE_OPT_KEY_KEEP),
56 FUSE_OPT_KEY("subtype=", FUSE_OPT_KEY_KEEP),
57 FUSE_OPT_END
58};
59
60static void usage(const char *progname)
61{
62 fprintf(stderr,
63 "usage: %s mountpoint [options]\n\n", progname);
64 fprintf(stderr,
65 "general options:\n"
66 " -o opt,[opt...] mount options\n"
67 " -h --help print help\n"
68 " -V --version print version\n"
69 "\n");
70}
71
72static void helper_help(void)
73{
74 fprintf(stderr,
75 "FUSE options:\n"
76 " -d -o debug enable debug output (implies -f)\n"
77 " -f foreground operation\n"
78 " -s disable multi-threaded operation\n"
79 "\n"
80 );
81}
82
83static void helper_version(void)
84{
85 fprintf(stderr, "FUSE library version: %s\n", PACKAGE_VERSION);
86}
87
88static int fuse_helper_opt_proc(void *data, const char *arg, int key,
89 struct fuse_args *outargs)
90{
91 struct helper_opts *hopts = data;
92
93 switch (key) {
94 case KEY_HELP:
95 usage(outargs->argv[0]);
96 /* fall through */
97
98 case KEY_HELP_NOHEADER:
99 helper_help();
100 return fuse_opt_add_arg(outargs, "-h");
101
102 case KEY_VERSION:
103 helper_version();
104 return 1;
105
106 case FUSE_OPT_KEY_NONOPT:
107 if (!hopts->mountpoint) {
108 char mountpoint[PATH_MAX];
109 if (realpath(arg, mountpoint) == NULL) {
110 fprintf(stderr,
111 "fuse: bad mount point `%s': %s\n",
112 arg, strerror(errno));
113 return -1;
114 }
115 return fuse_opt_add_opt(&hopts->mountpoint, mountpoint);
116 } else {
117 fprintf(stderr, "fuse: invalid argument `%s'\n", arg);
118 return -1;
119 }
120
121 default:
122 return 1;
123 }
124}
125
126static int add_default_subtype(const char *progname, struct fuse_args *args)
127{
128 int res;
129 char *subtype_opt;
130 const char *basename = strrchr(progname, '/');
131 if (basename == NULL)
132 basename = progname;
133 else if (basename[1] != '\0')
134 basename++;
135
136 subtype_opt = (char *) malloc(strlen(basename) + 64);
137 if (subtype_opt == NULL) {
138 fprintf(stderr, "fuse: memory allocation failed\n");
139 return -1;
140 }
141 sprintf(subtype_opt, "-osubtype=%s", basename);
142 res = fuse_opt_add_arg(args, subtype_opt);
143 free(subtype_opt);
144 return res;
145}
146
147int fuse_parse_cmdline(struct fuse_args *args, char **mountpoint,
148 int *multithreaded, int *foreground)
149{
150 int res;
151 struct helper_opts hopts;
152
153 memset(&hopts, 0, sizeof(hopts));
154 res = fuse_opt_parse(args, &hopts, fuse_helper_opts,
155 fuse_helper_opt_proc);
156 if (res == -1)
157 return -1;
158
159 if (!hopts.nodefault_subtype) {
160 res = add_default_subtype(args->argv[0], args);
161 if (res == -1)
162 goto err;
163 }
164 if (mountpoint)
165 *mountpoint = hopts.mountpoint;
166 else
167 free(hopts.mountpoint);
168
169 if (multithreaded)
170 *multithreaded = !hopts.singlethread;
171 if (foreground)
172 *foreground = hopts.foreground;
173 return 0;
174
175err:
176 free(hopts.mountpoint);
177 return -1;
178}
179
180int fuse_daemonize(int foreground)
181{
182 int res;
183
184 if (!foreground) {
185 res = daemon(0, 0);
186 if (res == -1) {
187 perror("fuse: failed to daemonize program\n");
188 return -1;
189 }
190 }
191 return 0;
192}
193
194static struct fuse_chan *fuse_mount_common(const char *mountpoint,
195 struct fuse_args *args)
196{
197 struct fuse_chan *ch;
198 int fd;
199
200 /*
201 * Make sure file descriptors 0, 1 and 2 are open, otherwise chaos
202 * would ensue.
203 */
204 do {
205 fd = open("/dev/null", O_RDWR);
206 if (fd > 2)
207 close(fd);
208 } while (fd >= 0 && fd <= 2);
209
210 fd = fuse_mount_compat25(mountpoint, args);
211 if (fd == -1)
212 return NULL;
213
214 ch = fuse_kern_chan_new(fd);
215 if (!ch)
216 fuse_kern_unmount(mountpoint, fd);
217
218 return ch;
219}
220
221struct fuse_chan *fuse_mount(const char *mountpoint, struct fuse_args *args)
222{
223 return fuse_mount_common(mountpoint, args);
224}
225
226static void fuse_unmount_common(const char *mountpoint, struct fuse_chan *ch)
227{
228 int fd = ch ? fuse_chan_fd(ch) : -1;
229 fuse_kern_unmount(mountpoint, fd);
230 fuse_chan_destroy(ch);
231}
232
233void fuse_unmount(const char *mountpoint, struct fuse_chan *ch)
234{
235 fuse_unmount_common(mountpoint, ch);
236}
237
238struct fuse *fuse_setup_common(int argc, char *argv[],
239 const struct fuse_operations *op,
240 size_t op_size,
241 char **mountpoint,
242 int *multithreaded,
243 int *fd,
244 void *user_data,
245 int compat)
246{
247 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
248 struct fuse_chan *ch;
249 struct fuse *fuse;
250 int foreground;
251 int res;
252
253 res = fuse_parse_cmdline(&args, mountpoint, multithreaded, &foreground);
254 if (res == -1)
255 return NULL;
256
257 ch = fuse_mount_common(*mountpoint, &args);
258 if (!ch) {
259 fuse_opt_free_args(&args);
260 goto err_free;
261 }
262
263 fuse = fuse_new_common(ch, &args, op, op_size, user_data, compat);
264 fuse_opt_free_args(&args);
265 if (fuse == NULL)
266 goto err_unmount;
267
268 res = fuse_daemonize(foreground);
269 if (res == -1)
270 goto err_unmount;
271
272 res = fuse_set_signal_handlers(fuse_get_session(fuse));
273 if (res == -1)
274 goto err_unmount;
275
276 if (fd)
277 *fd = fuse_chan_fd(ch);
278
279 return fuse;
280
281err_unmount:
282 fuse_unmount_common(*mountpoint, ch);
283 if (fuse)
284 fuse_destroy(fuse);
285err_free:
286 free(*mountpoint);
287 return NULL;
288}
289
290struct fuse *fuse_setup(int argc, char *argv[],
291 const struct fuse_operations *op, size_t op_size,
292 char **mountpoint, int *multithreaded, void *user_data)
293{
294 return fuse_setup_common(argc, argv, op, op_size, mountpoint,
295 multithreaded, NULL, user_data, 0);
296}
297
298static void fuse_teardown_common(struct fuse *fuse, char *mountpoint)
299{
300 struct fuse_session *se = fuse_get_session(fuse);
301 struct fuse_chan *ch = fuse_session_next_chan(se, NULL);
302 fuse_remove_signal_handlers(se);
303 fuse_unmount_common(mountpoint, ch);
304 fuse_destroy(fuse);
305 free(mountpoint);
306}
307
308void fuse_teardown(struct fuse *fuse, char *mountpoint)
309{
310 fuse_teardown_common(fuse, mountpoint);
311}
312
313static int fuse_main_common(int argc, char *argv[],
314 const struct fuse_operations *op, size_t op_size,
315 void *user_data, int compat)
316{
317 struct fuse *fuse;
318 char *mountpoint;
319 int multithreaded;
320 int res;
321
322 fuse = fuse_setup_common(argc, argv, op, op_size, &mountpoint,
323 &multithreaded, NULL, user_data, compat);
324 if (fuse == NULL)
325 return 1;
326
327#ifdef __MULTI_THREAD
328 if (multithreaded)
329 res = fuse_loop_mt(fuse);
330 else
331#endif
332 res = fuse_loop(fuse);
333
334 fuse_teardown_common(fuse, mountpoint);
335 if (res == -1)
336 return 1;
337
338 return 0;
339}
340
341int fuse_main_real(int argc, char *argv[], const struct fuse_operations *op,
342 size_t op_size, void *user_data)
343{
344 return fuse_main_common(argc, argv, op, op_size, user_data, 0);
345}
346
347#undef fuse_main
348int fuse_main(void);
349int fuse_main(void)
350{
351 fprintf(stderr, "fuse_main(): This function does not exist\n");
352 return -1;
353}
354
355int fuse_version(void)
356{
357 return FUSE_VERSION;
358}
359
360#include "fuse_compat.h"
361
362#ifndef __FreeBSD__
363
364struct fuse *fuse_setup_compat22(int argc, char *argv[],
365 const struct fuse_operations_compat22 *op,
366 size_t op_size, char **mountpoint,
367 int *multithreaded, int *fd)
368{
369 return fuse_setup_common(argc, argv, (struct fuse_operations *) op,
370 op_size, mountpoint, multithreaded, fd, NULL,
371 22);
372}
373
374struct fuse *fuse_setup_compat2(int argc, char *argv[],
375 const struct fuse_operations_compat2 *op,
376 char **mountpoint, int *multithreaded,
377 int *fd)
378{
379 return fuse_setup_common(argc, argv, (struct fuse_operations *) op,
380 sizeof(struct fuse_operations_compat2),
381 mountpoint, multithreaded, fd, NULL, 21);
382}
383
384int fuse_main_real_compat22(int argc, char *argv[],
385 const struct fuse_operations_compat22 *op,
386 size_t op_size)
387{
388 return fuse_main_common(argc, argv, (struct fuse_operations *) op,
389 op_size, NULL, 22);
390}
391
392void fuse_main_compat1(int argc, char *argv[],
393 const struct fuse_operations_compat1 *op)
394{
395 fuse_main_common(argc, argv, (struct fuse_operations *) op,
396 sizeof(struct fuse_operations_compat1), NULL, 11);
397}
398
399int fuse_main_compat2(int argc, char *argv[],
400 const struct fuse_operations_compat2 *op)
401{
402 return fuse_main_common(argc, argv, (struct fuse_operations *) op,
403 sizeof(struct fuse_operations_compat2), NULL,
404 21);
405}
406
407int fuse_mount_compat1(const char *mountpoint, const char *args[])
408{
409 /* just ignore mount args for now */
410 (void) args;
411 return fuse_mount_compat22(mountpoint, NULL);
412}
413
414FUSE_SYMVER(".symver fuse_setup_compat2,__fuse_setup@");
415FUSE_SYMVER(".symver fuse_setup_compat22,fuse_setup@FUSE_2.2");
416FUSE_SYMVER(".symver fuse_teardown,__fuse_teardown@");
417FUSE_SYMVER(".symver fuse_main_compat2,fuse_main@");
418FUSE_SYMVER(".symver fuse_main_real_compat22,fuse_main_real@FUSE_2.2");
419
420#endif /* __FreeBSD__ */
421
422
423struct fuse *fuse_setup_compat25(int argc, char *argv[],
424 const struct fuse_operations_compat25 *op,
425 size_t op_size, char **mountpoint,
426 int *multithreaded, int *fd)
427{
428 return fuse_setup_common(argc, argv, (struct fuse_operations *) op,
429 op_size, mountpoint, multithreaded, fd, NULL,
430 25);
431}
432
433int fuse_main_real_compat25(int argc, char *argv[],
434 const struct fuse_operations_compat25 *op,
435 size_t op_size)
436{
437 return fuse_main_common(argc, argv, (struct fuse_operations *) op,
438 op_size, NULL, 25);
439}
440
441void fuse_teardown_compat22(struct fuse *fuse, int fd, char *mountpoint)
442{
443 (void) fd;
444 fuse_teardown_common(fuse, mountpoint);
445}
446
447int fuse_mount_compat25(const char *mountpoint, struct fuse_args *args)
448{
449 return fuse_kern_mount(mountpoint, args);
450}
451
452FUSE_SYMVER(".symver fuse_setup_compat25,fuse_setup@FUSE_2.5");
453FUSE_SYMVER(".symver fuse_teardown_compat22,fuse_teardown@FUSE_2.2");
454FUSE_SYMVER(".symver fuse_main_real_compat25,fuse_main_real@FUSE_2.5");
455FUSE_SYMVER(".symver fuse_mount_compat25,fuse_mount@FUSE_2.5");