MiniDLNA update: 1.0.19.1 to 1.0.20
[tomato.git] / release / src / router / ntfs-3g / libfuse-lite / mount.c
blob07f5ead299e828ccfbe9c57ff581fbd6a544f923
1 /*
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.
7 */
9 #include "config.h"
10 #include "fuse_i.h"
11 #include "fuse_opt.h"
12 #include "mount_util.h"
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <stddef.h>
18 #include <fcntl.h>
19 #include <errno.h>
20 #include <sys/poll.h>
21 #include <sys/socket.h>
22 #include <sys/un.h>
23 #include <sys/wait.h>
24 #include <sys/mount.h>
26 #ifndef MS_DIRSYNC
27 #define MS_DIRSYNC 128
28 #endif
30 enum {
31 KEY_KERN_FLAG,
32 KEY_KERN_OPT,
33 KEY_FUSERMOUNT_OPT,
34 KEY_SUBTYPE_OPT,
35 KEY_MTAB_OPT,
36 KEY_ALLOW_ROOT,
37 KEY_RO,
38 KEY_HELP,
39 KEY_VERSION,
42 struct mount_opts {
43 int allow_other;
44 int allow_root;
45 int ishelp;
46 int flags;
47 int blkdev;
48 char *fsname;
49 char *mtab_opts;
50 char *fusermount_opts;
51 char *kernel_opts;
54 #define FUSE_MOUNT_OPT(t, p) { t, offsetof(struct mount_opts, p), 1 }
56 static const struct fuse_opt fuse_mount_opts[] = {
57 FUSE_MOUNT_OPT("allow_other", allow_other),
58 FUSE_MOUNT_OPT("allow_root", allow_root),
59 FUSE_MOUNT_OPT("blkdev", blkdev),
60 FUSE_MOUNT_OPT("fsname=%s", fsname),
61 FUSE_OPT_KEY("allow_other", KEY_KERN_OPT),
62 FUSE_OPT_KEY("allow_root", KEY_ALLOW_ROOT),
63 FUSE_OPT_KEY("blkdev", KEY_FUSERMOUNT_OPT),
64 FUSE_OPT_KEY("fsname=", KEY_FUSERMOUNT_OPT),
65 FUSE_OPT_KEY("large_read", KEY_KERN_OPT),
66 FUSE_OPT_KEY("blksize=", KEY_KERN_OPT),
67 FUSE_OPT_KEY("default_permissions", KEY_KERN_OPT),
68 FUSE_OPT_KEY("context=", KEY_KERN_OPT),
69 FUSE_OPT_KEY("max_read=", KEY_KERN_OPT),
70 FUSE_OPT_KEY("max_read=", FUSE_OPT_KEY_KEEP),
71 FUSE_OPT_KEY("user=", KEY_MTAB_OPT),
72 FUSE_OPT_KEY("-r", KEY_RO),
73 FUSE_OPT_KEY("ro", KEY_KERN_FLAG),
74 FUSE_OPT_KEY("rw", KEY_KERN_FLAG),
75 FUSE_OPT_KEY("suid", KEY_KERN_FLAG),
76 FUSE_OPT_KEY("nosuid", KEY_KERN_FLAG),
77 FUSE_OPT_KEY("dev", KEY_KERN_FLAG),
78 FUSE_OPT_KEY("nodev", KEY_KERN_FLAG),
79 FUSE_OPT_KEY("exec", KEY_KERN_FLAG),
80 FUSE_OPT_KEY("noexec", KEY_KERN_FLAG),
81 FUSE_OPT_KEY("async", KEY_KERN_FLAG),
82 FUSE_OPT_KEY("sync", KEY_KERN_FLAG),
83 FUSE_OPT_KEY("dirsync", KEY_KERN_FLAG),
84 FUSE_OPT_KEY("atime", KEY_KERN_FLAG),
85 FUSE_OPT_KEY("noatime", KEY_KERN_FLAG),
86 FUSE_OPT_KEY("-h", KEY_HELP),
87 FUSE_OPT_KEY("--help", KEY_HELP),
88 FUSE_OPT_KEY("-V", KEY_VERSION),
89 FUSE_OPT_KEY("--version", KEY_VERSION),
90 FUSE_OPT_END
93 struct mount_flags {
94 const char *opt;
95 unsigned long flag;
96 int on;
99 static struct mount_flags mount_flags[] = {
100 {"rw", MS_RDONLY, 0},
101 {"ro", MS_RDONLY, 1},
102 {"suid", MS_NOSUID, 0},
103 {"nosuid", MS_NOSUID, 1},
104 {"dev", MS_NODEV, 0},
105 {"nodev", MS_NODEV, 1},
106 {"exec", MS_NOEXEC, 0},
107 {"noexec", MS_NOEXEC, 1},
108 {"async", MS_SYNCHRONOUS, 0},
109 {"sync", MS_SYNCHRONOUS, 1},
110 {"atime", MS_NOATIME, 0},
111 {"noatime", MS_NOATIME, 1},
112 {"dirsync", MS_DIRSYNC, 1},
113 {NULL, 0, 0}
116 static void set_mount_flag(const char *s, int *flags)
118 int i;
120 for (i = 0; mount_flags[i].opt != NULL; i++) {
121 const char *opt = mount_flags[i].opt;
122 if (strcmp(opt, s) == 0) {
123 if (mount_flags[i].on)
124 *flags |= mount_flags[i].flag;
125 else
126 *flags &= ~mount_flags[i].flag;
127 return;
130 fprintf(stderr, "fuse: internal error, can't find mount flag\n");
131 abort();
134 static int fuse_mount_opt_proc(void *data, const char *arg, int key,
135 struct fuse_args *outargs)
137 struct mount_opts *mo = data;
139 switch (key) {
140 case KEY_ALLOW_ROOT:
141 if (fuse_opt_add_opt(&mo->kernel_opts, "allow_other") == -1 ||
142 fuse_opt_add_arg(outargs, "-oallow_root") == -1)
143 return -1;
144 return 0;
146 case KEY_RO:
147 arg = "ro";
148 /* fall through */
149 case KEY_KERN_FLAG:
150 set_mount_flag(arg, &mo->flags);
151 return 0;
153 case KEY_KERN_OPT:
154 return fuse_opt_add_opt(&mo->kernel_opts, arg);
156 case KEY_FUSERMOUNT_OPT:
157 return fuse_opt_add_opt(&mo->fusermount_opts, arg);
159 case KEY_MTAB_OPT:
160 return fuse_opt_add_opt(&mo->mtab_opts, arg);
162 case KEY_HELP:
163 mo->ishelp = 1;
164 break;
166 case KEY_VERSION:
167 mo->ishelp = 1;
168 break;
170 return 1;
173 void fuse_kern_unmount(const char *mountpoint, int fd)
175 int res;
177 if (!mountpoint)
178 return;
180 if (fd != -1) {
181 struct pollfd pfd;
183 pfd.fd = fd;
184 pfd.events = 0;
185 res = poll(&pfd, 1, 0);
186 /* If file poll returns POLLERR on the device file descriptor,
187 then the filesystem is already unmounted */
188 if (res == 1 && (pfd.revents & POLLERR))
189 return;
191 close(fd);
193 fusermount(1, 0, 1, "", mountpoint);
196 static int get_mnt_flag_opts(char **mnt_optsp, int flags)
198 int i;
200 if (!(flags & MS_RDONLY) && fuse_opt_add_opt(mnt_optsp, "rw") == -1)
201 return -1;
203 for (i = 0; mount_flags[i].opt != NULL; i++) {
204 if (mount_flags[i].on && (flags & mount_flags[i].flag) &&
205 fuse_opt_add_opt(mnt_optsp, mount_flags[i].opt) == -1)
206 return -1;
208 return 0;
211 int fuse_kern_mount(const char *mountpoint, struct fuse_args *args)
213 struct mount_opts mo;
214 int res = -1;
215 char *mnt_opts = NULL;
217 memset(&mo, 0, sizeof(mo));
218 if (getuid())
219 mo.flags = MS_NOSUID | MS_NODEV;
221 if (args &&
222 fuse_opt_parse(args, &mo, fuse_mount_opts, fuse_mount_opt_proc) == -1)
223 return -1;
225 if (mo.allow_other && mo.allow_root) {
226 fprintf(stderr, "fuse: 'allow_other' and 'allow_root' options are mutually exclusive\n");
227 goto out;
229 res = 0;
230 if (mo.ishelp)
231 goto out;
233 res = -1;
234 if (get_mnt_flag_opts(&mnt_opts, mo.flags) == -1)
235 goto out;
236 if (!(mo.flags & MS_NODEV) && fuse_opt_add_opt(&mnt_opts, "dev") == -1)
237 goto out;
238 if (!(mo.flags & MS_NOSUID) && fuse_opt_add_opt(&mnt_opts, "suid") == -1)
239 goto out;
240 if (mo.kernel_opts && fuse_opt_add_opt(&mnt_opts, mo.kernel_opts) == -1)
241 goto out;
242 if (mo.mtab_opts && fuse_opt_add_opt(&mnt_opts, mo.mtab_opts) == -1)
243 goto out;
244 if (mo.fusermount_opts && fuse_opt_add_opt(&mnt_opts, mo.fusermount_opts) < 0)
245 goto out;
247 res = fusermount(0, 0, 0, mnt_opts ? mnt_opts : "", mountpoint);
249 out:
250 free(mnt_opts);
251 free(mo.fsname);
252 free(mo.fusermount_opts);
253 free(mo.kernel_opts);
254 free(mo.mtab_opts);
255 return res;