Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | FUSE: Filesystem in Userspace |
Miklos Szeredi | 149f607 | 2005-01-10 12:29:28 +0000 | [diff] [blame] | 3 | Copyright (C) 2001-2005 Miklos Szeredi <miklos@szeredi.hu> |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 4 | |
Miklos Szeredi | 8b39a9f | 2002-10-25 12:41:16 +0000 | [diff] [blame] | 5 | This program can be distributed under the terms of the GNU LGPL. |
| 6 | See the file COPYING.LIB. |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Miklos Szeredi | 178451d | 2005-08-15 13:19:07 +0000 | [diff] [blame] | 9 | #include "fuse_i.h" |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 10 | |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 13 | #include <unistd.h> |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 14 | #include <string.h> |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 15 | #include <limits.h> |
| 16 | #include <signal.h> |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 17 | |
Miklos Szeredi | e175fb7 | 2004-10-21 16:33:17 +0000 | [diff] [blame] | 18 | static struct fuse *fuse_instance; |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 19 | |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 20 | static void usage(const char *progname) |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 21 | { |
Miklos Szeredi | 799993c | 2004-12-04 21:20:05 +0000 | [diff] [blame] | 22 | if (progname) |
| 23 | fprintf(stderr, |
| 24 | "usage: %s mountpoint [FUSE options]\n\n", progname); |
| 25 | |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 26 | fprintf(stderr, |
Miklos Szeredi | 799993c | 2004-12-04 21:20:05 +0000 | [diff] [blame] | 27 | "FUSE options:\n" |
Miklos Szeredi | fbb5c74 | 2005-02-09 15:47:51 +0000 | [diff] [blame] | 28 | " -d enable debug output (implies -f)\n" |
| 29 | " -f foreground operation\n" |
Miklos Szeredi | b75d4b9 | 2005-10-11 10:12:08 +0000 | [diff] [blame] | 30 | " -s disable multi-threaded operation\n" |
Miklos Szeredi | fbb5c74 | 2005-02-09 15:47:51 +0000 | [diff] [blame] | 31 | " -r mount read only (equivalent to '-o ro')\n" |
| 32 | " -o opt,[opt...] mount options\n" |
| 33 | " -h print help\n" |
Miklos Szeredi | 307242f | 2004-01-26 11:28:44 +0000 | [diff] [blame] | 34 | "\n" |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 35 | "Mount options:\n" |
| 36 | " default_permissions enable permission checking\n" |
| 37 | " allow_other allow access to other users\n" |
Miklos Szeredi | 3c7d41b | 2005-01-09 20:05:27 +0000 | [diff] [blame] | 38 | " allow_root allow access to root\n" |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 39 | " kernel_cache cache files in kernel\n" |
| 40 | " large_read issue large read requests (2.4 only)\n" |
| 41 | " direct_io use direct I/O\n" |
| 42 | " max_read=N set maximum size of read requests\n" |
| 43 | " hard_remove immediate removal (don't hide files)\n" |
| 44 | " debug enable debug output\n" |
Miklos Szeredi | 830ef70 | 2005-02-10 19:39:34 +0000 | [diff] [blame] | 45 | " fsname=NAME set filesystem name in mtab\n" |
Miklos Szeredi | 33be22d | 2005-05-27 09:12:43 +0000 | [diff] [blame] | 46 | " use_ino let filesystem set inode numbers\n" |
| 47 | " readdir_ino try to fill in d_ino in readdir\n" |
Miklos Szeredi | 340d21f | 2005-07-06 10:07:52 +0000 | [diff] [blame] | 48 | " nonempty allow mounts over non-empty file/dir\n" |
Miklos Szeredi | e331c4b | 2005-07-06 13:34:02 +0000 | [diff] [blame] | 49 | " umask=M set file permissions (octal)\n" |
| 50 | " uid=N set file owner\n" |
| 51 | " gid=N set file group\n" |
Miklos Szeredi | 33be22d | 2005-05-27 09:12:43 +0000 | [diff] [blame] | 52 | ); |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 55 | static void invalid_option(const char *argv[], int argctr) |
Miklos Szeredi | 307242f | 2004-01-26 11:28:44 +0000 | [diff] [blame] | 56 | { |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 57 | fprintf(stderr, "fuse: invalid option: %s\n\n", argv[argctr]); |
Miklos Szeredi | d59bb9d | 2004-12-07 10:04:24 +0000 | [diff] [blame] | 58 | fprintf(stderr, "see `%s -h' for usage\n", argv[0]); |
Miklos Szeredi | 307242f | 2004-01-26 11:28:44 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 61 | static void exit_handler(int sig) |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 62 | { |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 63 | (void) sig; |
Miklos Szeredi | e175fb7 | 2004-10-21 16:33:17 +0000 | [diff] [blame] | 64 | if (fuse_instance != NULL) |
| 65 | fuse_exit(fuse_instance); |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 68 | static int set_one_signal_handler(int sig, void (*handler)(int)) |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 69 | { |
| 70 | struct sigaction sa; |
Miklos Szeredi | acb4d36 | 2004-07-02 16:20:45 +0000 | [diff] [blame] | 71 | struct sigaction old_sa; |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 72 | |
Miklos Szeredi | e7d5d7d | 2004-07-16 18:27:50 +0000 | [diff] [blame] | 73 | memset(&sa, 0, sizeof(struct sigaction)); |
Miklos Szeredi | acb4d36 | 2004-07-02 16:20:45 +0000 | [diff] [blame] | 74 | sa.sa_handler = handler; |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 75 | sigemptyset(&(sa.sa_mask)); |
| 76 | sa.sa_flags = 0; |
| 77 | |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 78 | if (sigaction(sig, NULL, &old_sa) == -1) { |
Miklos Szeredi | acb4d36 | 2004-07-02 16:20:45 +0000 | [diff] [blame] | 79 | perror("FUSE: cannot get old signal handler"); |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 80 | return -1; |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 81 | } |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 82 | |
Miklos Szeredi | acb4d36 | 2004-07-02 16:20:45 +0000 | [diff] [blame] | 83 | if (old_sa.sa_handler == SIG_DFL && |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 84 | sigaction(sig, &sa, NULL) == -1) { |
Miklos Szeredi | acb4d36 | 2004-07-02 16:20:45 +0000 | [diff] [blame] | 85 | perror("Cannot set signal handler"); |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 86 | return -1; |
Miklos Szeredi | acb4d36 | 2004-07-02 16:20:45 +0000 | [diff] [blame] | 87 | } |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 88 | return 0; |
Miklos Szeredi | acb4d36 | 2004-07-02 16:20:45 +0000 | [diff] [blame] | 89 | } |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 90 | |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 91 | static int set_signal_handlers(void) |
Miklos Szeredi | acb4d36 | 2004-07-02 16:20:45 +0000 | [diff] [blame] | 92 | { |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 93 | if (set_one_signal_handler(SIGHUP, exit_handler) == -1 || |
| 94 | set_one_signal_handler(SIGINT, exit_handler) == -1 || |
| 95 | set_one_signal_handler(SIGTERM, exit_handler) == -1 || |
| 96 | set_one_signal_handler(SIGPIPE, SIG_IGN) == -1) |
| 97 | return -1; |
Miklos Szeredi | ba1e2dc | 2004-09-16 08:43:24 +0000 | [diff] [blame] | 98 | |
Miklos Szeredi | ff87535 | 2004-06-03 13:52:40 +0000 | [diff] [blame] | 99 | return 0; |
| 100 | } |
| 101 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 102 | static int opt_member(const char *opts, const char *opt) |
| 103 | { |
| 104 | const char *e, *s = opts; |
| 105 | int optlen = strlen(opt); |
| 106 | for (s = opts; s; s = e + 1) { |
| 107 | if(!(e = strchr(s, ','))) |
| 108 | break; |
| 109 | if (e - s == optlen && strncmp(s, opt, optlen) == 0) |
| 110 | return 1; |
| 111 | } |
| 112 | return (s && strcmp(s, opt) == 0); |
| 113 | } |
| 114 | |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 115 | static int add_option_to(const char *opt, char **optp) |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 116 | { |
| 117 | unsigned len = strlen(opt); |
| 118 | if (*optp) { |
| 119 | unsigned oldlen = strlen(*optp); |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 120 | *optp = (char *) realloc(*optp, oldlen + 1 + len + 1); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 121 | if (*optp == NULL) |
| 122 | return -1; |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 123 | (*optp)[oldlen] = ','; |
| 124 | strcpy(*optp + oldlen + 1, opt); |
| 125 | } else { |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 126 | *optp = (char *) malloc(len + 1); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 127 | if (*optp == NULL) |
| 128 | return -1; |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 129 | strcpy(*optp, opt); |
| 130 | } |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 131 | return 0; |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 134 | static int add_options(char **lib_optp, char **kernel_optp, const char *opts) |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 135 | { |
| 136 | char *xopts = strdup(opts); |
| 137 | char *s = xopts; |
| 138 | char *opt; |
Miklos Szeredi | c902a85 | 2005-07-07 12:35:37 +0000 | [diff] [blame] | 139 | int has_allow_other = 0; |
| 140 | int has_allow_root = 0; |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 141 | |
| 142 | if (xopts == NULL) { |
| 143 | fprintf(stderr, "fuse: memory allocation failed\n"); |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 144 | return -1; |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 147 | while((opt = strsep(&s, ",")) != NULL) { |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 148 | int res; |
Miklos Szeredi | 0111f9d | 2005-04-22 12:04:55 +0000 | [diff] [blame] | 149 | if (fuse_is_lib_option(opt)) { |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 150 | res = add_option_to(opt, lib_optp); |
Miklos Szeredi | 0111f9d | 2005-04-22 12:04:55 +0000 | [diff] [blame] | 151 | /* Compatibility hack */ |
Miklos Szeredi | c902a85 | 2005-07-07 12:35:37 +0000 | [diff] [blame] | 152 | if (strcmp(opt, "allow_root") == 0 && res != -1) { |
| 153 | has_allow_root = 1; |
Miklos Szeredi | 0111f9d | 2005-04-22 12:04:55 +0000 | [diff] [blame] | 154 | res = add_option_to("allow_other", kernel_optp); |
Miklos Szeredi | c902a85 | 2005-07-07 12:35:37 +0000 | [diff] [blame] | 155 | } |
Miklos Szeredi | 0111f9d | 2005-04-22 12:04:55 +0000 | [diff] [blame] | 156 | } |
Miklos Szeredi | c902a85 | 2005-07-07 12:35:37 +0000 | [diff] [blame] | 157 | else { |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 158 | res = add_option_to(opt, kernel_optp); |
Miklos Szeredi | c902a85 | 2005-07-07 12:35:37 +0000 | [diff] [blame] | 159 | if (strcmp(opt, "allow_other") == 0) |
| 160 | has_allow_other = 1; |
| 161 | } |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 162 | if (res == -1) { |
| 163 | fprintf(stderr, "fuse: memory allocation failed\n"); |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 164 | return -1; |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 165 | } |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 166 | } |
Miklos Szeredi | c902a85 | 2005-07-07 12:35:37 +0000 | [diff] [blame] | 167 | if (has_allow_other && has_allow_root) { |
| 168 | fprintf(stderr, "fuse: 'allow_other' and 'allow_root' options are mutually exclusive\n"); |
| 169 | return -1; |
| 170 | } |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 171 | free(xopts); |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 172 | return 0; |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 175 | static int fuse_parse_cmdline(int argc, const char *argv[], char **kernel_opts, |
| 176 | char **lib_opts, char **mountpoint, |
| 177 | int *multithreaded, int *background) |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 178 | { |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 179 | int res; |
Miklos Szeredi | e970f30 | 2004-02-25 08:39:42 +0000 | [diff] [blame] | 180 | int argctr; |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 181 | const char *basename; |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 182 | char *fsname_opt; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 183 | |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 184 | *kernel_opts = NULL; |
| 185 | *lib_opts = NULL; |
| 186 | *mountpoint = NULL; |
| 187 | *multithreaded = 1; |
| 188 | *background = 1; |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 189 | |
| 190 | basename = strrchr(argv[0], '/'); |
| 191 | if (basename == NULL) |
| 192 | basename = argv[0]; |
| 193 | else if (basename[1] != '\0') |
| 194 | basename++; |
| 195 | |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 196 | fsname_opt = (char *) malloc(strlen(basename) + 64); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 197 | if (fsname_opt == NULL) { |
| 198 | fprintf(stderr, "fuse: memory allocation failed\n"); |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 199 | return -1; |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 200 | } |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 201 | sprintf(fsname_opt, "fsname=%s", basename); |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 202 | res = add_options(lib_opts, kernel_opts, fsname_opt); |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 203 | free(fsname_opt); |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 204 | if (res == -1) |
| 205 | goto err; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 206 | |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 207 | for (argctr = 1; argctr < argc; argctr ++) { |
Miklos Szeredi | ff87535 | 2004-06-03 13:52:40 +0000 | [diff] [blame] | 208 | if (argv[argctr][0] == '-') { |
| 209 | if (strlen(argv[argctr]) == 2) |
| 210 | switch (argv[argctr][1]) { |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 211 | case 'o': |
| 212 | if (argctr + 1 == argc || argv[argctr+1][0] == '-') { |
Miklos Szeredi | 799993c | 2004-12-04 21:20:05 +0000 | [diff] [blame] | 213 | fprintf(stderr, "missing option after -o\n"); |
Miklos Szeredi | d59bb9d | 2004-12-07 10:04:24 +0000 | [diff] [blame] | 214 | fprintf(stderr, "see `%s -h' for usage\n", argv[0]); |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 215 | goto err; |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 216 | } |
| 217 | argctr ++; |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 218 | res = add_options(lib_opts, kernel_opts, argv[argctr]); |
| 219 | if (res == -1) |
| 220 | goto err; |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 221 | break; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 222 | |
Miklos Szeredi | e970f30 | 2004-02-25 08:39:42 +0000 | [diff] [blame] | 223 | case 'd': |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 224 | res = add_options(lib_opts, kernel_opts, "debug"); |
| 225 | if (res == -1) |
| 226 | goto err; |
Miklos Szeredi | e970f30 | 2004-02-25 08:39:42 +0000 | [diff] [blame] | 227 | break; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 228 | |
Miklos Szeredi | 96dfad7 | 2004-11-30 00:00:02 +0000 | [diff] [blame] | 229 | case 'r': |
| 230 | res = add_options(lib_opts, kernel_opts, "ro"); |
| 231 | if (res == -1) |
| 232 | goto err; |
| 233 | break; |
| 234 | |
Miklos Szeredi | ff87535 | 2004-06-03 13:52:40 +0000 | [diff] [blame] | 235 | case 'f': |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 236 | *background = 0; |
Miklos Szeredi | ff87535 | 2004-06-03 13:52:40 +0000 | [diff] [blame] | 237 | break; |
| 238 | |
Miklos Szeredi | e970f30 | 2004-02-25 08:39:42 +0000 | [diff] [blame] | 239 | case 's': |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 240 | *multithreaded = 0; |
Miklos Szeredi | e970f30 | 2004-02-25 08:39:42 +0000 | [diff] [blame] | 241 | break; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 242 | |
Miklos Szeredi | e970f30 | 2004-02-25 08:39:42 +0000 | [diff] [blame] | 243 | case 'h': |
| 244 | usage(argv[0]); |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 245 | goto err; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 246 | |
Miklos Szeredi | e970f30 | 2004-02-25 08:39:42 +0000 | [diff] [blame] | 247 | default: |
| 248 | invalid_option(argv, argctr); |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 249 | goto err; |
Miklos Szeredi | e970f30 | 2004-02-25 08:39:42 +0000 | [diff] [blame] | 250 | } |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 251 | else { |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 252 | if (argv[argctr][1] == 'o') { |
| 253 | res = add_options(lib_opts, kernel_opts, &argv[argctr][2]); |
| 254 | if (res == -1) |
| 255 | goto err; |
Miklos Szeredi | 799993c | 2004-12-04 21:20:05 +0000 | [diff] [blame] | 256 | } else if(strcmp(argv[argctr], "-ho") == 0) { |
| 257 | usage(NULL); |
| 258 | goto err; |
| 259 | } else { |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 260 | invalid_option(argv, argctr); |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 261 | goto err; |
| 262 | } |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 263 | } |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 264 | } else if (*mountpoint == NULL) { |
| 265 | *mountpoint = strdup(argv[argctr]); |
| 266 | if (*mountpoint == NULL) { |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 267 | fprintf(stderr, "fuse: memory allocation failed\n"); |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 268 | goto err; |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 269 | } |
| 270 | } |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 271 | else { |
Miklos Szeredi | 307242f | 2004-01-26 11:28:44 +0000 | [diff] [blame] | 272 | invalid_option(argv, argctr); |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 273 | goto err; |
| 274 | } |
Miklos Szeredi | 307242f | 2004-01-26 11:28:44 +0000 | [diff] [blame] | 275 | } |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 276 | return 0; |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 277 | |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 278 | err: |
| 279 | free(*kernel_opts); |
| 280 | free(*lib_opts); |
| 281 | free(*mountpoint); |
| 282 | return -1; |
| 283 | } |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 284 | |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 285 | static struct fuse *fuse_setup_common(int argc, char *argv[], |
| 286 | const struct fuse_operations *op, |
| 287 | size_t op_size, |
| 288 | char **mountpoint, |
| 289 | int *multithreaded, |
| 290 | int *fd, |
| 291 | int compat) |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 292 | { |
| 293 | struct fuse *fuse; |
| 294 | int background; |
| 295 | char *kernel_opts; |
| 296 | char *lib_opts; |
| 297 | int res; |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 298 | |
Miklos Szeredi | e175fb7 | 2004-10-21 16:33:17 +0000 | [diff] [blame] | 299 | if (fuse_instance != NULL) { |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 300 | fprintf(stderr, "fuse: fuse_setup() called twice\n"); |
Miklos Szeredi | e175fb7 | 2004-10-21 16:33:17 +0000 | [diff] [blame] | 301 | return NULL; |
| 302 | } |
| 303 | |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 304 | res = fuse_parse_cmdline(argc, (const char **) argv, &kernel_opts, |
| 305 | &lib_opts, mountpoint, multithreaded, |
| 306 | &background); |
| 307 | if (res == -1) |
| 308 | return NULL; |
| 309 | |
| 310 | *fd = fuse_mount(*mountpoint, kernel_opts); |
| 311 | if (*fd == -1) |
| 312 | goto err_free; |
| 313 | |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 314 | fuse = fuse_new_common(*fd, lib_opts, op, op_size, compat); |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 315 | if (fuse == NULL) |
| 316 | goto err_unmount; |
| 317 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 318 | if (background && !opt_member(lib_opts, "debug")) { |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 319 | res = daemon(0, 0); |
| 320 | if (res == -1) { |
| 321 | perror("fuse: failed to daemonize program\n"); |
| 322 | goto err_destroy; |
| 323 | } |
| 324 | } |
Miklos Szeredi | e175fb7 | 2004-10-21 16:33:17 +0000 | [diff] [blame] | 325 | |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 326 | res = set_signal_handlers(); |
| 327 | if (res == -1) |
| 328 | goto err_destroy; |
| 329 | |
Miklos Szeredi | cc82a8c | 2004-10-21 16:39:09 +0000 | [diff] [blame] | 330 | fuse_instance = fuse; |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 331 | free(kernel_opts); |
| 332 | free(lib_opts); |
| 333 | return fuse; |
| 334 | |
| 335 | err_destroy: |
| 336 | fuse_destroy(fuse); |
| 337 | err_unmount: |
| 338 | fuse_unmount(*mountpoint); |
| 339 | err_free: |
| 340 | free(kernel_opts); |
| 341 | free(lib_opts); |
| 342 | free(*mountpoint); |
| 343 | return NULL; |
| 344 | } |
| 345 | |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 346 | struct fuse *fuse_setup(int argc, char *argv[], |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 347 | const struct fuse_operations *op, |
| 348 | size_t op_size, char **mountpoint, |
| 349 | int *multithreaded, int *fd) |
| 350 | { |
| 351 | return fuse_setup_common(argc, argv, op, op_size, mountpoint, |
| 352 | multithreaded, fd, 0); |
| 353 | } |
| 354 | |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 355 | void fuse_teardown(struct fuse *fuse, int fd, char *mountpoint) |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 356 | { |
Miklos Szeredi | bcc5385 | 2005-11-10 09:54:41 +0000 | [diff] [blame] | 357 | (void) fd; |
| 358 | |
Miklos Szeredi | e175fb7 | 2004-10-21 16:33:17 +0000 | [diff] [blame] | 359 | if (fuse_instance != fuse) |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 360 | fprintf(stderr, "fuse: fuse_teardown() with unknown fuse object\n"); |
Miklos Szeredi | e175fb7 | 2004-10-21 16:33:17 +0000 | [diff] [blame] | 361 | else |
| 362 | fuse_instance = NULL; |
| 363 | |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 364 | fuse_unmount(mountpoint); |
Miklos Szeredi | b3f9972 | 2005-11-16 13:00:24 +0000 | [diff] [blame] | 365 | fuse_destroy(fuse); |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 366 | free(mountpoint); |
| 367 | } |
| 368 | |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 369 | static int fuse_main_common(int argc, char *argv[], |
| 370 | const struct fuse_operations *op, size_t op_size, |
| 371 | int compat) |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 372 | { |
Miklos Szeredi | e175fb7 | 2004-10-21 16:33:17 +0000 | [diff] [blame] | 373 | struct fuse *fuse; |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 374 | char *mountpoint; |
| 375 | int multithreaded; |
| 376 | int res; |
| 377 | int fd; |
| 378 | |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 379 | fuse = fuse_setup_common(argc, argv, op, op_size, &mountpoint, |
| 380 | &multithreaded, &fd, compat); |
Miklos Szeredi | e175fb7 | 2004-10-21 16:33:17 +0000 | [diff] [blame] | 381 | if (fuse == NULL) |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 382 | return 1; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 383 | |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 384 | if (multithreaded) |
Miklos Szeredi | e175fb7 | 2004-10-21 16:33:17 +0000 | [diff] [blame] | 385 | res = fuse_loop_mt(fuse); |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 386 | else |
Miklos Szeredi | e175fb7 | 2004-10-21 16:33:17 +0000 | [diff] [blame] | 387 | res = fuse_loop(fuse); |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 388 | |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 389 | fuse_teardown(fuse, fd, mountpoint); |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 390 | if (res == -1) |
| 391 | return 1; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 392 | |
Miklos Szeredi | 5dc8a80 | 2004-10-21 09:35:10 +0000 | [diff] [blame] | 393 | return 0; |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 396 | int fuse_main_real(int argc, char *argv[], const struct fuse_operations *op, |
| 397 | size_t op_size) |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 398 | { |
| 399 | return fuse_main_common(argc, argv, op, op_size, 0); |
| 400 | } |
| 401 | |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 402 | #undef fuse_main |
| 403 | int fuse_main(void) |
| 404 | { |
| 405 | fprintf(stderr, "fuse_main(): This function does not exist\n"); |
| 406 | return -1; |
| 407 | } |
| 408 | |
| 409 | #ifndef __FreeBSD__ |
| 410 | |
| 411 | #include "fuse_compat.h" |
| 412 | |
| 413 | struct fuse *fuse_setup_compat22(int argc, char *argv[], |
| 414 | const struct fuse_operations_compat22 *op, |
| 415 | size_t op_size, char **mountpoint, |
| 416 | int *multithreaded, int *fd) |
| 417 | { |
| 418 | return fuse_setup_common(argc, argv, (struct fuse_operations *) op, |
| 419 | op_size, mountpoint, multithreaded, fd, 22); |
| 420 | } |
| 421 | |
| 422 | struct fuse *fuse_setup_compat2(int argc, char *argv[], |
| 423 | const struct fuse_operations_compat2 *op, |
| 424 | char **mountpoint, int *multithreaded, |
| 425 | int *fd) |
| 426 | { |
| 427 | return fuse_setup_common(argc, argv, (struct fuse_operations *) op, |
| 428 | sizeof(struct fuse_operations_compat2), |
| 429 | mountpoint, multithreaded, fd, 21); |
| 430 | } |
| 431 | |
Miklos Szeredi | 3a77047 | 2005-11-11 21:32:42 +0000 | [diff] [blame] | 432 | int fuse_main_real_compat22(int argc, char *argv[], |
| 433 | const struct fuse_operations_compat22 *op, |
| 434 | size_t op_size) |
| 435 | { |
| 436 | return fuse_main_common(argc, argv, (struct fuse_operations *) op, |
| 437 | op_size, 22); |
| 438 | } |
| 439 | |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 440 | void fuse_main_compat1(int argc, char *argv[], |
| 441 | const struct fuse_operations_compat1 *op) |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 442 | { |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 443 | fuse_main_common(argc, argv, (struct fuse_operations *) op, |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 444 | sizeof(struct fuse_operations_compat1), 11); |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 447 | int fuse_main_compat2(int argc, char *argv[], |
| 448 | const struct fuse_operations_compat2 *op) |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 449 | { |
| 450 | return fuse_main_common(argc, argv, (struct fuse_operations *) op, |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 451 | sizeof(struct fuse_operations_compat2), 21); |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 454 | __asm__(".symver fuse_setup_compat2,__fuse_setup@"); |
Miklos Szeredi | 3a77047 | 2005-11-11 21:32:42 +0000 | [diff] [blame] | 455 | __asm__(".symver fuse_setup_compat22,fuse_setup@FUSE_2.2"); |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 456 | __asm__(".symver fuse_teardown,__fuse_teardown@"); |
| 457 | __asm__(".symver fuse_main_compat2,fuse_main@"); |
Miklos Szeredi | 3a77047 | 2005-11-11 21:32:42 +0000 | [diff] [blame] | 458 | __asm__(".symver fuse_main_real_compat22,fuse_main_real@FUSE_2.2"); |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 459 | |
| 460 | #endif /* __FreeBSD__ */ |