]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/mount_fusefs/mount_fusefs.c
Remove spurious newline
[FreeBSD/FreeBSD.git] / sbin / mount_fusefs / mount_fusefs.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2005 Jean-Sebastien Pedron
5  * Copyright (c) 2005 Csaba Henk 
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  *
29  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include <sys/param.h>
35 #include <sys/mount.h>
36 #include <sys/uio.h>
37 #include <sys/stat.h>
38 #include <sys/sysctl.h>
39
40 #include <err.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <sysexits.h>
45 #include <unistd.h>
46 #include <fcntl.h>
47 #include <signal.h>
48 #include <getopt.h>
49 #include <limits.h>
50 #include <osreldate.h>
51 #include <paths.h>
52
53 #include "mntopts.h"
54
55 #ifndef FUSE4BSD_VERSION
56 #define FUSE4BSD_VERSION        "0.3.9-pre1"
57 #endif
58
59 void    __usage_short(void);
60 void    usage(void);
61 void    helpmsg(void);
62 void    showversion(void);
63 int     init_backgrounded(void);
64
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 },
78         /*
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.
82          */
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 },
94         MOPT_STDOPTS,
95         MOPT_END
96 };
97
98 struct mntval {
99         int mv_flag;
100         void *mv_value;
101         int mv_len;
102 };
103
104 static struct mntval mvals[] = {
105         { ALTF_MAXREAD, NULL, 0 },
106         { ALTF_SUBTYPE, NULL, 0 },
107         { 0, NULL, 0 }
108 };
109
110 #define DEFAULT_MOUNT_FLAGS ALTF_PRIVATE | ALTF_SYNC_UNMOUNT
111
112 int
113 main(int argc, char *argv[])
114 {
115         struct iovec *iov;
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;
123         int ch = 0;
124         struct mntopt *mo;
125         struct mntval *mv;
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'},
135                 {0,0,0,0}
136         };
137         int pid = 0;
138         int fd = -1, fdx;
139         char *ep;
140         char *daemon_str = NULL, *daemon_opts = NULL;
141
142         /*
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.)
148          */
149
150         iov = NULL;
151         iovlen = 0;
152         mntflags = 0;
153         /* All in all, I feel it more robust this way... */
154         unsetenv("POSIXLY_CORRECT");
155         if (getenv("MOUNT_FUSEFS_IGNORE_UNKNOWN"))
156                 getmnt_silent = 1;
157         if (getenv("MOUNT_FUSEFS_VERBOSE"))
158                 verbose = 1;
159
160         do {
161                 for (i = 0; i < 3; i++) {
162                         if (optind < argc && argv[optind][0] != '-') {
163                                 if (dir) {
164                                         done = 1;
165                                         break;
166                                 }
167                                 if (dev)
168                                         dir = argv[optind];
169                                 else
170                                         dev = argv[optind];
171                                 optind++;
172                         }
173                 }
174                 switch(ch) {
175                 case 'A':
176                         reject_allow_other = 1;
177                         break;
178                 case 'S':
179                         safe_level = 1;
180                         break;
181                 case 'D':
182                         if (daemon_str)
183                                 errx(1, "daemon specified inconsistently");
184                         daemon_str = optarg;
185                         break;
186                 case 'O':
187                         if (daemon_opts)
188                                 errx(1, "daemon opts specified inconsistently");
189                         daemon_opts = optarg;
190                         break;
191                 case 'o':
192                         getmntopts(optarg, mopts, &mntflags, &altflags);
193                         for (mv = mvals; mv->mv_flag; ++mv) {
194                                 if (! (altflags & mv->mv_flag))
195                                         continue;
196                                 for (mo = mopts; mo->m_flag; ++mo) {
197                                         char *p, *q;
198
199                                         if (mo->m_flag != mv->mv_flag)
200                                                 continue;
201                                         p = strstr(optarg, mo->m_option);
202                                         if (p) {
203                                                 p += strlen(mo->m_option);
204                                                 q = p;
205                                                 while (*q != '\0' && *q != ',')
206                                                         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';
211                                                 break;
212                                         }
213                                 }
214                         }
215                         break;
216                 case 's':
217                         if (devo)
218                                 errx(1, "special specified inconsistently");
219                         devo = optarg;
220                         break;
221                 case 'm':
222                         if (diro)
223                                 errx(1, "mount path specified inconsistently");
224                         diro = optarg;
225                         break;
226                 case 'v': 
227                         verbose = 1;
228                         break;
229                 case 'h':
230                         helpmsg();
231                         break;
232                 case 'V':
233                         showversion();
234                         break;
235                 case '\0':
236                         break;
237                 case '?':
238                 default:
239                         usage();
240                 }
241                 if (done)
242                         break;
243         } while ((ch = getopt_long(argc, argv, "AvVho:SD:O:s:m:", longopts, NULL)) != -1);
244
245         argc -= optind;
246         argv += optind;
247
248         if (devo) {
249                 if (dev)
250                         errx(1, "special specified inconsistently");
251                 dev = devo;
252         } else if (diro)
253                 errx(1, "if mountpoint is given via an option, special should also be given via an option"); 
254
255         if (diro) {
256                 if (dir)
257                         errx(1, "mount path specified inconsistently");
258                 dir = diro;
259         }
260
261         if ((! dev) && argc > 0) {
262                 dev = *argv++;
263                 argc--;
264         }
265
266         if ((! dir) && argc > 0) {
267                 dir = *argv++;
268                 argc--;
269         }
270
271         if (! (dev && dir))
272                 errx(1, "missing special and/or mountpoint");
273
274         for (mo = mopts; mo->m_flag; ++mo) {
275                 if (altflags & mo->m_flag) {
276                         int iov_done = 0;
277
278                         if (reject_allow_other &&
279                             strcmp(mo->m_option, "allow_other") == 0)
280                                 /*
281                                  * reject_allow_other is stronger than a
282                                  * negative of allow_other: if this is set,
283                                  * allow_other is blocked, period.
284                                  */
285                                 errx(1, "\"allow_other\" usage is banned by respective option");
286
287                         for (mv = mvals; mv->mv_flag; ++mv) {
288                                 if (mo->m_flag != mv->mv_flag)
289                                         continue;
290                                 if (mv->mv_value) {
291                                         build_iovec(&iov, &iovlen, mo->m_option, mv->mv_value, mv->mv_len);
292                                         iov_done = 1;
293                                         break;
294                                 }
295                         }
296                         if (! iov_done)
297                                 build_iovec(&iov, &iovlen, mo->m_option,
298                                     __DECONST(void *, ""), -1);
299                 }
300                 if (__altflags & mo->m_flag) {
301                         char *uscore_opt;
302
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);
307                         free(uscore_opt);
308                 }
309         }
310
311         if (getenv("MOUNT_FUSEFS_SAFE"))
312                 safe_level = 1;
313
314         if (safe_level > 0 && (argc > 0 || daemon_str || daemon_opts))
315                 errx(1, "safe mode, spawning daemon not allowed");
316
317         if ((argc > 0 && (daemon_str || daemon_opts)) ||
318             (daemon_opts && ! daemon_str))
319                 errx(1, "daemon specified inconsistently");
320
321         /*
322          * Resolve the mountpoint with realpath(3) and remove unnecessary
323          * slashes from the devicename if there are any.
324          */
325         if (checkpath(dir, mntpath) != 0)
326                 err(1, "%s", mntpath);
327         (void)rmslashes(dev, dev);
328
329         if (strcmp(dev, "auto") == 0)
330                 dev = __DECONST(char *, "/dev/fuse");
331
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");
335                         usage();
336                 }
337                 if ((fd = open(dev, O_RDWR)) < 0)
338                         err(1, "failed to open fuse device");
339         } else {
340                 fdx = strtol(dev, &ep, 10);
341                 if (*ep == '\0')
342                         fd = fdx;
343         }
344
345         /* Identifying device */
346         if (fd >= 0) {
347                 struct stat sbuf;
348                 char *ndevbas, *lep;
349
350                 if (fstat(fd, &sbuf) == -1)
351                         err(1, "cannot stat device file descriptor");
352
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));
357
358                 if (strncmp(ndevbas, "fuse", 4))
359                         errx(1, "mounting inappropriate device");
360
361                 strtol(ndevbas + 4, &lep, 10);
362                 if (*lep != '\0')
363                         errx(1, "mounting inappropriate device");
364
365                 dev = ndev;
366         }
367
368         if (argc > 0 || daemon_str) {
369                 char *fds;
370
371                 if (fd < 0 && (fd = open(dev, O_RDWR)) < 0)
372                         err(1, "failed to open fuse device");
373         
374                 if (asprintf(&fds, "%d", fd) == -1)
375                         err(1, "failed to allocate memory");
376                 setenv("FUSE_DEV_FD", fds, 1);
377                 free(fds);
378                 setenv("FUSE_NO_MOUNT", "1", 1);
379
380                 if (daemon_str) {
381                         char *bgdaemon;
382                         int len;
383
384                         if (! daemon_opts)
385                                 daemon_opts = __DECONST(char *, "");
386
387                         len =  strlen(daemon_str) + 1 + strlen(daemon_opts) +
388                             2 + 1;
389                         bgdaemon = calloc(1, len);
390
391                         if (! bgdaemon)
392                                 err(1, "failed to allocate memory");
393
394                         strlcpy(bgdaemon, daemon_str, len);
395                         strlcat(bgdaemon, " ", len);
396                         strlcat(bgdaemon, daemon_opts, len);
397                         strlcat(bgdaemon, " &", len);
398
399                         if (system(bgdaemon))
400                                 err(1, "failed to call fuse daemon");
401                 } else {
402                         if ((pid = fork()) < 0)
403                                 err(1, "failed to fork for fuse daemon");
404
405                         if (pid == 0) {
406                                 execvp(argv[0], argv);
407                                 err(1, "failed to exec fuse daemon");
408                         }
409                 }
410         }
411
412         if (fd >= 0 && ! init_backgrounded() && close(fd) < 0) {
413                 if (pid)
414                         kill(pid, SIGKILL);
415                 err(1, "failed to close fuse device");
416         }
417
418         /* Prepare the options vector for nmount(). build_iovec() is declared
419          * in mntopts.h. */
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);
425
426         if (verbose)
427                 fprintf(stderr, "mounting fuse daemon on device %s\n", dev);
428
429         if (nmount(iov, iovlen, mntflags) < 0)
430                 err(EX_OSERR, "%s on %s", dev, mntpath);
431
432         exit(0);
433 }
434
435 void
436 __usage_short(void) {
437         fprintf(stderr,
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",
439             getprogname());
440 }
441
442 void
443 usage(void)
444 {
445         struct mntopt *mo;
446
447         __usage_short();
448
449         fprintf(stderr, "known options:\n");
450         for (mo = mopts; mo->m_flag; ++mo)
451                 fprintf(stderr, "\t%s\n", mo->m_option);
452
453         fprintf(stderr, "\n(use -h for a detailed description of these options)\n");
454         exit(EX_USAGE);
455 }
456
457 void
458 helpmsg(void)
459 {
460         if (! getenv("MOUNT_FUSEFS_CALL_BY_LIB")) {
461                 __usage_short();
462                 fprintf(stderr, "description of options:\n");
463         }
464
465         /*
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.
469          */
470         fprintf(stderr,
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"
474                 /*
475                 "    -o fsname=NAME         set filesystem name\n"
476                 "    -o large_read          issue large read requests (2.4 only)\n"
477                  */
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"
485                 );
486         exit(EX_USAGE);
487 }
488
489 void
490 showversion(void)
491 {
492         puts("mount_fusefs [fuse4bsd] version: " FUSE4BSD_VERSION);
493         exit(EX_USAGE);
494 }
495
496 int
497 init_backgrounded(void)
498 {
499         int ibg;
500         size_t len;
501
502         len = sizeof(ibg);
503
504         if (sysctlbyname("vfs.fusefs.init_backgrounded", &ibg, &len, NULL, 0))
505                 return (0);
506
507         return (ibg);
508 }