2 * Copyright (c) 2005 Jean-Sebastien Pedron
3 * Copyright (c) 2005 Csaba Henk
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/mount.h>
36 #include <sys/sysctl.h>
48 #include <osreldate.h>
53 #ifndef FUSE4BSD_VERSION
54 #define FUSE4BSD_VERSION "0.3.9-pre1"
57 void __usage_short(void);
60 void showversion(void);
61 int init_backgrounded(void);
63 static struct mntopt mopts[] = {
64 #define ALTF_PRIVATE 0x01
65 { "private", 0, ALTF_PRIVATE, 1 },
66 { "neglect_shares", 0, 0x02, 1 },
67 { "push_symlinks_in", 0, 0x04, 1 },
68 { "allow_other", 0, 0x08, 1 },
69 { "default_permissions", 0, 0x10, 1 },
70 #define ALTF_MAXREAD 0x20
71 { "max_read=", 0, ALTF_MAXREAD, 1 },
72 #define ALTF_SUBTYPE 0x40
73 { "subtype=", 0, ALTF_SUBTYPE, 1 },
74 #define ALTF_SYNC_UNMOUNT 0x80
75 { "sync_unmount", 0, ALTF_SYNC_UNMOUNT, 1 },
76 /* Linux specific options, we silently ignore them */
77 { "fsname=", 0, 0x00, 1 },
78 { "fd=", 0, 0x00, 1 },
79 { "rootmode=", 0, 0x00, 1 },
80 { "user_id=", 0, 0x00, 1 },
81 { "group_id=", 0, 0x00, 1 },
82 { "large_read", 0, 0x00, 1 },
83 /* "nonempty", just the first two chars are stripped off during parsing */
84 { "nempty", 0, 0x00, 1 },
95 static struct mntval mvals[] = {
96 { ALTF_MAXREAD, NULL, 0 },
97 { ALTF_SUBTYPE, NULL, 0 },
101 #define DEFAULT_MOUNT_FLAGS ALTF_PRIVATE | ALTF_SYNC_UNMOUNT
104 main(int argc, char *argv[])
107 int mntflags, iovlen, verbose = 0;
108 char *dev = NULL, *dir = NULL, mntpath[MAXPATHLEN];
109 char *devo = NULL, *diro = NULL;
110 char ndev[128], fdstr[15];
111 int i, done = 0, reject_allow_other = 0, safe_level = 0;
112 int altflags = DEFAULT_MOUNT_FLAGS;
113 int __altflags = DEFAULT_MOUNT_FLAGS;
117 static struct option longopts[] = {
118 {"reject-allow_other", no_argument, NULL, 'A'},
119 {"safe", no_argument, NULL, 'S'},
120 {"daemon", required_argument, NULL, 'D'},
121 {"daemon_opts", required_argument, NULL, 'O'},
122 {"special", required_argument, NULL, 's'},
123 {"mountpath", required_argument, NULL, 'm'},
124 {"version", no_argument, NULL, 'V'},
125 {"help", no_argument, NULL, 'h'},
131 char *daemon_str = NULL, *daemon_opts = NULL;
134 * We want a parsing routine which is not sensitive to
135 * the position of args/opts; it should extract the
136 * first two args and stop at the beginning of the rest.
137 * (This makes it easier to call mount_fusefs from external
138 * utils than it is with a strict "util flags args" syntax.)
144 /* All in all, I feel it more robust this way... */
145 unsetenv("POSIXLY_CORRECT");
146 if (getenv("MOUNT_FUSEFS_IGNORE_UNKNOWN"))
148 if (getenv("MOUNT_FUSEFS_VERBOSE"))
152 for (i = 0; i < 3; i++) {
153 if (optind < argc && argv[optind][0] != '-') {
167 reject_allow_other = 1;
174 errx(1, "daemon specified inconsistently");
179 errx(1, "daemon opts specified inconsistently");
180 daemon_opts = optarg;
183 getmntopts(optarg, mopts, &mntflags, &altflags);
184 for (mv = mvals; mv->mv_flag; ++mv) {
185 if (! (altflags & mv->mv_flag))
187 for (mo = mopts; mo->m_flag; ++mo) {
190 if (mo->m_flag != mv->mv_flag)
192 p = strstr(optarg, mo->m_option);
194 p += strlen(mo->m_option);
196 while (*q != '\0' && *q != ',')
198 mv->mv_len = q - p + 1;
199 mv->mv_value = malloc(mv->mv_len);
200 memcpy(mv->mv_value, p, mv->mv_len - 1);
201 ((char *)mv->mv_value)[mv->mv_len - 1] = '\0';
209 errx(1, "special specified inconsistently");
214 errx(1, "mount path specified inconsistently");
234 } while ((ch = getopt_long(argc, argv, "AvVho:SD:O:s:m:", longopts, NULL)) != -1);
241 errx(1, "special specified inconsistently");
244 errx(1, "if mountpoint is given via an option, special should also be given via an option");
248 errx(1, "mount path specified inconsistently");
252 if ((! dev) && argc > 0) {
257 if ((! dir) && argc > 0) {
263 errx(1, "missing special and/or mountpoint");
265 for (mo = mopts; mo->m_flag; ++mo) {
266 if (altflags & mo->m_flag) {
269 if (reject_allow_other &&
270 strcmp(mo->m_option, "allow_other") == 0)
272 * reject_allow_other is stronger than a
273 * negative of allow_other: if this is set,
274 * allow_other is blocked, period.
276 errx(1, "\"allow_other\" usage is banned by respective option");
278 for (mv = mvals; mv->mv_flag; ++mv) {
279 if (mo->m_flag != mv->mv_flag)
282 build_iovec(&iov, &iovlen, mo->m_option, mv->mv_value, mv->mv_len);
288 build_iovec(&iov, &iovlen, mo->m_option,
289 __DECONST(void *, ""), -1);
291 if (__altflags & mo->m_flag) {
294 if (asprintf(&uscore_opt, "__%s", mo->m_option) == -1)
295 err(1, "failed to allocate memory");
296 build_iovec(&iov, &iovlen, uscore_opt,
297 __DECONST(void *, ""), -1);
302 if (getenv("MOUNT_FUSEFS_SAFE"))
305 if (safe_level > 0 && (argc > 0 || daemon_str || daemon_opts))
306 errx(1, "safe mode, spawning daemon not allowed");
308 if ((argc > 0 && (daemon_str || daemon_opts)) ||
309 (daemon_opts && ! daemon_str))
310 errx(1, "daemon specified inconsistently");
313 * Resolve the mountpoint with realpath(3) and remove unnecessary
314 * slashes from the devicename if there are any.
316 if (checkpath(dir, mntpath) != 0)
317 err(1, "%s", mntpath);
318 (void)rmslashes(dev, dev);
320 if (strcmp(dev, "auto") == 0)
321 dev = __DECONST(char *, "/dev/fuse");
323 if (strcmp(dev, "/dev/fuse") == 0) {
324 if (! (argc > 0 || daemon_str)) {
325 fprintf(stderr, "Please also specify the fuse daemon to run when mounting via the multiplexer!\n");
328 if ((fd = open(dev, O_RDWR)) < 0)
329 err(1, "failed to open fuse device");
331 fdx = strtol(dev, &ep, 10);
336 /* Identifying device */
341 if (fstat(fd, &sbuf) == -1)
342 err(1, "cannot stat device file descriptor");
344 strcpy(ndev, _PATH_DEV);
345 ndevbas = ndev + strlen(_PATH_DEV);
346 devname_r(sbuf.st_rdev, S_IFCHR, ndevbas,
347 sizeof(ndev) - strlen(_PATH_DEV));
349 if (strncmp(ndevbas, "fuse", 4))
350 errx(1, "mounting inappropriate device");
352 strtol(ndevbas + 4, &lep, 10);
354 errx(1, "mounting inappropriate device");
359 if (argc > 0 || daemon_str) {
362 if (fd < 0 && (fd = open(dev, O_RDWR)) < 0)
363 err(1, "failed to open fuse device");
365 if (asprintf(&fds, "%d", fd) == -1)
366 err(1, "failed to allocate memory");
367 setenv("FUSE_DEV_FD", fds, 1);
369 setenv("FUSE_NO_MOUNT", "1", 1);
376 daemon_opts = __DECONST(char *, "");
378 len = strlen(daemon_str) + 1 + strlen(daemon_opts) +
380 bgdaemon = calloc(1, len);
383 err(1, "failed to allocate memory");
385 strlcpy(bgdaemon, daemon_str, len);
386 strlcat(bgdaemon, " ", len);
387 strlcat(bgdaemon, daemon_opts, len);
388 strlcat(bgdaemon, " &", len);
390 if (system(bgdaemon))
391 err(1, "failed to call fuse daemon");
393 if ((pid = fork()) < 0)
394 err(1, "failed to fork for fuse daemon");
397 execvp(argv[0], argv);
398 err(1, "failed to exec fuse daemon");
403 if (fd >= 0 && ! init_backgrounded() && close(fd) < 0) {
406 err(1, "failed to close fuse device");
409 /* Prepare the options vector for nmount(). build_iovec() is declared
411 sprintf(fdstr, "%d", fd);
412 build_iovec(&iov, &iovlen, "fstype", __DECONST(void *, "fusefs"), -1);
413 build_iovec(&iov, &iovlen, "fspath", mntpath, -1);
414 build_iovec(&iov, &iovlen, "from", dev, -1);
415 build_iovec(&iov, &iovlen, "fd", fdstr, -1);
418 fprintf(stderr, "mounting fuse daemon on device %s\n", dev);
420 if (nmount(iov, iovlen, mntflags) < 0)
421 err(EX_OSERR, "%s on %s", dev, mntpath);
427 __usage_short(void) {
429 "usage:\n%s [-A|-S|-v|-V|-h|-D daemon|-O args|-s special|-m node|-o option...] special node [daemon args...]\n\n",
440 fprintf(stderr, "known options:\n");
441 for (mo = mopts; mo->m_flag; ++mo)
442 fprintf(stderr, "\t%s\n", mo->m_option);
444 fprintf(stderr, "\n(use -h for a detailed description of these options)\n");
451 if (! getenv("MOUNT_FUSEFS_CALL_BY_LIB")) {
453 fprintf(stderr, "description of options:\n");
457 * The main use case of this function is giving info embedded in general
458 * FUSE lib help output. Therefore the style and the content of the output
459 * tries to fit there as much as possible.
462 " -o allow_other allow access to other users\n"
463 /* " -o nonempty allow mounts over non-empty file/dir\n" */
464 " -o default_permissions enable permission checking by kernel\n"
466 " -o fsname=NAME set filesystem name\n"
467 " -o large_read issue large read requests (2.4 only)\n"
469 " -o subtype=NAME set filesystem type\n"
470 " -o max_read=N set maximum size of read requests\n"
471 " -o noprivate allow secondary mounting of the filesystem\n"
472 " -o neglect_shares don't report EBUSY when unmount attempted\n"
473 " in presence of secondary mounts\n"
474 " -o push_symlinks_in prefix absolute symlinks with mountpoint\n"
475 " -o sync_unmount do unmount synchronously\n"
483 puts("mount_fusefs [fuse4bsd] version: " FUSE4BSD_VERSION);
488 init_backgrounded(void)
495 if (sysctlbyname("vfs.fuse.init_backgrounded", &ibg, &len, NULL, 0))