libfuse/lib/helper.c

327 lines
8.6 KiB
C
Raw Normal View History

2001-11-21 18:03:39 +08:00
/*
FUSE: Filesystem in Userspace
2004-07-08 03:19:53 +08:00
Copyright (C) 2001-2004 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
*/
#include "fuse.h"
#include <stdio.h>
#include <stdlib.h>
#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>
#include <signal.h>
2004-10-21 17:35:10 +08:00
static struct fuse *fuse_obj;
2001-11-21 18:03:39 +08:00
2004-10-21 17:35:10 +08:00
static void usage(const char *progname)
2001-11-21 18:03:39 +08:00
{
fprintf(stderr,
2004-07-24 01:16:29 +08:00
"usage: %s mountpoint [options]\n"
2001-11-21 18:03:39 +08:00
"Options:\n"
2004-07-24 01:16:29 +08:00
" -d enable debug output (implies -f)\n"
" -f foreground operation\n"
" -s disable multithreaded operation\n"
" -o opt,[opt...] mount options\n"
" -h print help\n"
2004-01-26 19:28:44 +08:00
"\n"
2004-07-24 01:16:29 +08:00
"Mount options:\n"
" default_permissions enable permission checking\n"
" allow_other allow access to other users\n"
" kernel_cache cache files in kernel\n"
" large_read issue large read requests (2.4 only)\n"
" direct_io use direct I/O\n"
" max_read=N set maximum size of read requests\n"
" hard_remove immediate removal (don't hide files)\n"
" debug enable debug output\n"
" fsname=NAME set filesystem name in mtab\n",
2001-11-21 18:03:39 +08:00
progname);
}
2004-10-21 17:35:10 +08:00
static void invalid_option(const char *argv[], int argctr)
2004-01-26 19:28:44 +08:00
{
2004-10-21 17:35:10 +08:00
fprintf(stderr, "fuse: invalid option: %s\n\n", argv[argctr]);
2004-01-26 19:28:44 +08:00
usage(argv[0]);
}
2001-11-21 18:03:39 +08:00
static void exit_handler()
{
2004-10-21 17:35:10 +08:00
if (fuse_obj != NULL)
fuse_exit(fuse_obj);
2001-11-21 18:03:39 +08:00
}
2004-10-21 17:35:10 +08:00
static int set_one_signal_handler(int signal, void (*handler)(int))
2001-11-21 18:03:39 +08:00
{
struct sigaction sa;
2004-07-03 00:20:45 +08:00
struct sigaction old_sa;
2001-11-21 18:03:39 +08:00
2004-07-17 02:27:50 +08:00
memset(&sa, 0, sizeof(struct sigaction));
2004-07-03 00:20:45 +08:00
sa.sa_handler = handler;
2001-11-21 18:03:39 +08:00
sigemptyset(&(sa.sa_mask));
sa.sa_flags = 0;
2004-07-03 00:20:45 +08:00
if (sigaction(signal, NULL, &old_sa) == -1) {
perror("FUSE: cannot get old signal handler");
2004-10-21 17:35:10 +08:00
return -1;
2001-11-21 18:03:39 +08:00
}
2004-07-03 00:20:45 +08:00
if (old_sa.sa_handler == SIG_DFL &&
sigaction(signal, &sa, NULL) == -1) {
perror("Cannot set signal handler");
2004-10-21 17:35:10 +08:00
return -1;
2001-11-21 18:03:39 +08:00
}
2004-10-21 17:35:10 +08:00
return 0;
2001-11-21 18:03:39 +08:00
}
2004-10-21 17:35:10 +08:00
static int set_signal_handlers()
{
2004-10-21 17:35:10 +08:00
if (set_one_signal_handler(SIGHUP, exit_handler) == -1 ||
set_one_signal_handler(SIGINT, exit_handler) == -1 ||
set_one_signal_handler(SIGTERM, exit_handler) == -1 ||
set_one_signal_handler(SIGPIPE, SIG_IGN) == -1)
return -1;
2004-09-16 16:43:24 +08:00
return 0;
}
2004-09-16 16:42:40 +08:00
static int add_option_to(const char *opt, char **optp)
2004-07-24 01:16:29 +08:00
{
unsigned len = strlen(opt);
if (*optp) {
unsigned oldlen = strlen(*optp);
*optp = realloc(*optp, oldlen + 1 + len + 1);
2004-09-16 16:42:40 +08:00
if (*optp == NULL)
return -1;
2004-07-24 01:16:29 +08:00
(*optp)[oldlen] = ',';
strcpy(*optp + oldlen + 1, opt);
} else {
*optp = malloc(len + 1);
2004-09-16 16:42:40 +08:00
if (*optp == NULL)
return -1;
2004-07-24 01:16:29 +08:00
strcpy(*optp, opt);
}
2004-09-16 16:42:40 +08:00
return 0;
2004-07-24 01:16:29 +08:00
}
2004-10-21 17:35:10 +08:00
static int add_options(char **lib_optp, char **kernel_optp, const char *opts)
2004-07-24 01:16:29 +08:00
{
char *xopts = strdup(opts);
char *s = xopts;
char *opt;
2004-09-16 16:42:40 +08:00
if (xopts == NULL) {
fprintf(stderr, "fuse: memory allocation failed\n");
2004-10-21 17:35:10 +08:00
return -1;
2004-09-16 16:42:40 +08:00
}
2004-07-24 01:16:29 +08:00
while((opt = strsep(&s, ",")) != NULL) {
2004-09-16 16:42:40 +08:00
int res;
2004-07-24 01:16:29 +08:00
if (fuse_is_lib_option(opt))
2004-09-16 16:42:40 +08:00
res = add_option_to(opt, lib_optp);
2004-07-24 01:16:29 +08:00
else
2004-09-16 16:42:40 +08:00
res = add_option_to(opt, kernel_optp);
if (res == -1) {
fprintf(stderr, "fuse: memory allocation failed\n");
2004-10-21 17:35:10 +08:00
return -1;
2004-09-16 16:42:40 +08:00
}
2004-07-24 01:16:29 +08:00
}
free(xopts);
2004-10-21 17:35:10 +08:00
return 0;
2004-07-24 01:16:29 +08:00
}
2004-10-21 17:35:10 +08:00
static int fuse_parse_cmdline(int argc, const char *argv[], char **kernel_opts,
char **lib_opts, char **mountpoint,
int *multithreaded, int *background)
2001-11-21 18:03:39 +08:00
{
2004-10-21 17:35:10 +08:00
int res;
2004-02-25 16:39:42 +08:00
int argctr;
2004-10-21 17:35:10 +08:00
const char *basename;
2004-07-24 01:16:29 +08:00
char *fsname_opt;
2004-10-21 17:35:10 +08:00
*kernel_opts = NULL;
*lib_opts = NULL;
*mountpoint = NULL;
*multithreaded = 1;
*background = 1;
2004-07-24 01:16:29 +08:00
basename = strrchr(argv[0], '/');
if (basename == NULL)
basename = argv[0];
else if (basename[1] != '\0')
basename++;
fsname_opt = malloc(strlen(basename) + 64);
2004-09-16 16:42:40 +08:00
if (fsname_opt == NULL) {
fprintf(stderr, "fuse: memory allocation failed\n");
2004-10-21 17:35:10 +08:00
return -1;
2004-09-16 16:42:40 +08:00
}
2004-07-24 01:16:29 +08:00
sprintf(fsname_opt, "fsname=%s", basename);
2004-10-21 17:35:10 +08:00
res = add_options(lib_opts, kernel_opts, fsname_opt);
2004-07-24 01:16:29 +08:00
free(fsname_opt);
2004-10-21 17:35:10 +08:00
if (res == -1)
goto err;
2004-01-26 19:28:44 +08:00
2004-07-24 01:16:29 +08:00
for (argctr = 1; argctr < argc; argctr ++) {
if (argv[argctr][0] == '-') {
if (strlen(argv[argctr]) == 2)
switch (argv[argctr][1]) {
2004-07-24 01:16:29 +08:00
case 'o':
if (argctr + 1 == argc || argv[argctr+1][0] == '-') {
fprintf(stderr, "missing option after -o\n\n");
usage(argv[0]);
2004-10-21 17:35:10 +08:00
goto err;
2004-07-24 01:16:29 +08:00
}
argctr ++;
2004-10-21 17:35:10 +08:00
res = add_options(lib_opts, kernel_opts, argv[argctr]);
if (res == -1)
goto err;
2004-07-24 01:16:29 +08:00
break;
2004-02-25 16:39:42 +08:00
case 'd':
2004-10-21 17:35:10 +08:00
res = add_options(lib_opts, kernel_opts, "debug");
if (res == -1)
goto err;
*background = 0;
2004-02-25 16:39:42 +08:00
break;
case 'f':
2004-10-21 17:35:10 +08:00
*background = 0;
break;
2004-02-25 16:39:42 +08:00
case 's':
2004-10-21 17:35:10 +08:00
*multithreaded = 0;
2004-02-25 16:39:42 +08:00
break;
case 'h':
usage(argv[0]);
2004-10-21 17:35:10 +08:00
goto err;
2004-02-25 16:39:42 +08:00
default:
invalid_option(argv, argctr);
2004-10-21 17:35:10 +08:00
goto err;
2004-02-25 16:39:42 +08:00
}
2004-07-24 01:16:29 +08:00
else {
2004-10-21 17:35:10 +08:00
if (argv[argctr][1] == 'o') {
res = add_options(lib_opts, kernel_opts, &argv[argctr][2]);
if (res == -1)
goto err;
}
else {
2004-07-24 01:16:29 +08:00
invalid_option(argv, argctr);
2004-10-21 17:35:10 +08:00
goto err;
}
2004-07-24 01:16:29 +08:00
}
2004-10-21 17:35:10 +08:00
} else if (*mountpoint == NULL) {
*mountpoint = strdup(argv[argctr]);
if (*mountpoint == NULL) {
2004-09-16 16:42:40 +08:00
fprintf(stderr, "fuse: memory allocation failed\n");
2004-10-21 17:35:10 +08:00
goto err;
2004-09-16 16:42:40 +08:00
}
}
2004-10-21 17:35:10 +08:00
else {
2004-01-26 19:28:44 +08:00
invalid_option(argv, argctr);
2004-10-21 17:35:10 +08:00
goto err;
}
2004-01-26 19:28:44 +08:00
}
2004-10-21 17:35:10 +08:00
if (*mountpoint == NULL) {
2004-07-24 01:16:29 +08:00
fprintf(stderr, "missing mountpoint\n\n");
2004-02-25 16:39:42 +08:00
usage(argv[0]);
2004-10-21 17:35:10 +08:00
goto err;
2004-02-25 16:39:42 +08:00
}
2004-10-21 17:35:10 +08:00
return 0;
err:
free(*kernel_opts);
free(*lib_opts);
free(*mountpoint);
return -1;
}
struct fuse *__fuse_setup(int argc, char *argv[],
const struct fuse_operations *op,
char **mountpoint, int *multithreaded, int *fd)
{
struct fuse *fuse;
int background;
char *kernel_opts;
char *lib_opts;
int res;
res = fuse_parse_cmdline(argc, (const char **) argv, &kernel_opts,
&lib_opts, mountpoint, multithreaded,
&background);
if (res == -1)
return NULL;
*fd = fuse_mount(*mountpoint, kernel_opts);
if (*fd == -1)
goto err_free;
fuse = fuse_new(*fd, lib_opts, op);
if (fuse == NULL)
goto err_unmount;
if (background) {
res = daemon(0, 0);
if (res == -1) {
perror("fuse: failed to daemonize program\n");
goto err_destroy;
}
}
res = set_signal_handlers();
if (res == -1)
goto err_destroy;
free(kernel_opts);
free(lib_opts);
return fuse;
err_destroy:
fuse_destroy(fuse);
err_unmount:
fuse_unmount(*mountpoint);
err_free:
free(kernel_opts);
free(lib_opts);
free(*mountpoint);
return NULL;
}
void __fuse_teardown(struct fuse *fuse, int fd, char *mountpoint)
{
fuse_destroy(fuse);
close(fd);
fuse_unmount(mountpoint);
free(mountpoint);
}
int fuse_main(int argc, char *argv[], const struct fuse_operations *op)
{
char *mountpoint;
int multithreaded;
int res;
int fd;
fuse_obj = __fuse_setup(argc, argv, op, &mountpoint, &multithreaded, &fd);
if (fuse_obj == NULL)
return 1;
if (multithreaded)
res = fuse_loop_mt(fuse_obj);
else
res = fuse_loop(fuse_obj);
2004-03-03 00:18:30 +08:00
2004-10-21 17:35:10 +08:00
__fuse_teardown(fuse_obj, fd, mountpoint);
if (res == -1)
return 1;
return 0;
2001-11-21 18:03:39 +08:00
}