libfuse: fuse-3.13.1/lib/mount.c Source File

libfuse

mount.c
1 /*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
4
5 Architecture specific file system mounting (Linux).
6
7 This program can be distributed under the terms of the GNU LGPLv2.
8 See the file COPYING.LIB.
9 */
10
11 #include "fuse_config.h"
12 #include "fuse_i.h"
13 #include "fuse_misc.h"
14 #include "fuse_opt.h"
15 #include "mount_util.h"
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <stddef.h>
21 #include <string.h>
22 #include <fcntl.h>
23 #include <errno.h>
24 #include <poll.h>
25 #include <sys/socket.h>
26 #include <sys/un.h>
27 #include <sys/wait.h>
28 #include <sys/mount.h>
29
30 #ifdef __NetBSD__
31 #include <perfuse.h>
32
33 #define MS_RDONLY MNT_RDONLY
34 #define MS_NOSUID MNT_NOSUID
35 #define MS_NODEV MNT_NODEV
36 #define MS_NOEXEC MNT_NOEXEC
37 #define MS_SYNCHRONOUS MNT_SYNCHRONOUS
38 #define MS_NOATIME MNT_NOATIME
39
40 #define umount2(mnt, flags) unmount(mnt, (flags == 2) ? MNT_FORCE : 0)
41 #endif
42
43 #define FUSERMOUNT_PROG "fusermount3"
44 #define FUSE_COMMFD_ENV "_FUSE_COMMFD"
45
46 #ifndef HAVE_FORK
47 #define fork() vfork()
48 #endif
49
50 #ifndef MS_DIRSYNC
51 #define MS_DIRSYNC 128
52 #endif
53
54 enum {
55 KEY_KERN_FLAG,
56 KEY_KERN_OPT,
57 KEY_FUSERMOUNT_OPT,
58 KEY_SUBTYPE_OPT,
59 KEY_MTAB_OPT,
60 KEY_ALLOW_OTHER,
61 KEY_RO,
62 };
63
64 struct mount_opts {
65 int allow_other;
66 int flags;
67 int auto_unmount;
68 int blkdev;
69 char *fsname;
70 char *subtype;
71 char *subtype_opt;
72 char *mtab_opts;
73 char *fusermount_opts;
74 char *kernel_opts;
75 unsigned max_read;
76 };
77
78 #define FUSE_MOUNT_OPT(t, p) { t, offsetof(struct mount_opts, p), 1 }
79
80 static const struct fuse_opt fuse_mount_opts[] = {
81 FUSE_MOUNT_OPT("allow_other", allow_other),
82 FUSE_MOUNT_OPT("blkdev", blkdev),
83 FUSE_MOUNT_OPT("auto_unmount", auto_unmount),
84 FUSE_MOUNT_OPT("fsname=%s", fsname),
85 FUSE_MOUNT_OPT("max_read=%u", max_read),
86 FUSE_MOUNT_OPT("subtype=%s", subtype),
87 FUSE_OPT_KEY("allow_other", KEY_KERN_OPT),
88 FUSE_OPT_KEY("auto_unmount", KEY_FUSERMOUNT_OPT),
89 FUSE_OPT_KEY("blkdev", KEY_FUSERMOUNT_OPT),
90 FUSE_OPT_KEY("fsname=", KEY_FUSERMOUNT_OPT),
91 FUSE_OPT_KEY("subtype=", KEY_SUBTYPE_OPT),
92 FUSE_OPT_KEY("blksize=", KEY_KERN_OPT),
93 FUSE_OPT_KEY("default_permissions", KEY_KERN_OPT),
94 FUSE_OPT_KEY("context=", KEY_KERN_OPT),
95 FUSE_OPT_KEY("fscontext=", KEY_KERN_OPT),
96 FUSE_OPT_KEY("defcontext=", KEY_KERN_OPT),
97 FUSE_OPT_KEY("rootcontext=", KEY_KERN_OPT),
98 FUSE_OPT_KEY("max_read=", KEY_KERN_OPT),
99 FUSE_OPT_KEY("user=", KEY_MTAB_OPT),
100 FUSE_OPT_KEY("-n", KEY_MTAB_OPT),
101 FUSE_OPT_KEY("-r", KEY_RO),
102 FUSE_OPT_KEY("ro", KEY_KERN_FLAG),
103 FUSE_OPT_KEY("rw", KEY_KERN_FLAG),
104 FUSE_OPT_KEY("suid", KEY_KERN_FLAG),
105 FUSE_OPT_KEY("nosuid", KEY_KERN_FLAG),
106 FUSE_OPT_KEY("dev", KEY_KERN_FLAG),
107 FUSE_OPT_KEY("nodev", KEY_KERN_FLAG),
108 FUSE_OPT_KEY("exec", KEY_KERN_FLAG),
109 FUSE_OPT_KEY("noexec", KEY_KERN_FLAG),
110 FUSE_OPT_KEY("async", KEY_KERN_FLAG),
111 FUSE_OPT_KEY("sync", KEY_KERN_FLAG),
112 FUSE_OPT_KEY("dirsync", KEY_KERN_FLAG),
113 FUSE_OPT_KEY("atime", KEY_KERN_FLAG),
114 FUSE_OPT_KEY("noatime", KEY_KERN_FLAG),
115 FUSE_OPT_END
116 };
117
118 static void exec_fusermount(const char *argv[])
119 {
120 execv(FUSERMOUNT_DIR "/" FUSERMOUNT_PROG, (char **) argv);
121 execvp(FUSERMOUNT_PROG, (char **) argv);
122 }
123
124 void fuse_mount_version(void)
125 {
126 int pid = fork();
127 if (!pid) {
128 const char *argv[] = { FUSERMOUNT_PROG, "--version", NULL };
129 exec_fusermount(argv);
130 _exit(1);
131 } else if (pid != -1)
132 waitpid(pid, NULL, 0);
133 }
134
135 struct mount_flags {
136 const char *opt;
137 unsigned long flag;
138 int on;
139 };
140
141 static const struct mount_flags mount_flags[] = {
142 {"rw", MS_RDONLY, 0},
143 {"ro", MS_RDONLY, 1},
144 {"suid", MS_NOSUID, 0},
145 {"nosuid", MS_NOSUID, 1},
146 {"dev", MS_NODEV, 0},
147 {"nodev", MS_NODEV, 1},
148 {"exec", MS_NOEXEC, 0},
149 {"noexec", MS_NOEXEC, 1},
150 {"async", MS_SYNCHRONOUS, 0},
151 {"sync", MS_SYNCHRONOUS, 1},
152 {"atime", MS_NOATIME, 0},
153 {"noatime", MS_NOATIME, 1},
154 #ifndef __NetBSD__
155 {"dirsync", MS_DIRSYNC, 1},
156 #endif
157 {NULL, 0, 0}
158 };
159
160 unsigned get_max_read(struct mount_opts *o)
161 {
162 return o->max_read;
163 }
164
165 static void set_mount_flag(const char *s, int *flags)
166 {
167 int i;
168
169 for (i = 0; mount_flags[i].opt != NULL; i++) {
170 const char *opt = mount_flags[i].opt;
171 if (strcmp(opt, s) == 0) {
172 if (mount_flags[i].on)
173 *flags |= mount_flags[i].flag;
174 else
175 *flags &= ~mount_flags[i].flag;
176 return;
177 }
178 }
179 fuse_log(FUSE_LOG_ERR, "fuse: internal error, can't find mount flag\n");
180 abort();
181 }
182
183 static int fuse_mount_opt_proc(void *data, const char *arg, int key,
184 struct fuse_args *outargs)
185 {
186 (void) outargs;
187 struct mount_opts *mo = data;
188
189 switch (key) {
190 case KEY_RO:
191 arg = "ro";
192 /* fall through */
193 case KEY_KERN_FLAG:
194 set_mount_flag(arg, &mo->flags);
195 return 0;
196
197 case KEY_KERN_OPT:
198 return fuse_opt_add_opt(&mo->kernel_opts, arg);
199
200 case KEY_FUSERMOUNT_OPT:
201 return fuse_opt_add_opt_escaped(&mo->fusermount_opts, arg);
202
203 case KEY_SUBTYPE_OPT:
204 return fuse_opt_add_opt(&mo->subtype_opt, arg);
205
206 case KEY_MTAB_OPT:
207 return fuse_opt_add_opt(&mo->mtab_opts, arg);
208 }
209
210 /* Pass through unknown options */
211 return 1;
212 }
213
214 /* return value:
215 * >= 0 => fd
216 * -1 => error
217 */
218 static int receive_fd(int fd)
219 {
220 struct msghdr msg;
221 struct iovec iov;
222 char buf[1];
223 int rv;
224 size_t ccmsg[CMSG_SPACE(sizeof(int)) / sizeof(size_t)];
225 struct cmsghdr *cmsg;
226
227 iov.iov_base = buf;
228 iov.iov_len = 1;
229
230 memset(&msg, 0, sizeof(msg));
231 msg.msg_name = 0;
232 msg.msg_namelen = 0;
233 msg.msg_iov = &iov;
234 msg.msg_iovlen = 1;
235 /* old BSD implementations should use msg_accrights instead of
236 * msg_control; the interface is different. */
237 msg.msg_control = ccmsg;
238 msg.msg_controllen = sizeof(ccmsg);
239
240 while(((rv = recvmsg(fd, &msg, 0)) == -1) && errno == EINTR);
241 if (rv == -1) {
242 perror("recvmsg");
243 return -1;
244 }
245 if(!rv) {
246 /* EOF */
247 return -1;
248 }
249
250 cmsg = CMSG_FIRSTHDR(&msg);
251 if (cmsg->cmsg_type != SCM_RIGHTS) {
252 fuse_log(FUSE_LOG_ERR, "got control message of unknown type %d\n",
253 cmsg->cmsg_type);
254 return -1;
255 }
256 return *(int*)CMSG_DATA(cmsg);
257 }
258
259 void fuse_kern_unmount(const char *mountpoint, int fd)
260 {
261 int res;
262 int pid;
263
264 if (fd != -1) {
265 struct pollfd pfd;
266
267 pfd.fd = fd;
268 pfd.events = 0;
269 res = poll(&pfd, 1, 0);
270
271 /* Need to close file descriptor, otherwise synchronous umount
272 would recurse into filesystem, and deadlock.
273
274 Caller expects fuse_kern_unmount to close the fd, so close it
275 anyway. */
276 close(fd);
277
278 /* If file poll returns POLLERR on the device file descriptor,
279 then the filesystem is already unmounted or the connection
280 was severed via /sys/fs/fuse/connections/NNN/abort */
281 if (res == 1 && (pfd.revents & POLLERR))
282 return;
283 }
284
285 if (geteuid() == 0) {
286 fuse_mnt_umount("fuse", mountpoint, mountpoint, 1);
287 return;
288 }
289
290 res = umount2(mountpoint, 2);
291 if (res == 0)
292 return;
293
294 pid = fork();
295 if(pid == -1)
296 return;
297
298 if(pid == 0) {
299 const char *argv[] = { FUSERMOUNT_PROG, "-u", "-q", "-z",
300 "--", mountpoint, NULL };
301
302 exec_fusermount(argv);
303 _exit(1);
304 }
305 waitpid(pid, NULL, 0);
306 }
307
308 static int fuse_mount_fusermount(const char *mountpoint, struct mount_opts *mo,
309 const char *opts, int quiet)
310 {
311 int fds[2], pid;
312 int res;
313 int rv;
314
315 if (!mountpoint) {
316 fuse_log(FUSE_LOG_ERR, "fuse: missing mountpoint parameter\n");
317 return -1;
318 }
319
320 res = socketpair(PF_UNIX, SOCK_STREAM, 0, fds);
321 if(res == -1) {
322 perror("fuse: socketpair() failed");
323 return -1;
324 }
325
326 pid = fork();
327 if(pid == -1) {
328 perror("fuse: fork() failed");
329 close(fds[0]);
330 close(fds[1]);
331 return -1;
332 }
333
334 if(pid == 0) {
335 char env[10];
336 const char *argv[32];
337 int a = 0;
338
339 if (quiet) {
340 int fd = open("/dev/null", O_RDONLY);
341 if (fd != -1) {
342 dup2(fd, 1);
343 dup2(fd, 2);
344 }
345 }
346
347 argv[a++] = FUSERMOUNT_PROG;
348 if (opts) {
349 argv[a++] = "-o";
350 argv[a++] = opts;
351 }
352 argv[a++] = "--";
353 argv[a++] = mountpoint;
354 argv[a++] = NULL;
355
356 close(fds[1]);
357 fcntl(fds[0], F_SETFD, 0);
358 snprintf(env, sizeof(env), "%i", fds[0]);
359 setenv(FUSE_COMMFD_ENV, env, 1);
360 exec_fusermount(argv);
361 perror("fuse: failed to exec fusermount3");
362 _exit(1);
363 }
364
365 close(fds[0]);
366 rv = receive_fd(fds[1]);
367
368 if (!mo->auto_unmount) {
369 /* with auto_unmount option fusermount3 will not exit until
370 this socket is closed */
371 close(fds[1]);
372 waitpid(pid, NULL, 0); /* bury zombie */
373 }
374
375 if (rv >= 0)
376 fcntl(rv, F_SETFD, FD_CLOEXEC);
377
378 return rv;
379 }
380
381 #ifndef O_CLOEXEC
382 #define O_CLOEXEC 0
383 #endif
384
385 static int fuse_mount_sys(const char *mnt, struct mount_opts *mo,
386 const char *mnt_opts)
387 {
388 char tmp[128];
389 const char *devname = "/dev/fuse";
390 char *source = NULL;
391 char *type = NULL;
392 struct stat stbuf;
393 int fd;
394 int res;
395
396 if (!mnt) {
397 fuse_log(FUSE_LOG_ERR, "fuse: missing mountpoint parameter\n");
398 return -1;
399 }
400
401 res = stat(mnt, &stbuf);
402 if (res == -1) {
403 fuse_log(FUSE_LOG_ERR, "fuse: failed to access mountpoint %s: %s\n",
404 mnt, strerror(errno));
405 return -1;
406 }
407
408 if (mo->auto_unmount) {
409 /* Tell the caller to fallback to fusermount3 because
410 auto-unmount does not work otherwise. */
411 return -2;
412 }
413
414 fd = open(devname, O_RDWR | O_CLOEXEC);
415 if (fd == -1) {
416 if (errno == ENODEV || errno == ENOENT)
417 fuse_log(FUSE_LOG_ERR, "fuse: device not found, try 'modprobe fuse' first\n");
418 else
419 fuse_log(FUSE_LOG_ERR, "fuse: failed to open %s: %s\n",
420 devname, strerror(errno));
421 return -1;
422 }
423 if (!O_CLOEXEC)
424 fcntl(fd, F_SETFD, FD_CLOEXEC);
425
426 snprintf(tmp, sizeof(tmp), "fd=%i,rootmode=%o,user_id=%u,group_id=%u",
427 fd, stbuf.st_mode & S_IFMT, getuid(), getgid());
428
429 res = fuse_opt_add_opt(&mo->kernel_opts, tmp);
430 if (res == -1)
431 goto out_close;
432
433 source = malloc((mo->fsname ? strlen(mo->fsname) : 0) +
434 (mo->subtype ? strlen(mo->subtype) : 0) +
435 strlen(devname) + 32);
436
437 type = malloc((mo->subtype ? strlen(mo->subtype) : 0) + 32);
438 if (!type || !source) {
439 fuse_log(FUSE_LOG_ERR, "fuse: failed to allocate memory\n");
440 goto out_close;
441 }
442
443 strcpy(type, mo->blkdev ? "fuseblk" : "fuse");
444 if (mo->subtype) {
445 strcat(type, ".");
446 strcat(type, mo->subtype);
447 }
448 strcpy(source,
449 mo->fsname ? mo->fsname : (mo->subtype ? mo->subtype : devname));
450
451 res = mount(source, mnt, type, mo->flags, mo->kernel_opts);
452 if (res == -1 && errno == ENODEV && mo->subtype) {
453 /* Probably missing subtype support */
454 strcpy(type, mo->blkdev ? "fuseblk" : "fuse");
455 if (mo->fsname) {
456 if (!mo->blkdev)
457 sprintf(source, "%s#%s", mo->subtype,
458 mo->fsname);
459 } else {
460 strcpy(source, type);
461 }
462 res = mount(source, mnt, type, mo->flags, mo->kernel_opts);
463 }
464 if (res == -1) {
465 /*
466 * Maybe kernel doesn't support unprivileged mounts, in this
467 * case try falling back to fusermount3
468 */
469 if (errno == EPERM) {
470 res = -2;
471 } else {
472 int errno_save = errno;
473 if (mo->blkdev && errno == ENODEV &&
474 !fuse_mnt_check_fuseblk())
475 fuse_log(FUSE_LOG_ERR,
476 "fuse: 'fuseblk' support missing\n");
477 else
478 fuse_log(FUSE_LOG_ERR, "fuse: mount failed: %s\n",
479 strerror(errno_save));
480 }
481
482 goto out_close;
483 }
484
485 #ifndef IGNORE_MTAB
486 if (geteuid() == 0) {
487 char *newmnt = fuse_mnt_resolve_path("fuse", mnt);
488 res = -1;
489 if (!newmnt)
490 goto out_umount;
491
492 res = fuse_mnt_add_mount("fuse", source, newmnt, type,
493 mnt_opts);
494 free(newmnt);
495 if (res == -1)
496 goto out_umount;
497 }
498 #endif /* IGNORE_MTAB */
499 free(type);
500 free(source);
501
502 return fd;
503
504 out_umount:
505 umount2(mnt, 2); /* lazy umount */
506 out_close:
507 free(type);
508 free(source);
509 close(fd);
510 return res;
511 }
512
513 static int get_mnt_flag_opts(char **mnt_optsp, int flags)
514 {
515 int i;
516
517 if (!(flags & MS_RDONLY) && fuse_opt_add_opt(mnt_optsp, "rw") == -1)
518 return -1;
519
520 for (i = 0; mount_flags[i].opt != NULL; i++) {
521 if (mount_flags[i].on && (flags & mount_flags[i].flag) &&
522 fuse_opt_add_opt(mnt_optsp, mount_flags[i].opt) == -1)
523 return -1;
524 }
525 return 0;
526 }
527
528 struct mount_opts *parse_mount_opts(struct fuse_args *args)
529 {
530 struct mount_opts *mo;
531
532 mo = (struct mount_opts*) malloc(sizeof(struct mount_opts));
533 if (mo == NULL)
534 return NULL;
535
536 memset(mo, 0, sizeof(struct mount_opts));
537 mo->flags = MS_NOSUID | MS_NODEV;
538
539 if (args &&
540 fuse_opt_parse(args, mo, fuse_mount_opts, fuse_mount_opt_proc) == -1)
541 goto err_out;
542
543 return mo;
544
545 err_out:
546 destroy_mount_opts(mo);
547 return NULL;
548 }
549
550 void destroy_mount_opts(struct mount_opts *mo)
551 {
552 free(mo->fsname);
553 free(mo->subtype);
554 free(mo->fusermount_opts);
555 free(mo->subtype_opt);
556 free(mo->kernel_opts);
557 free(mo->mtab_opts);
558 free(mo);
559 }
560
561
562 int fuse_kern_mount(const char *mountpoint, struct mount_opts *mo)
563 {
564 int res = -1;
565 char *mnt_opts = NULL;
566
567 res = -1;
568 if (get_mnt_flag_opts(&mnt_opts, mo->flags) == -1)
569 goto out;
570 if (mo->kernel_opts && fuse_opt_add_opt(&mnt_opts, mo->kernel_opts) == -1)
571 goto out;
572 if (mo->mtab_opts && fuse_opt_add_opt(&mnt_opts, mo->mtab_opts) == -1)
573 goto out;
574
575 res = fuse_mount_sys(mountpoint, mo, mnt_opts);
576 if (res == -2) {
577 if (mo->fusermount_opts &&
578 fuse_opt_add_opt(&mnt_opts, mo->fusermount_opts) == -1)
579 goto out;
580
581 if (mo->subtype) {
582 char *tmp_opts = NULL;
583
584 res = -1;
585 if (fuse_opt_add_opt(&tmp_opts, mnt_opts) == -1 ||
586 fuse_opt_add_opt(&tmp_opts, mo->subtype_opt) == -1) {
587 free(tmp_opts);
588 goto out;
589 }
590
591 res = fuse_mount_fusermount(mountpoint, mo, tmp_opts, 1);
592 free(tmp_opts);
593 if (res == -1)
594 res = fuse_mount_fusermount(mountpoint, mo,
595 mnt_opts, 0);
596 } else {
597 res = fuse_mount_fusermount(mountpoint, mo, mnt_opts, 0);
598 }
599 }
600 out:
601 free(mnt_opts);
602 return res;
603 }
fuse_log
void fuse_log(enum fuse_log_level level, const char *fmt,...)
Definition: fuse_log.c:33
FUSE_OPT_KEY
#define FUSE_OPT_KEY(templ, key)
Definition: fuse_opt.h:98
fuse_opt_add_opt_escaped
int fuse_opt_add_opt_escaped(char **opts, const char *opt)
Definition: fuse_opt.c:144
fuse_opt_parse
int fuse_opt_parse(struct fuse_args *args, void *data, const struct fuse_opt opts[], fuse_opt_proc_t proc)
Definition: fuse_opt.c:398
fuse_opt_add_opt
int fuse_opt_add_opt(char **opts, const char *opt)
Definition: fuse_opt.c:139
FUSE_OPT_END
#define FUSE_OPT_END
Definition: fuse_opt.h:104
fuse_args
Definition: fuse_opt.h:109
fuse_opt
Definition: fuse_opt.h:77