2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2005 Jean-Sebastien Pedron
5 * Copyright (c) 2005 Csaba Henk
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/mount.h>
38 #include <sys/sysctl.h>
50 #include <osreldate.h>
55 #ifndef FUSE4BSD_VERSION
56 #define FUSE4BSD_VERSION "0.3.9-pre1"
59 void __usage_short(void);
62 void showversion(void);
63 int init_backgrounded(void);
65 static struct mntopt mopts[] = {
66 #define ALTF_PRIVATE 0x01
67 { "private", 0, ALTF_PRIVATE, 1 },
68 { "neglect_shares", 0, 0x02, 1 },
69 { "push_symlinks_in", 0, 0x04, 1 },
70 { "allow_other", 0, 0x08, 1 },
71 { "default_permissions", 0, 0x10, 1 },
72 #define ALTF_MAXREAD 0x20
73 { "max_read=", 0, ALTF_MAXREAD, 1 },
74 #define ALTF_SUBTYPE 0x40
75 { "subtype=", 0, ALTF_SUBTYPE, 1 },
76 #define ALTF_SYNC_UNMOUNT 0x80
77 { "sync_unmount", 0, ALTF_SYNC_UNMOUNT, 1 },
79 * MOPT_AUTOMOUNTED, included by MOPT_STDOPTS, does not fit into
80 * the 'flags' argument to nmount(2). We have to abuse altflags
81 * to pass it, as string, via iovec.
83 #define ALTF_AUTOMOUNTED 0x100
84 { "automounted", 0, ALTF_AUTOMOUNTED, 1 },
85 /* Linux specific options, we silently ignore them */
86 { "fsname=", 0, 0x00, 1 },
87 { "fd=", 0, 0x00, 1 },
88 { "rootmode=", 0, 0x00, 1 },
89 { "user_id=", 0, 0x00, 1 },
90 { "group_id=", 0, 0x00, 1 },
91 { "large_read", 0, 0x00, 1 },
92 /* "nonempty", just the first two chars are stripped off during parsing */
93 { "nempty", 0, 0x00, 1 },
104 static struct mntval mvals[] = {
105 { ALTF_MAXREAD, NULL, 0 },
106 { ALTF_SUBTYPE, NULL, 0 },
110 #define DEFAULT_MOUNT_FLAGS ALTF_PRIVATE | ALTF_SYNC_UNMOUNT
113 main(int argc, char *argv[])
116 int mntflags, iovlen, verbose = 0;
117 char *dev = NULL, *dir = NULL, mntpath[MAXPATHLEN];
118 char *devo = NULL, *diro = NULL;
119 char ndev[128], fdstr[15];
120 int i, done = 0, reject_allow_other = 0, safe_level = 0;
121 int altflags = DEFAULT_MOUNT_FLAGS;
122 int __altflags = DEFAULT_MOUNT_FLAGS;
126 static struct option longopts[] = {
127 {"reject-allow_other", no_argument, NULL, 'A'},
128 {"safe", no_argument, NULL, 'S'},
129 {"daemon", required_argument, NULL, 'D'},
130 {"daemon_opts", required_argument, NULL, 'O'},
131 {"special", required_argument, NULL, 's'},
132 {"mountpath", required_argument, NULL, 'm'},
133 {"version", no_argument, NULL, 'V'},
134 {"help", no_argument, NULL, 'h'},
140 char *daemon_str = NULL, *daemon_opts = NULL;
143 * We want a parsing routine which is not sensitive to
144 * the position of args/opts; it should extract the
145 * first two args and stop at the beginning of the rest.
146 * (This makes it easier to call mount_fusefs from external
147 * utils than it is with a strict "util flags args" syntax.)
153 /* All in all, I feel it more robust this way... */
154 unsetenv("POSIXLY_CORRECT");
155 if (getenv("MOUNT_FUSEFS_IGNORE_UNKNOWN"))
157 if (getenv("MOUNT_FUSEFS_VERBOSE"))
161 for (i = 0; i < 3; i++) {
162 if (optind < argc && argv[optind][0] != '-') {
176 reject_allow_other = 1;
183 errx(1, "daemon specified inconsistently");
188 errx(1, "daemon opts specified inconsistently");
189 daemon_opts = optarg;
192 getmntopts(optarg, mopts, &mntflags, &altflags);
193 for (mv = mvals; mv->mv_flag; ++mv) {
194 if (! (altflags & mv->mv_flag))
196 for (mo = mopts; mo->m_flag; ++mo) {
199 if (mo->m_flag != mv->mv_flag)
201 p = strstr(optarg, mo->m_option);
203 p += strlen(mo->m_option);
205 while (*q != '\0' && *q != ',')
207 mv->mv_len = q - p + 1;
208 mv->mv_value = malloc(mv->mv_len);
209 memcpy(mv->mv_value, p, mv->mv_len - 1);
210 ((char *)mv->mv_value)[mv->mv_len - 1] = '\0';
218 errx(1, "special specified inconsistently");
223 errx(1, "mount path specified inconsistently");
243 } while ((ch = getopt_long(argc, argv, "AvVho:SD:O:s:m:", longopts, NULL)) != -1);
250 errx(1, "special specified inconsistently");
253 errx(1, "if mountpoint is given via an option, special should also be given via an option");
257 errx(1, "mount path specified inconsistently");
261 if ((! dev) && argc > 0) {
266 if ((! dir) && argc > 0) {
272 errx(1, "missing special and/or mountpoint");
274 for (mo = mopts; mo->m_flag; ++mo) {
275 if (altflags & mo->m_flag) {
278 if (reject_allow_other &&
279 strcmp(mo->m_option, "allow_other") == 0)
281 * reject_allow_other is stronger than a
282 * negative of allow_other: if this is set,
283 * allow_other is blocked, period.
285 errx(1, "\"allow_other\" usage is banned by respective option");
287 for (mv = mvals; mv->mv_flag; ++mv) {
288 if (mo->m_flag != mv->mv_flag)
291 build_iovec(&iov, &iovlen, mo->m_option, mv->mv_value, mv->mv_len);
297 build_iovec(&iov, &iovlen, mo->m_option,
298 __DECONST(void *, ""), -1);
300 if (__altflags & mo->m_flag) {
303 if (asprintf(&uscore_opt, "__%s", mo->m_option) == -1)
304 err(1, "failed to allocate memory");
305 build_iovec(&iov, &iovlen, uscore_opt,
306 __DECONST(void *, ""), -1);
311 if (getenv("MOUNT_FUSEFS_SAFE"))
314 if (safe_level > 0 && (argc > 0 || daemon_str || daemon_opts))
315 errx(1, "safe mode, spawning daemon not allowed");
317 if ((argc > 0 && (daemon_str || daemon_opts)) ||
318 (daemon_opts && ! daemon_str))
319 errx(1, "daemon specified inconsistently");
322 * Resolve the mountpoint with realpath(3) and remove unnecessary
323 * slashes from the devicename if there are any.
325 if (checkpath(dir, mntpath) != 0)
326 err(1, "%s", mntpath);
327 (void)rmslashes(dev, dev);
329 if (strcmp(dev, "auto") == 0)
330 dev = __DECONST(char *, "/dev/fuse");
332 if (strcmp(dev, "/dev/fuse") == 0) {
333 if (! (argc > 0 || daemon_str)) {
334 fprintf(stderr, "Please also specify the fuse daemon to run when mounting via the multiplexer!\n");
337 if ((fd = open(dev, O_RDWR)) < 0)
338 err(1, "failed to open fuse device");
340 fdx = strtol(dev, &ep, 10);
345 /* Identifying device */
350 if (fstat(fd, &sbuf) == -1)
351 err(1, "cannot stat device file descriptor");
353 strcpy(ndev, _PATH_DEV);
354 ndevbas = ndev + strlen(_PATH_DEV);
355 devname_r(sbuf.st_rdev, S_IFCHR, ndevbas,
356 sizeof(ndev) - strlen(_PATH_DEV));
358 if (strncmp(ndevbas, "fuse", 4))
359 errx(1, "mounting inappropriate device");
361 strtol(ndevbas + 4, &lep, 10);
363 errx(1, "mounting inappropriate device");
368 if (argc > 0 || daemon_str) {
371 if (fd < 0 && (fd = open(dev, O_RDWR)) < 0)
372 err(1, "failed to open fuse device");
374 if (asprintf(&fds, "%d", fd) == -1)
375 err(1, "failed to allocate memory");
376 setenv("FUSE_DEV_FD", fds, 1);
378 setenv("FUSE_NO_MOUNT", "1", 1);
385 daemon_opts = __DECONST(char *, "");
387 len = strlen(daemon_str) + 1 + strlen(daemon_opts) +
389 bgdaemon = calloc(1, len);
392 err(1, "failed to allocate memory");
394 strlcpy(bgdaemon, daemon_str, len);
395 strlcat(bgdaemon, " ", len);
396 strlcat(bgdaemon, daemon_opts, len);
397 strlcat(bgdaemon, " &", len);
399 if (system(bgdaemon))
400 err(1, "failed to call fuse daemon");
402 if ((pid = fork()) < 0)
403 err(1, "failed to fork for fuse daemon");
406 execvp(argv[0], argv);
407 err(1, "failed to exec fuse daemon");
412 if (fd >= 0 && ! init_backgrounded() && close(fd) < 0) {
415 err(1, "failed to close fuse device");
418 /* Prepare the options vector for nmount(). build_iovec() is declared
420 sprintf(fdstr, "%d", fd);
421 build_iovec(&iov, &iovlen, "fstype", __DECONST(void *, "fusefs"), -1);
422 build_iovec(&iov, &iovlen, "fspath", mntpath, -1);
423 build_iovec(&iov, &iovlen, "from", dev, -1);
424 build_iovec(&iov, &iovlen, "fd", fdstr, -1);
427 fprintf(stderr, "mounting fuse daemon on device %s\n", dev);
429 if (nmount(iov, iovlen, mntflags) < 0)
430 err(EX_OSERR, "%s on %s", dev, mntpath);
436 __usage_short(void) {
438 "usage:\n%s [-A|-S|-v|-V|-h|-D daemon|-O args|-s special|-m node|-o option...] special node [daemon args...]\n\n",
449 fprintf(stderr, "known options:\n");
450 for (mo = mopts; mo->m_flag; ++mo)
451 fprintf(stderr, "\t%s\n", mo->m_option);
453 fprintf(stderr, "\n(use -h for a detailed description of these options)\n");
460 if (! getenv("MOUNT_FUSEFS_CALL_BY_LIB")) {
462 fprintf(stderr, "description of options:\n");
466 * The main use case of this function is giving info embedded in general
467 * FUSE lib help output. Therefore the style and the content of the output
468 * tries to fit there as much as possible.
471 " -o allow_other allow access to other users\n"
472 /* " -o nonempty allow mounts over non-empty file/dir\n" */
473 " -o default_permissions enable permission checking by kernel\n"
475 " -o fsname=NAME set filesystem name\n"
476 " -o large_read issue large read requests (2.4 only)\n"
478 " -o subtype=NAME set filesystem type\n"
479 " -o max_read=N set maximum size of read requests\n"
480 " -o noprivate allow secondary mounting of the filesystem\n"
481 " -o neglect_shares don't report EBUSY when unmount attempted\n"
482 " in presence of secondary mounts\n"
483 " -o push_symlinks_in prefix absolute symlinks with mountpoint\n"
484 " -o sync_unmount do unmount synchronously\n"
492 puts("mount_fusefs [fuse4bsd] version: " FUSE4BSD_VERSION);
497 init_backgrounded(void)
504 if (sysctlbyname("vfs.fusefs.init_backgrounded", &ibg, &len, NULL, 0))