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