2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2005 Jean-Sebastien Pedron
5 * Copyright (c) 2005 Csaba Henk
8 * Copyright (c) 2019 The FreeBSD Foundation
10 * Portions of this software were developed by BFF Storage Systems under
11 * sponsorship from the FreeBSD Foundation.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/param.h>
37 #include <sys/mount.h>
40 #include <sys/sysctl.h>
52 #include <osreldate.h>
57 #ifndef FUSE4BSD_VERSION
58 #define FUSE4BSD_VERSION "0.3.9-pre1"
61 void __usage_short(void);
64 void showversion(void);
66 static struct mntopt mopts[] = {
67 #define ALTF_PRIVATE 0x01
68 { "private", 0, ALTF_PRIVATE, 1 },
69 { "neglect_shares", 0, 0x02, 1 },
70 { "push_symlinks_in", 0, 0x04, 1 },
71 { "allow_other", 0, 0x08, 1 },
72 { "default_permissions", 0, 0x10, 1 },
73 #define ALTF_MAXREAD 0x20
74 { "max_read=", 0, ALTF_MAXREAD, 1 },
75 #define ALTF_SUBTYPE 0x40
76 { "subtype=", 0, ALTF_SUBTYPE, 1 },
77 #define ALTF_FSNAME 0x80
78 { "fsname=", 0, ALTF_FSNAME, 1 },
80 * MOPT_AUTOMOUNTED, included by MOPT_STDOPTS, does not fit into
81 * the 'flags' argument to nmount(2). We have to abuse altflags
82 * to pass it, as string, via iovec.
84 #define ALTF_AUTOMOUNTED 0x100
85 { "automounted", 0, ALTF_AUTOMOUNTED, 1 },
86 #define ALTF_INTR 0x200
87 { "intr", 0, ALTF_INTR, 1 },
88 /* Linux specific options, we silently ignore them */
89 { "fd=", 0, 0x00, 1 },
90 { "rootmode=", 0, 0x00, 1 },
91 { "user_id=", 0, 0x00, 1 },
92 { "group_id=", 0, 0x00, 1 },
93 { "large_read", 0, 0x00, 1 },
94 /* "nonempty", just the first two chars are stripped off during parsing */
95 { "nempty", 0, 0x00, 1 },
96 { "async", 0, MNT_ASYNC, 0},
97 { "noasync", 1, MNT_ASYNC, 0},
108 static struct mntval mvals[] = {
109 { ALTF_MAXREAD, NULL, 0 },
110 { ALTF_SUBTYPE, NULL, 0 },
111 { ALTF_FSNAME, NULL, 0 },
115 #define DEFAULT_MOUNT_FLAGS ALTF_PRIVATE
118 main(int argc, char *argv[])
121 int mntflags, iovlen, verbose = 0;
122 char *dev = NULL, *dir = NULL, mntpath[MAXPATHLEN];
123 char *devo = NULL, *diro = NULL;
124 char ndev[128], fdstr[15];
125 int i, done = 0, reject_allow_other = 0, safe_level = 0;
126 int altflags = DEFAULT_MOUNT_FLAGS;
127 int __altflags = DEFAULT_MOUNT_FLAGS;
131 static struct option longopts[] = {
132 {"reject-allow_other", no_argument, NULL, 'A'},
133 {"safe", no_argument, NULL, 'S'},
134 {"daemon", required_argument, NULL, 'D'},
135 {"daemon_opts", required_argument, NULL, 'O'},
136 {"special", required_argument, NULL, 's'},
137 {"mountpath", required_argument, NULL, 'm'},
138 {"version", no_argument, NULL, 'V'},
139 {"help", no_argument, NULL, 'h'},
145 char *daemon_str = NULL, *daemon_opts = NULL;
148 * We want a parsing routine which is not sensitive to
149 * the position of args/opts; it should extract the
150 * first two args and stop at the beginning of the rest.
151 * (This makes it easier to call mount_fusefs from external
152 * utils than it is with a strict "util flags args" syntax.)
158 /* All in all, I feel it more robust this way... */
159 unsetenv("POSIXLY_CORRECT");
160 if (getenv("MOUNT_FUSEFS_IGNORE_UNKNOWN"))
162 if (getenv("MOUNT_FUSEFS_VERBOSE"))
166 for (i = 0; i < 3; i++) {
167 if (optind < argc && argv[optind][0] != '-') {
181 reject_allow_other = 1;
188 errx(1, "daemon specified inconsistently");
193 errx(1, "daemon opts specified inconsistently");
194 daemon_opts = optarg;
197 getmntopts(optarg, mopts, &mntflags, &altflags);
198 for (mv = mvals; mv->mv_flag; ++mv) {
199 if (! (altflags & mv->mv_flag))
201 for (mo = mopts; mo->m_flag; ++mo) {
204 if (mo->m_flag != mv->mv_flag)
206 p = strstr(optarg, mo->m_option);
208 p += strlen(mo->m_option);
210 while (*q != '\0' && *q != ',')
212 mv->mv_len = q - p + 1;
213 mv->mv_value = malloc(mv->mv_len);
214 if (mv->mv_value == NULL)
216 memcpy(mv->mv_value, p, mv->mv_len - 1);
217 ((char *)mv->mv_value)[mv->mv_len - 1] = '\0';
225 errx(1, "special specified inconsistently");
230 errx(1, "mount path specified inconsistently");
250 } while ((ch = getopt_long(argc, argv, "AvVho:SD:O:s:m:", longopts, NULL)) != -1);
257 errx(1, "special specified inconsistently");
260 errx(1, "if mountpoint is given via an option, special should also be given via an option");
264 errx(1, "mount path specified inconsistently");
268 if ((! dev) && argc > 0) {
273 if ((! dir) && argc > 0) {
279 errx(1, "missing special and/or mountpoint");
281 for (mo = mopts; mo->m_flag; ++mo) {
282 if (altflags & mo->m_flag) {
285 if (reject_allow_other &&
286 strcmp(mo->m_option, "allow_other") == 0)
288 * reject_allow_other is stronger than a
289 * negative of allow_other: if this is set,
290 * allow_other is blocked, period.
292 errx(1, "\"allow_other\" usage is banned by respective option");
294 for (mv = mvals; mv->mv_flag; ++mv) {
295 if (mo->m_flag != mv->mv_flag)
298 build_iovec(&iov, &iovlen, mo->m_option, mv->mv_value, mv->mv_len);
304 build_iovec(&iov, &iovlen, mo->m_option,
305 __DECONST(void *, ""), -1);
307 if (__altflags & mo->m_flag) {
310 if (asprintf(&uscore_opt, "__%s", mo->m_option) == -1)
311 err(1, "failed to allocate memory");
312 build_iovec(&iov, &iovlen, uscore_opt,
313 __DECONST(void *, ""), -1);
318 if (getenv("MOUNT_FUSEFS_SAFE"))
321 if (safe_level > 0 && (argc > 0 || daemon_str || daemon_opts))
322 errx(1, "safe mode, spawning daemon not allowed");
324 if ((argc > 0 && (daemon_str || daemon_opts)) ||
325 (daemon_opts && ! daemon_str))
326 errx(1, "daemon specified inconsistently");
329 * Resolve the mountpoint with realpath(3) and remove unnecessary
330 * slashes from the devicename if there are any.
332 if (checkpath(dir, mntpath) != 0)
333 err(1, "%s", mntpath);
334 (void)rmslashes(dev, dev);
336 if (strcmp(dev, "auto") == 0)
337 dev = __DECONST(char *, "/dev/fuse");
339 if (strcmp(dev, "/dev/fuse") == 0) {
340 if (! (argc > 0 || daemon_str)) {
341 fprintf(stderr, "Please also specify the fuse daemon to run when mounting via the multiplexer!\n");
344 if ((fd = open(dev, O_RDWR)) < 0)
345 err(1, "failed to open fuse device");
347 fdx = strtol(dev, &ep, 10);
352 /* Identifying device */
357 if (fstat(fd, &sbuf) == -1)
358 err(1, "cannot stat device file descriptor");
360 strcpy(ndev, _PATH_DEV);
361 ndevbas = ndev + strlen(_PATH_DEV);
362 devname_r(sbuf.st_rdev, S_IFCHR, ndevbas,
363 sizeof(ndev) - strlen(_PATH_DEV));
365 if (strncmp(ndevbas, "fuse", 4))
366 errx(1, "mounting inappropriate device");
368 strtol(ndevbas + 4, &lep, 10);
370 errx(1, "mounting inappropriate device");
375 if (argc > 0 || daemon_str) {
378 if (fd < 0 && (fd = open(dev, O_RDWR)) < 0)
379 err(1, "failed to open fuse device");
381 if (asprintf(&fds, "%d", fd) == -1)
382 err(1, "failed to allocate memory");
383 setenv("FUSE_DEV_FD", fds, 1);
385 setenv("FUSE_NO_MOUNT", "1", 1);
392 daemon_opts = __DECONST(char *, "");
394 len = strlen(daemon_str) + 1 + strlen(daemon_opts) +
396 bgdaemon = calloc(1, len);
399 err(1, "failed to allocate memory");
401 strlcpy(bgdaemon, daemon_str, len);
402 strlcat(bgdaemon, " ", len);
403 strlcat(bgdaemon, daemon_opts, len);
404 strlcat(bgdaemon, " &", len);
406 if (system(bgdaemon))
407 err(1, "failed to call fuse daemon");
409 if ((pid = fork()) < 0)
410 err(1, "failed to fork for fuse daemon");
413 execvp(argv[0], argv);
414 err(1, "failed to exec fuse daemon");
419 /* Prepare the options vector for nmount(). build_iovec() is declared
421 sprintf(fdstr, "%d", fd);
422 build_iovec(&iov, &iovlen, "fstype", __DECONST(void *, "fusefs"), -1);
423 build_iovec(&iov, &iovlen, "fspath", mntpath, -1);
424 build_iovec(&iov, &iovlen, "from", dev, -1);
425 build_iovec(&iov, &iovlen, "fd", fdstr, -1);
428 fprintf(stderr, "mounting fuse daemon on device %s\n", dev);
430 if (nmount(iov, iovlen, mntflags) < 0)
431 err(EX_OSERR, "%s on %s", dev, mntpath);
437 __usage_short(void) {
439 "usage:\n%s [-A|-S|-v|-V|-h|-D daemon|-O args|-s special|-m node|-o option...] special node [daemon args...]\n\n",
450 fprintf(stderr, "known options:\n");
451 for (mo = mopts; mo->m_flag; ++mo)
452 fprintf(stderr, "\t%s\n", mo->m_option);
454 fprintf(stderr, "\n(use -h for a detailed description of these options)\n");
461 if (! getenv("MOUNT_FUSEFS_CALL_BY_LIB")) {
463 fprintf(stderr, "description of options:\n");
467 * The main use case of this function is giving info embedded in general
468 * FUSE lib help output. Therefore the style and the content of the output
469 * tries to fit there as much as possible.
472 " -o allow_other allow access to other users\n"
473 /* " -o nonempty allow mounts over non-empty file/dir\n" */
474 " -o default_permissions enable permission checking by kernel\n"
475 " -o intr interruptible mount\n"
476 " -o fsname=NAME set filesystem name\n"
478 " -o large_read issue large read requests (2.4 only)\n"
480 " -o subtype=NAME set filesystem type\n"
481 " -o max_read=N set maximum size of read requests\n"
482 " -o noprivate allow secondary mounting of the filesystem\n"
483 " -o neglect_shares don't report EBUSY when unmount attempted\n"
484 " in presence of secondary mounts\n"
485 " -o push_symlinks_in prefix absolute symlinks with mountpoint\n"
493 puts("mount_fusefs [fuse4bsd] version: " FUSE4BSD_VERSION);