libfuse/lib/helper.c

364 lines
10 KiB
C
Raw Normal View History

2001-11-21 18:03:39 +08:00
/*
FUSE: Filesystem in Userspace
2006-01-07 02:29:40 +08:00
Copyright (C) 2001-2006 Miklos Szeredi <miklos@szeredi.hu>
2001-11-21 18:03:39 +08:00
2002-10-25 20:41:16 +08:00
This program can be distributed under the terms of the GNU LGPL.
See the file COPYING.LIB.
2001-11-21 18:03:39 +08:00
*/
2006-01-07 18:14:34 +08:00
#include "config.h"
2005-08-15 21:19:07 +08:00
#include "fuse_i.h"
2005-12-10 01:41:42 +08:00
#include "fuse_opt.h"
2006-01-07 02:29:40 +08:00
#include "fuse_lowlevel.h"
2001-11-21 18:03:39 +08:00
#include <stdio.h>
#include <stdlib.h>
2005-12-10 01:41:42 +08:00
#include <stddef.h>
2001-11-21 18:03:39 +08:00
#include <unistd.h>
2002-12-05 22:23:01 +08:00
#include <string.h>
2001-11-21 18:03:39 +08:00
#include <limits.h>
2005-12-10 01:41:42 +08:00
enum {
KEY_HELP,
KEY_HELP_NOHEADER,
2006-01-07 18:14:34 +08:00
KEY_VERSION,
2005-12-10 01:41:42 +08:00
};
struct helper_opts {
int singlethread;
int foreground;
int fsname;
char *mountpoint;
};
#define FUSE_HELPER_OPT(t, p) { t, offsetof(struct helper_opts, p), 1 }
static const struct fuse_opt fuse_helper_opts[] = {
2006-01-07 02:29:40 +08:00
FUSE_HELPER_OPT("-d", foreground),
FUSE_HELPER_OPT("debug", foreground),
2006-01-07 18:14:34 +08:00
FUSE_HELPER_OPT("-f", foreground),
FUSE_HELPER_OPT("-s", singlethread),
2006-01-07 02:29:40 +08:00
FUSE_HELPER_OPT("fsname=", fsname),
FUSE_OPT_KEY("-h", KEY_HELP),
FUSE_OPT_KEY("--help", KEY_HELP),
FUSE_OPT_KEY("-ho", KEY_HELP_NOHEADER),
2006-01-07 18:14:34 +08:00
FUSE_OPT_KEY("-V", KEY_VERSION),
FUSE_OPT_KEY("--version", KEY_VERSION),
2006-01-20 23:15:21 +08:00
FUSE_OPT_KEY("-d", FUSE_OPT_KEY_KEEP),
FUSE_OPT_KEY("debug", FUSE_OPT_KEY_KEEP),
FUSE_OPT_KEY("fsname=", FUSE_OPT_KEY_KEEP),
2005-12-10 01:41:42 +08:00
FUSE_OPT_END
};
2006-01-07 18:14:34 +08:00
static void usage(const char *progname)
{
fprintf(stderr,
"usage: %s mountpoint [options]\n\n", progname);
fprintf(stderr,
"general options:\n"
" -o opt,[opt...] mount options\n"
" -h --help print help\n"
" -V --version print version\n"
"\n");
}
static void helper_help(void)
{
fprintf(stderr,
"FUSE options:\n"
" -d -o debug enable debug output (implies -f)\n"
" -f foreground operation\n"
" -s disable multi-threaded operation\n"
"\n"
);
}
static void helper_version(void)
{
fprintf(stderr, "FUSE library version: %s\n", PACKAGE_VERSION);
}
2005-12-15 07:25:00 +08:00
static int fuse_helper_opt_proc(void *data, const char *arg, int key,
2005-12-16 19:12:16 +08:00
struct fuse_args *outargs)
2005-01-04 20:45:54 +08:00
{
2005-12-10 01:41:42 +08:00
struct helper_opts *hopts = data;
switch (key) {
case KEY_HELP:
2006-01-07 18:14:34 +08:00
usage(outargs->argv[0]);
/* fall through */
2005-12-10 01:41:42 +08:00
case KEY_HELP_NOHEADER:
2006-01-07 18:14:34 +08:00
helper_help();
return fuse_opt_add_arg(outargs, "-h");
case KEY_VERSION:
helper_version();
return 1;
2005-12-10 01:41:42 +08:00
case FUSE_OPT_KEY_NONOPT:
2006-01-07 02:29:40 +08:00
if (!hopts->mountpoint)
return fuse_opt_add_opt(&hopts->mountpoint, arg);
2006-01-20 23:15:21 +08:00
else {
fprintf(stderr, "fuse: invalid argument `%s'\n", arg);
return -1;
}
2005-12-10 01:41:42 +08:00
2006-01-07 02:29:40 +08:00
default:
return 1;
2004-07-24 01:16:29 +08:00
}
2006-01-07 02:29:40 +08:00
}
2005-12-10 01:41:42 +08:00
2006-01-07 02:29:40 +08:00
static int add_default_fsname(const char *progname, struct fuse_args *args)
{
int res;
char *fsname_opt;
const char *basename = strrchr(progname, '/');
if (basename == NULL)
basename = progname;
else if (basename[1] != '\0')
basename++;
fsname_opt = (char *) malloc(strlen(basename) + 64);
if (fsname_opt == NULL) {
fprintf(stderr, "fuse: memory allocation failed\n");
return -1;
}
sprintf(fsname_opt, "-ofsname=%s", basename);
res = fuse_opt_add_arg(args, fsname_opt);
free(fsname_opt);
return res;
2004-07-24 01:16:29 +08:00
}
2006-01-07 02:29:40 +08:00
int fuse_parse_cmdline(struct fuse_args *args, char **mountpoint,
int *multithreaded, int *foreground)
2004-07-24 01:16:29 +08:00
{
2005-12-10 01:41:42 +08:00
int res;
2006-01-07 02:29:40 +08:00
struct helper_opts hopts;
2005-12-10 01:41:42 +08:00
2006-01-07 02:29:40 +08:00
memset(&hopts, 0, sizeof(hopts));
res = fuse_opt_parse(args, &hopts, fuse_helper_opts, fuse_helper_opt_proc);
2005-12-10 01:41:42 +08:00
if (res == -1)
2004-10-21 17:35:10 +08:00
return -1;
2004-09-16 16:42:40 +08:00
2006-01-07 02:29:40 +08:00
if (!hopts.fsname) {
res = add_default_fsname(args->argv[0], args);
2005-12-11 04:47:46 +08:00
if (res == -1)
2006-01-07 02:29:40 +08:00
goto err;
2004-01-26 19:28:44 +08:00
}
2006-01-07 02:29:40 +08:00
if (mountpoint)
*mountpoint = hopts.mountpoint;
else
free(hopts.mountpoint);
if (multithreaded)
*multithreaded = !hopts.singlethread;
if (foreground)
*foreground = hopts.foreground;
2004-10-21 17:35:10 +08:00
return 0;
2006-01-07 02:29:40 +08:00
err:
free(hopts.mountpoint);
return -1;
2004-10-21 17:35:10 +08:00
}
2006-01-07 16:30:41 +08:00
static int fuse_daemonize(int foreground)
{
int res;
if (!foreground) {
res = daemon(0, 0);
if (res == -1) {
perror("fuse: failed to daemonize program\n");
return -1;
}
} else {
/* Ensure consistant behavior across debug and normal modes */
res = chdir("/");
if (res == -1) {
perror("fuse: failed to change working directory to /\n");
return -1;
}
}
return 0;
}
static struct fuse *fuse_setup_common(int argc, char *argv[],
2004-12-04 08:40:50 +08:00
const struct fuse_operations *op,
size_t op_size,
char **mountpoint,
int *multithreaded,
int *fd,
int compat)
2004-10-21 17:35:10 +08:00
{
2006-01-07 16:30:41 +08:00
struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
2004-10-21 17:35:10 +08:00
struct fuse *fuse;
2006-01-07 02:29:40 +08:00
int foreground;
2004-10-21 17:35:10 +08:00
int res;
2004-12-04 08:40:50 +08:00
2006-01-07 16:30:41 +08:00
res = fuse_parse_cmdline(&args, mountpoint, multithreaded, &foreground);
2004-10-21 17:35:10 +08:00
if (res == -1)
2006-01-07 02:29:40 +08:00
return NULL;
2004-10-21 17:35:10 +08:00
2006-01-07 16:30:41 +08:00
*fd = fuse_mount(*mountpoint, &args);
if (*fd == -1) {
fuse_opt_free_args(&args);
2004-10-21 17:35:10 +08:00
goto err_free;
2006-01-07 16:30:41 +08:00
}
2004-10-21 17:35:10 +08:00
2006-01-07 16:30:41 +08:00
fuse = fuse_new_common(*fd, &args, op, op_size, compat);
fuse_opt_free_args(&args);
2004-10-21 17:35:10 +08:00
if (fuse == NULL)
goto err_unmount;
2006-01-07 16:30:41 +08:00
res = fuse_daemonize(foreground);
if (res == -1)
goto err_destroy;
2004-10-22 00:33:17 +08:00
2006-01-07 02:29:40 +08:00
res = fuse_set_signal_handlers(fuse_get_session(fuse));
2004-10-21 17:35:10 +08:00
if (res == -1)
goto err_destroy;
return fuse;
err_destroy:
fuse_destroy(fuse);
err_unmount:
fuse_unmount(*mountpoint, *fd);
2004-10-21 17:35:10 +08:00
err_free:
2006-01-07 02:29:40 +08:00
free(*mountpoint);
2004-10-21 17:35:10 +08:00
return NULL;
}
2006-01-07 16:30:41 +08:00
struct fuse *fuse_setup(int argc, char *argv[],
2006-01-07 02:29:40 +08:00
const struct fuse_operations *op,
size_t op_size, char **mountpoint,
int *multithreaded, int *fd)
2004-12-04 08:40:50 +08:00
{
2006-01-07 16:30:41 +08:00
return fuse_setup_common(argc, argv, op, op_size, mountpoint,
multithreaded, fd, 0);
2004-12-04 08:40:50 +08:00
}
2004-12-08 00:46:42 +08:00
void fuse_teardown(struct fuse *fuse, int fd, char *mountpoint)
2004-10-21 17:35:10 +08:00
{
2006-01-07 02:29:40 +08:00
fuse_remove_signal_handlers(fuse_get_session(fuse));
fuse_unmount(mountpoint, fd);
2005-11-16 21:00:24 +08:00
fuse_destroy(fuse);
2004-10-21 17:35:10 +08:00
free(mountpoint);
}
2006-01-07 16:30:41 +08:00
static int fuse_main_common(int argc, char *argv[],
2004-12-04 08:40:50 +08:00
const struct fuse_operations *op, size_t op_size,
int compat)
2004-10-21 17:35:10 +08:00
{
2004-10-22 00:33:17 +08:00
struct fuse *fuse;
2004-10-21 17:35:10 +08:00
char *mountpoint;
int multithreaded;
int res;
int fd;
2006-01-07 16:30:41 +08:00
fuse = fuse_setup_common(argc, argv, op, op_size, &mountpoint,
&multithreaded, &fd, compat);
2004-10-22 00:33:17 +08:00
if (fuse == NULL)
2004-10-21 17:35:10 +08:00
return 1;
2005-02-02 19:14:04 +08:00
2004-10-21 17:35:10 +08:00
if (multithreaded)
2004-10-22 00:33:17 +08:00
res = fuse_loop_mt(fuse);
2004-10-21 17:35:10 +08:00
else
2004-10-22 00:33:17 +08:00
res = fuse_loop(fuse);
2005-02-02 19:14:04 +08:00
2004-12-08 00:46:42 +08:00
fuse_teardown(fuse, fd, mountpoint);
2004-10-21 17:35:10 +08:00
if (res == -1)
return 1;
2005-02-02 19:14:04 +08:00
2004-10-21 17:35:10 +08:00
return 0;
2001-11-21 18:03:39 +08:00
}
2004-12-08 00:46:42 +08:00
int fuse_main_real(int argc, char *argv[], const struct fuse_operations *op,
size_t op_size)
2004-12-04 08:40:50 +08:00
{
2006-01-07 16:30:41 +08:00
return fuse_main_common(argc, argv, op, op_size, 0);
2004-12-04 08:40:50 +08:00
}
2005-11-18 01:11:48 +08:00
#undef fuse_main
int fuse_main(void)
{
fprintf(stderr, "fuse_main(): This function does not exist\n");
return -1;
}
#include "fuse_compat.h"
2006-01-20 23:15:21 +08:00
#ifndef __FreeBSD__
2005-11-18 01:11:48 +08:00
struct fuse *fuse_setup_compat22(int argc, char *argv[],
const struct fuse_operations_compat22 *op,
size_t op_size, char **mountpoint,
int *multithreaded, int *fd)
{
2006-01-07 16:30:41 +08:00
return fuse_setup_common(argc, argv, (struct fuse_operations *) op,
op_size, mountpoint, multithreaded, fd, 22);
2005-11-18 01:11:48 +08:00
}
struct fuse *fuse_setup_compat2(int argc, char *argv[],
const struct fuse_operations_compat2 *op,
char **mountpoint, int *multithreaded,
int *fd)
{
2006-01-07 16:30:41 +08:00
return fuse_setup_common(argc, argv, (struct fuse_operations *) op,
sizeof(struct fuse_operations_compat2),
mountpoint, multithreaded, fd, 21);
2005-11-18 01:11:48 +08:00
}
2005-11-12 05:32:42 +08:00
int fuse_main_real_compat22(int argc, char *argv[],
const struct fuse_operations_compat22 *op,
size_t op_size)
{
2006-01-07 16:30:41 +08:00
return fuse_main_common(argc, argv, (struct fuse_operations *) op, op_size,
22);
2005-11-12 05:32:42 +08:00
}
2004-12-08 00:46:42 +08:00
void fuse_main_compat1(int argc, char *argv[],
const struct fuse_operations_compat1 *op)
2004-12-04 08:40:50 +08:00
{
2006-01-07 16:30:41 +08:00
fuse_main_common(argc, argv, (struct fuse_operations *) op,
2004-12-08 00:46:42 +08:00
sizeof(struct fuse_operations_compat1), 11);
2004-12-04 08:40:50 +08:00
}
2004-12-08 00:46:42 +08:00
int fuse_main_compat2(int argc, char *argv[],
const struct fuse_operations_compat2 *op)
2004-12-04 08:40:50 +08:00
{
2006-01-07 16:30:41 +08:00
return fuse_main_common(argc, argv, (struct fuse_operations *) op,
sizeof(struct fuse_operations_compat2), 21);
2004-12-04 08:40:50 +08:00
}
2004-12-08 00:46:42 +08:00
__asm__(".symver fuse_setup_compat2,__fuse_setup@");
2005-11-12 05:32:42 +08:00
__asm__(".symver fuse_setup_compat22,fuse_setup@FUSE_2.2");
2004-12-08 00:46:42 +08:00
__asm__(".symver fuse_teardown,__fuse_teardown@");
__asm__(".symver fuse_main_compat2,fuse_main@");
2005-11-12 05:32:42 +08:00
__asm__(".symver fuse_main_real_compat22,fuse_main_real@FUSE_2.2");
2005-11-18 01:11:48 +08:00
#endif /* __FreeBSD__ */
2006-01-20 23:15:21 +08:00
struct fuse *fuse_setup_compat25(int argc, char *argv[],
const struct fuse_operations_compat25 *op,
size_t op_size, char **mountpoint,
int *multithreaded, int *fd)
{
return fuse_setup_common(argc, argv, (struct fuse_operations *) op,
op_size, mountpoint, multithreaded, fd, 25);
}
int fuse_main_real_compat25(int argc, char *argv[],
const struct fuse_operations_compat25 *op,
size_t op_size)
{
return fuse_main_common(argc, argv, (struct fuse_operations *) op, op_size,
25);
}
__asm__(".symver fuse_setup_compat25,fuse_setup@FUSE_2.5");
__asm__(".symver fuse_main_real_compat25,fuse_main_real@FUSE_2.5");