2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
5 This program can be distributed under the terms of the GNU LGPLv2.
6 See the file COPYING.LIB.
11 #include "fuse_misc.h"
13 #include "fuse_lowlevel.h"
14 #include "fuse_common_compat.h"
23 #include <sys/param.h>
34 int nodefault_subtype
;
38 #define FUSE_HELPER_OPT(t, p) { t, offsetof(struct helper_opts, p), 1 }
40 static 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
),
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
),
60 static void usage(const char *progname
)
63 "usage: %s mountpoint [options]\n\n", progname
);
66 " -o opt,[opt...] mount options\n"
67 " -h --help print help\n"
68 " -V --version print version\n"
72 static void helper_help(void)
76 " -d -o debug enable debug output (implies -f)\n"
77 " -f foreground operation\n"
78 " -s disable multi-threaded operation\n"
83 static void helper_version(void)
85 fprintf(stderr
, "FUSE library version: %s\n", PACKAGE_VERSION
);
88 static int fuse_helper_opt_proc(void *data
, const char *arg
, int key
,
89 struct fuse_args
*outargs
)
91 struct helper_opts
*hopts
= data
;
95 usage(outargs
->argv
[0]);
98 case KEY_HELP_NOHEADER
:
100 return fuse_opt_add_arg(outargs
, "-h");
106 case FUSE_OPT_KEY_NONOPT
:
107 if (!hopts
->mountpoint
) {
108 char mountpoint
[PATH_MAX
];
109 if (realpath(arg
, mountpoint
) == NULL
) {
111 "fuse: bad mount point `%s': %s\n",
112 arg
, strerror(errno
));
115 return fuse_opt_add_opt(&hopts
->mountpoint
, mountpoint
);
117 fprintf(stderr
, "fuse: invalid argument `%s'\n", arg
);
126 static int add_default_subtype(const char *progname
, struct fuse_args
*args
)
130 const char *basename
= strrchr(progname
, '/');
131 if (basename
== NULL
)
133 else if (basename
[1] != '\0')
136 subtype_opt
= (char *) malloc(strlen(basename
) + 64);
137 if (subtype_opt
== NULL
) {
138 fprintf(stderr
, "fuse: memory allocation failed\n");
141 sprintf(subtype_opt
, "-osubtype=%s", basename
);
142 res
= fuse_opt_add_arg(args
, subtype_opt
);
147 int fuse_parse_cmdline(struct fuse_args
*args
, char **mountpoint
,
148 int *multithreaded
, int *foreground
)
151 struct helper_opts hopts
;
153 memset(&hopts
, 0, sizeof(hopts
));
154 res
= fuse_opt_parse(args
, &hopts
, fuse_helper_opts
,
155 fuse_helper_opt_proc
);
159 if (!hopts
.nodefault_subtype
) {
160 res
= add_default_subtype(args
->argv
[0], args
);
165 *mountpoint
= hopts
.mountpoint
;
167 free(hopts
.mountpoint
);
170 *multithreaded
= !hopts
.singlethread
;
172 *foreground
= hopts
.foreground
;
176 free(hopts
.mountpoint
);
180 int fuse_daemonize(int foreground
)
187 perror("fuse: failed to daemonize program\n");
194 static struct fuse_chan
*fuse_mount_common(const char *mountpoint
,
195 struct fuse_args
*args
)
197 struct fuse_chan
*ch
;
201 * Make sure file descriptors 0, 1 and 2 are open, otherwise chaos
205 fd
= open("/dev/null", O_RDWR
);
208 } while (fd
>= 0 && fd
<= 2);
210 fd
= fuse_mount_compat25(mountpoint
, args
);
214 ch
= fuse_kern_chan_new(fd
);
216 fuse_kern_unmount(mountpoint
, fd
);
221 struct fuse_chan
*fuse_mount(const char *mountpoint
, struct fuse_args
*args
)
223 return fuse_mount_common(mountpoint
, args
);
226 static void fuse_unmount_common(const char *mountpoint
, struct fuse_chan
*ch
)
228 int fd
= ch
? fuse_chan_fd(ch
) : -1;
229 fuse_kern_unmount(mountpoint
, fd
);
230 fuse_chan_destroy(ch
);
233 void fuse_unmount(const char *mountpoint
, struct fuse_chan
*ch
)
235 fuse_unmount_common(mountpoint
, ch
);
238 struct fuse
*fuse_setup_common(int argc
, char *argv
[],
239 const struct fuse_operations
*op
,
247 struct fuse_args args
= FUSE_ARGS_INIT(argc
, argv
);
248 struct fuse_chan
*ch
;
253 res
= fuse_parse_cmdline(&args
, mountpoint
, multithreaded
, &foreground
);
257 ch
= fuse_mount_common(*mountpoint
, &args
);
259 fuse_opt_free_args(&args
);
263 fuse
= fuse_new_common(ch
, &args
, op
, op_size
, user_data
, compat
);
264 fuse_opt_free_args(&args
);
268 res
= fuse_daemonize(foreground
);
272 res
= fuse_set_signal_handlers(fuse_get_session(fuse
));
277 *fd
= fuse_chan_fd(ch
);
282 fuse_unmount_common(*mountpoint
, ch
);
290 struct 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
)
294 return fuse_setup_common(argc
, argv
, op
, op_size
, mountpoint
,
295 multithreaded
, NULL
, user_data
, 0);
298 static void fuse_teardown_common(struct fuse
*fuse
, char *mountpoint
)
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
);
308 void fuse_teardown(struct fuse
*fuse
, char *mountpoint
)
310 fuse_teardown_common(fuse
, mountpoint
);
313 static int fuse_main_common(int argc
, char *argv
[],
314 const struct fuse_operations
*op
, size_t op_size
,
315 void *user_data
, int compat
)
322 fuse
= fuse_setup_common(argc
, argv
, op
, op_size
, &mountpoint
,
323 &multithreaded
, NULL
, user_data
, compat
);
328 res
= fuse_loop_mt(fuse
);
330 res
= fuse_loop(fuse
);
332 fuse_teardown_common(fuse
, mountpoint
);
339 int fuse_main_real(int argc
, char *argv
[], const struct fuse_operations
*op
,
340 size_t op_size
, void *user_data
)
342 return fuse_main_common(argc
, argv
, op
, op_size
, user_data
, 0);
349 fprintf(stderr
, "fuse_main(): This function does not exist\n");
353 int fuse_version(void)
358 #include "fuse_compat.h"
360 #if !defined(__FreeBSD__) && !defined(__NetBSD__)
362 struct fuse
*fuse_setup_compat22(int argc
, char *argv
[],
363 const struct fuse_operations_compat22
*op
,
364 size_t op_size
, char **mountpoint
,
365 int *multithreaded
, int *fd
)
367 return fuse_setup_common(argc
, argv
, (struct fuse_operations
*) op
,
368 op_size
, mountpoint
, multithreaded
, fd
, NULL
,
372 struct fuse
*fuse_setup_compat2(int argc
, char *argv
[],
373 const struct fuse_operations_compat2
*op
,
374 char **mountpoint
, int *multithreaded
,
377 return fuse_setup_common(argc
, argv
, (struct fuse_operations
*) op
,
378 sizeof(struct fuse_operations_compat2
),
379 mountpoint
, multithreaded
, fd
, NULL
, 21);
382 int fuse_main_real_compat22(int argc
, char *argv
[],
383 const struct fuse_operations_compat22
*op
,
386 return fuse_main_common(argc
, argv
, (struct fuse_operations
*) op
,
390 void fuse_main_compat1(int argc
, char *argv
[],
391 const struct fuse_operations_compat1
*op
)
393 fuse_main_common(argc
, argv
, (struct fuse_operations
*) op
,
394 sizeof(struct fuse_operations_compat1
), NULL
, 11);
397 int fuse_main_compat2(int argc
, char *argv
[],
398 const struct fuse_operations_compat2
*op
)
400 return fuse_main_common(argc
, argv
, (struct fuse_operations
*) op
,
401 sizeof(struct fuse_operations_compat2
), NULL
,
405 int fuse_mount_compat1(const char *mountpoint
, const char *args
[])
407 /* just ignore mount args for now */
409 return fuse_mount_compat22(mountpoint
, NULL
);
412 FUSE_SYMVER(".symver fuse_setup_compat2,__fuse_setup@");
413 FUSE_SYMVER(".symver fuse_setup_compat22,fuse_setup@FUSE_2.2");
414 FUSE_SYMVER(".symver fuse_teardown,__fuse_teardown@");
415 FUSE_SYMVER(".symver fuse_main_compat2,fuse_main@");
416 FUSE_SYMVER(".symver fuse_main_real_compat22,fuse_main_real@FUSE_2.2");
418 #endif /* __FreeBSD__ || __NetBSD__ */
421 struct fuse
*fuse_setup_compat25(int argc
, char *argv
[],
422 const struct fuse_operations_compat25
*op
,
423 size_t op_size
, char **mountpoint
,
424 int *multithreaded
, int *fd
)
426 return fuse_setup_common(argc
, argv
, (struct fuse_operations
*) op
,
427 op_size
, mountpoint
, multithreaded
, fd
, NULL
,
431 int fuse_main_real_compat25(int argc
, char *argv
[],
432 const struct fuse_operations_compat25
*op
,
435 return fuse_main_common(argc
, argv
, (struct fuse_operations
*) op
,
439 void fuse_teardown_compat22(struct fuse
*fuse
, int fd
, char *mountpoint
)
442 fuse_teardown_common(fuse
, mountpoint
);
445 int fuse_mount_compat25(const char *mountpoint
, struct fuse_args
*args
)
447 return fuse_kern_mount(mountpoint
, args
);
450 FUSE_SYMVER(".symver fuse_setup_compat25,fuse_setup@FUSE_2.5");
451 FUSE_SYMVER(".symver fuse_teardown_compat22,fuse_teardown@FUSE_2.2");
452 FUSE_SYMVER(".symver fuse_main_real_compat25,fuse_main_real@FUSE_2.5");
453 FUSE_SYMVER(".symver fuse_mount_compat25,fuse_mount@FUSE_2.5");