]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/mount/mount.c
All the other printf() calls cast to (void) here, do the two newer ones for
[FreeBSD/FreeBSD.git] / sbin / mount / mount.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1980, 1989, 1993, 1994
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32 #ifndef lint
33 static const char copyright[] =
34 "@(#) Copyright (c) 1980, 1989, 1993, 1994\n\
35         The Regents of the University of California.  All rights reserved.\n";
36 #if 0
37 static char sccsid[] = "@(#)mount.c     8.25 (Berkeley) 5/8/95";
38 #endif
39 #endif /* not lint */
40
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43
44 #include <sys/param.h>
45 #include <sys/mount.h>
46 #include <sys/stat.h>
47 #include <sys/wait.h>
48
49 #include <ctype.h>
50 #include <err.h>
51 #include <errno.h>
52 #include <fstab.h>
53 #include <paths.h>
54 #include <pwd.h>
55 #include <signal.h>
56 #include <stdint.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <unistd.h>
61 #include <libutil.h>
62
63 #include "extern.h"
64 #include "mntopts.h"
65 #include "pathnames.h"
66
67 /* `meta' options */
68 #define MOUNT_META_OPTION_FSTAB         "fstab"
69 #define MOUNT_META_OPTION_CURRENT       "current"
70
71 static int debug, fstab_style, verbose;
72
73 struct cpa {
74         char    **a;
75         ssize_t sz;
76         int     c;
77 };
78
79 char   *catopt(char *, const char *);
80 struct statfs *getmntpt(const char *);
81 int     hasopt(const char *, const char *);
82 int     ismounted(struct fstab *, struct statfs *, int);
83 int     isremountable(const char *);
84 void    mangle(char *, struct cpa *);
85 char   *update_options(char *, char *, int);
86 int     mountfs(const char *, const char *, const char *,
87                         int, const char *, const char *);
88 void    remopt(char *, const char *);
89 void    prmount(struct statfs *);
90 void    putfsent(struct statfs *);
91 void    usage(void);
92 char   *flags2opts(int);
93
94 /* Map from mount options to printable formats. */
95 static struct opt {
96         uint64_t o_opt;
97         const char *o_name;
98 } optnames[] = {
99         { MNT_ASYNC,            "asynchronous" },
100         { MNT_EXPORTED,         "NFS exported" },
101         { MNT_LOCAL,            "local" },
102         { MNT_NOATIME,          "noatime" },
103         { MNT_NOEXEC,           "noexec" },
104         { MNT_NOSUID,           "nosuid" },
105         { MNT_NOSYMFOLLOW,      "nosymfollow" },
106         { MNT_QUOTA,            "with quotas" },
107         { MNT_RDONLY,           "read-only" },
108         { MNT_SYNCHRONOUS,      "synchronous" },
109         { MNT_UNION,            "union" },
110         { MNT_NOCLUSTERR,       "noclusterr" },
111         { MNT_NOCLUSTERW,       "noclusterw" },
112         { MNT_SUIDDIR,          "suiddir" },
113         { MNT_SOFTDEP,          "soft-updates" },
114         { MNT_SUJ,              "journaled soft-updates" },
115         { MNT_MULTILABEL,       "multilabel" },
116         { MNT_ACLS,             "acls" },
117         { MNT_NFS4ACLS,         "nfsv4acls" },
118         { MNT_GJOURNAL,         "gjournal" },
119         { MNT_AUTOMOUNTED,      "automounted" },
120         { MNT_VERIFIED,         "verified" },
121         { MNT_UNTRUSTED,        "untrusted" },
122         { MNT_NOCOVER,          "nocover" },
123         { MNT_EMPTYDIR,         "emptydir" },
124         { 0, NULL }
125 };
126
127 /*
128  * List of VFS types that can be remounted without becoming mounted on top
129  * of each other.
130  * XXX Is this list correct?
131  */
132 static const char *
133 remountable_fs_names[] = {
134         "ufs", "ffs", "ext2fs",
135         0
136 };
137
138 static const char userquotaeq[] = "userquota=";
139 static const char groupquotaeq[] = "groupquota=";
140
141 static char *mountprog = NULL;
142
143 static int
144 use_mountprog(const char *vfstype)
145 {
146         /* XXX: We need to get away from implementing external mount
147          *      programs for every filesystem, and move towards having
148          *      each filesystem properly implement the nmount() system call.
149          */
150         unsigned int i;
151         const char *fs[] = {
152         "cd9660", "mfs", "msdosfs", "nfs",
153         "nullfs", "smbfs", "udf", "unionfs",
154         NULL
155         };
156
157         if (mountprog != NULL)
158                 return (1);
159
160         for (i = 0; fs[i] != NULL; ++i) {
161                 if (strcmp(vfstype, fs[i]) == 0)
162                         return (1);
163         }
164
165         return (0);
166 }
167
168 static int
169 exec_mountprog(const char *name, const char *execname, char *const argv[])
170 {
171         pid_t pid;
172         int status;
173
174         switch (pid = fork()) {
175         case -1:                                /* Error. */
176                 warn("fork");
177                 exit (1);
178         case 0:                                 /* Child. */
179                 /* Go find an executable. */
180                 execvP(execname, _PATH_SYSPATH, argv);
181                 if (errno == ENOENT) {
182                         warn("exec %s not found", execname);
183                         if (execname[0] != '/') {
184                                 warnx("in path: %s", _PATH_SYSPATH);
185                         }
186                 }
187                 exit(1);
188         default:                                /* Parent. */
189                 if (waitpid(pid, &status, 0) < 0) {
190                         warn("waitpid");
191                         return (1);
192                 }
193
194                 if (WIFEXITED(status)) {
195                         if (WEXITSTATUS(status) != 0)
196                                 return (WEXITSTATUS(status));
197                 } else if (WIFSIGNALED(status)) {
198                         warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
199                         return (1);
200                 }
201                 break;
202         }
203
204         return (0);
205 }
206
207 static int
208 specified_ro(const char *arg)
209 {
210         char *optbuf, *opt;
211         int ret = 0;
212
213         optbuf = strdup(arg);
214         if (optbuf == NULL)
215                  err(1, NULL);
216
217         for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
218                 if (strcmp(opt, "ro") == 0) {
219                         ret = 1;
220                         break;
221                 }
222         }
223         free(optbuf);
224         return (ret);
225 }
226
227 static void
228 restart_mountd(void)
229 {
230         struct pidfh *pfh;
231         pid_t mountdpid;
232
233         mountdpid = 0;
234         pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid);
235         if (pfh != NULL) {
236                 /* Mountd is not running. */
237                 pidfile_remove(pfh);
238                 return;
239         }
240         if (errno != EEXIST) {
241                 /* Cannot open pidfile for some reason. */
242                 return;
243         }
244
245         /*
246          * Refuse to send broadcast or group signals, this has
247          * happened due to the bugs in pidfile(3).
248          */
249         if (mountdpid <= 0) {
250                 warnx("mountd pid %d, refusing to send SIGHUP", mountdpid);
251                 return;
252         }
253
254         /* We have mountd(8) PID in mountdpid varible, let's signal it. */
255         if (kill(mountdpid, SIGHUP) == -1)
256                 err(1, "signal mountd");
257 }
258
259 int
260 main(int argc, char *argv[])
261 {
262         const char *mntfromname, **vfslist, *vfstype;
263         struct fstab *fs;
264         struct statfs *mntbuf;
265         int all, ch, i, init_flags, late, failok, mntsize, rval, have_fstab, ro;
266         int onlylate;
267         char *cp, *ep, *options;
268
269         all = init_flags = late = onlylate = 0;
270         ro = 0;
271         options = NULL;
272         vfslist = NULL;
273         vfstype = "ufs";
274         while ((ch = getopt(argc, argv, "adF:fLlno:prt:uvw")) != -1)
275                 switch (ch) {
276                 case 'a':
277                         all = 1;
278                         break;
279                 case 'd':
280                         debug = 1;
281                         break;
282                 case 'F':
283                         setfstab(optarg);
284                         break;
285                 case 'f':
286                         init_flags |= MNT_FORCE;
287                         break;
288                 case 'L':
289                         onlylate = 1;
290                         late = 1;
291                         break;
292                 case 'l':
293                         late = 1;
294                         break;
295                 case 'n':
296                         /* For compatibility with the Linux version of mount. */
297                         break;
298                 case 'o':
299                         if (*optarg) {
300                                 options = catopt(options, optarg);
301                                 if (specified_ro(optarg))
302                                         ro = 1;
303                         }
304                         break;
305                 case 'p':
306                         fstab_style = 1;
307                         verbose = 1;
308                         break;
309                 case 'r':
310                         options = catopt(options, "ro");
311                         ro = 1;
312                         break;
313                 case 't':
314                         if (vfslist != NULL)
315                                 errx(1, "only one -t option may be specified");
316                         vfslist = makevfslist(optarg);
317                         vfstype = optarg;
318                         break;
319                 case 'u':
320                         init_flags |= MNT_UPDATE;
321                         break;
322                 case 'v':
323                         verbose = 1;
324                         break;
325                 case 'w':
326                         options = catopt(options, "noro");
327                         break;
328                 case '?':
329                 default:
330                         usage();
331                         /* NOTREACHED */
332                 }
333         argc -= optind;
334         argv += optind;
335
336 #define BADTYPE(type)                                                   \
337         (strcmp(type, FSTAB_RO) &&                                      \
338             strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
339
340         if ((init_flags & MNT_UPDATE) && (ro == 0))
341                 options = catopt(options, "noro");
342
343         rval = 0;
344         switch (argc) {
345         case 0:
346                 if ((mntsize = getmntinfo(&mntbuf,
347                      verbose ? MNT_WAIT : MNT_NOWAIT)) == 0)
348                         err(1, "getmntinfo");
349                 if (all) {
350                         while ((fs = getfsent()) != NULL) {
351                                 if (BADTYPE(fs->fs_type))
352                                         continue;
353                                 if (checkvfsname(fs->fs_vfstype, vfslist))
354                                         continue;
355                                 if (hasopt(fs->fs_mntops, "noauto"))
356                                         continue;
357                                 if (!hasopt(fs->fs_mntops, "late") && onlylate)
358                                         continue;
359                                 if (hasopt(fs->fs_mntops, "late") && !late)
360                                         continue;
361                                 if (hasopt(fs->fs_mntops, "failok"))
362                                         failok = 1;
363                                 else
364                                         failok = 0;
365                                 if (!(init_flags & MNT_UPDATE) &&
366                                     !hasopt(fs->fs_mntops, "update") &&
367                                     ismounted(fs, mntbuf, mntsize))
368                                         continue;
369                                 options = update_options(options, fs->fs_mntops,
370                                     mntbuf->f_flags);
371                                 if (mountfs(fs->fs_vfstype, fs->fs_spec,
372                                     fs->fs_file, init_flags, options,
373                                     fs->fs_mntops) && !failok)
374                                         rval = 1;
375                         }
376                 } else if (fstab_style) {
377                         for (i = 0; i < mntsize; i++) {
378                                 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
379                                         continue;
380                                 putfsent(&mntbuf[i]);
381                         }
382                 } else {
383                         for (i = 0; i < mntsize; i++) {
384                                 if (checkvfsname(mntbuf[i].f_fstypename,
385                                     vfslist))
386                                         continue;
387                                 if (!verbose &&
388                                     (mntbuf[i].f_flags & MNT_IGNORE) != 0)
389                                         continue;
390                                 prmount(&mntbuf[i]);
391                         }
392                 }
393                 exit(rval);
394         case 1:
395                 if (vfslist != NULL)
396                         usage();
397
398                 rmslashes(*argv, *argv);
399                 if (init_flags & MNT_UPDATE) {
400                         mntfromname = NULL;
401                         have_fstab = 0;
402                         if ((mntbuf = getmntpt(*argv)) == NULL)
403                                 errx(1, "not currently mounted %s", *argv);
404                         /*
405                          * Only get the mntflags from fstab if both mntpoint
406                          * and mntspec are identical. Also handle the special
407                          * case where just '/' is mounted and 'spec' is not
408                          * identical with the one from fstab ('/dev' is missing
409                          * in the spec-string at boot-time).
410                          */
411                         if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
412                                 if (strcmp(fs->fs_spec,
413                                     mntbuf->f_mntfromname) == 0 &&
414                                     strcmp(fs->fs_file,
415                                     mntbuf->f_mntonname) == 0) {
416                                         have_fstab = 1;
417                                         mntfromname = mntbuf->f_mntfromname;
418                                 } else if (argv[0][0] == '/' &&
419                                     argv[0][1] == '\0' &&
420                                     strcmp(fs->fs_vfstype,
421                                     mntbuf->f_fstypename) == 0) {
422                                         fs = getfsfile("/");
423                                         have_fstab = 1;
424                                         mntfromname = fs->fs_spec;
425                                 }
426                         }
427                         if (have_fstab) {
428                                 options = update_options(options, fs->fs_mntops,
429                                     mntbuf->f_flags);
430                         } else {
431                                 mntfromname = mntbuf->f_mntfromname;
432                                 options = update_options(options, NULL,
433                                     mntbuf->f_flags);
434                         }
435                         rval = mountfs(mntbuf->f_fstypename, mntfromname,
436                             mntbuf->f_mntonname, init_flags, options, 0);
437                         break;
438                 }
439                 if ((fs = getfsfile(*argv)) == NULL &&
440                     (fs = getfsspec(*argv)) == NULL)
441                         errx(1, "%s: unknown special file or file system",
442                             *argv);
443                 if (BADTYPE(fs->fs_type))
444                         errx(1, "%s has unknown file system type",
445                             *argv);
446                 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
447                     init_flags, options, fs->fs_mntops);
448                 break;
449         case 2:
450                 /*
451                  * If -t flag has not been specified, the path cannot be
452                  * found, spec contains either a ':' or a '@', then assume
453                  * that an NFS file system is being specified ala Sun.
454                  * Check if the hostname contains only allowed characters
455                  * to reduce false positives.  IPv6 addresses containing
456                  * ':' will be correctly parsed only if the separator is '@'.
457                  * The definition of a valid hostname is taken from RFC 1034.
458                  */
459                 if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL ||
460                     (ep = strchr(argv[0], ':')) != NULL)) {
461                         if (*ep == '@') {
462                                 cp = ep + 1;
463                                 ep = cp + strlen(cp);
464                         } else
465                                 cp = argv[0];
466                         while (cp != ep) {
467                                 if (!isdigit(*cp) && !isalpha(*cp) &&
468                                     *cp != '.' && *cp != '-' && *cp != ':')
469                                         break;
470                                 cp++;
471                         }
472                         if (cp == ep)
473                                 vfstype = "nfs";
474                 }
475                 rval = mountfs(vfstype,
476                     argv[0], argv[1], init_flags, options, NULL);
477                 break;
478         default:
479                 usage();
480                 /* NOTREACHED */
481         }
482
483         /*
484          * If the mount was successfully, and done by root, tell mountd the
485          * good news.
486          */
487         if (rval == 0 && getuid() == 0)
488                 restart_mountd();
489
490         exit(rval);
491 }
492
493 int
494 ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
495 {
496         char realfsfile[PATH_MAX];
497         int i;
498
499         if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
500                 /* the root file system can always be remounted */
501                 return (0);
502
503         /* The user may have specified a symlink in fstab, resolve the path */
504         if (realpath(fs->fs_file, realfsfile) == NULL) {
505                 /* Cannot resolve the path, use original one */
506                 strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile));
507         }
508
509         /* 
510          * Consider the filesystem to be mounted if:
511          * It has the same mountpoint as a mounted filesytem, and
512          * It has the same type as that same mounted filesystem, and
513          * It has the same device name as that same mounted filesystem, OR
514          *     It is a nonremountable filesystem
515          */
516         for (i = mntsize - 1; i >= 0; --i)
517                 if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 &&
518                     strcmp(fs->fs_vfstype, mntbuf[i].f_fstypename) == 0 && 
519                     (!isremountable(fs->fs_vfstype) ||
520                      (strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0)))
521                         return (1);
522         return (0);
523 }
524
525 int
526 isremountable(const char *vfsname)
527 {
528         const char **cp;
529
530         for (cp = remountable_fs_names; *cp; cp++)
531                 if (strcmp(*cp, vfsname) == 0)
532                         return (1);
533         return (0);
534 }
535
536 int
537 hasopt(const char *mntopts, const char *option)
538 {
539         int negative, found;
540         char *opt, *optbuf;
541
542         if (option[0] == 'n' && option[1] == 'o') {
543                 negative = 1;
544                 option += 2;
545         } else
546                 negative = 0;
547         optbuf = strdup(mntopts);
548         found = 0;
549         for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
550                 if (opt[0] == 'n' && opt[1] == 'o') {
551                         if (!strcasecmp(opt + 2, option))
552                                 found = negative;
553                 } else if (!strcasecmp(opt, option))
554                         found = !negative;
555         }
556         free(optbuf);
557         return (found);
558 }
559
560 static void
561 append_arg(struct cpa *sa, char *arg)
562 {
563         if (sa->c + 1 == sa->sz) {
564                 sa->sz = sa->sz == 0 ? 8 : sa->sz * 2;
565                 sa->a = realloc(sa->a, sizeof(*sa->a) * sa->sz);
566                 if (sa->a == NULL)
567                         errx(1, "realloc failed");
568         }
569         sa->a[++sa->c] = arg;
570 }
571
572 int
573 mountfs(const char *vfstype, const char *spec, const char *name, int flags,
574         const char *options, const char *mntopts)
575 {
576         struct statfs sf;
577         int i, ret;
578         char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX];
579         static struct cpa mnt_argv;
580
581         /* resolve the mountpoint with realpath(3) */
582         if (checkpath(name, mntpath) != 0) {
583                 warn("%s", mntpath);
584                 return (1);
585         }
586         name = mntpath;
587
588         if (mntopts == NULL)
589                 mntopts = "";
590         optbuf = catopt(strdup(mntopts), options);
591
592         if (strcmp(name, "/") == 0)
593                 flags |= MNT_UPDATE;
594         if (flags & MNT_FORCE)
595                 optbuf = catopt(optbuf, "force");
596         if (flags & MNT_RDONLY)
597                 optbuf = catopt(optbuf, "ro");
598         /*
599          * XXX
600          * The mount_mfs (newfs) command uses -o to select the
601          * optimization mode.  We don't pass the default "-o rw"
602          * for that reason.
603          */
604         if (flags & MNT_UPDATE)
605                 optbuf = catopt(optbuf, "update");
606
607         /* Compatibility glue. */
608         if (strcmp(vfstype, "msdos") == 0)
609                 vfstype = "msdosfs";
610
611         /* Construct the name of the appropriate mount command */
612         (void)snprintf(execname, sizeof(execname), "mount_%s", vfstype);
613
614         mnt_argv.c = -1;
615         append_arg(&mnt_argv, execname);
616         mangle(optbuf, &mnt_argv);
617         if (mountprog != NULL)
618                 strlcpy(execname, mountprog, sizeof(execname));
619
620         append_arg(&mnt_argv, strdup(spec));
621         append_arg(&mnt_argv, strdup(name));
622         append_arg(&mnt_argv, NULL);
623
624         if (debug) {
625                 if (use_mountprog(vfstype))
626                         printf("exec: %s", execname);
627                 else
628                         printf("mount -t %s", vfstype);
629                 for (i = 1; i < mnt_argv.c; i++)
630                         (void)printf(" %s", mnt_argv.a[i]);
631                 (void)printf("\n");
632                 free(optbuf);
633                 free(mountprog);
634                 mountprog = NULL;
635                 return (0);
636         }
637
638         if (use_mountprog(vfstype)) {
639                 ret = exec_mountprog(name, execname, mnt_argv.a);
640         } else {
641                 ret = mount_fs(vfstype, mnt_argv.c, mnt_argv.a);
642         }
643
644         free(optbuf);
645         free(mountprog);
646         mountprog = NULL;
647
648         if (verbose) {
649                 if (statfs(name, &sf) < 0) {
650                         warn("statfs %s", name);
651                         return (1);
652                 }
653                 if (fstab_style)
654                         putfsent(&sf);
655                 else
656                         prmount(&sf);
657         }
658
659         return (ret);
660 }
661
662 void
663 prmount(struct statfs *sfp)
664 {
665         uint64_t flags;
666         unsigned int i;
667         struct opt *o;
668         struct passwd *pw;
669
670         (void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
671             sfp->f_fstypename);
672
673         flags = sfp->f_flags & MNT_VISFLAGMASK;
674         for (o = optnames; flags != 0 && o->o_opt != 0; o++)
675                 if (flags & o->o_opt) {
676                         (void)printf(", %s", o->o_name);
677                         flags &= ~o->o_opt;
678                 }
679         /*
680          * Inform when file system is mounted by an unprivileged user
681          * or privileged non-root user.
682          */
683         if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) {
684                 (void)printf(", mounted by ");
685                 if ((pw = getpwuid(sfp->f_owner)) != NULL)
686                         (void)printf("%s", pw->pw_name);
687                 else
688                         (void)printf("%d", sfp->f_owner);
689         }
690         if (verbose) {
691                 if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
692                         (void)printf(", writes: sync %ju async %ju",
693                             (uintmax_t)sfp->f_syncwrites,
694                             (uintmax_t)sfp->f_asyncwrites);
695                 if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
696                         (void)printf(", reads: sync %ju async %ju",
697                             (uintmax_t)sfp->f_syncreads,
698                             (uintmax_t)sfp->f_asyncreads);
699                 if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) {
700                         (void)printf(", fsid ");
701                         for (i = 0; i < sizeof(sfp->f_fsid); i++)
702                                 (void)printf("%02x", ((u_char *)&sfp->f_fsid)[i]);
703                 }
704         }
705         (void)printf(")\n");
706 }
707
708 struct statfs *
709 getmntpt(const char *name)
710 {
711         struct statfs *mntbuf;
712         int i, mntsize;
713
714         mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
715         for (i = mntsize - 1; i >= 0; i--) {
716                 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
717                     strcmp(mntbuf[i].f_mntonname, name) == 0)
718                         return (&mntbuf[i]);
719         }
720         return (NULL);
721 }
722
723 char *
724 catopt(char *s0, const char *s1)
725 {
726         char *cp;
727
728         if (s1 == NULL || *s1 == '\0')
729                 return (s0);
730
731         if (s0 && *s0) {
732                 if (asprintf(&cp, "%s,%s", s0, s1) == -1)
733                         errx(1, "asprintf failed");
734         } else
735                 cp = strdup(s1);
736
737         if (s0)
738                 free(s0);
739         return (cp);
740 }
741
742 void
743 mangle(char *options, struct cpa *a)
744 {
745         char *p, *s, *val;
746
747         for (s = options; (p = strsep(&s, ",")) != NULL;)
748                 if (*p != '\0') {
749                         if (strcmp(p, "noauto") == 0) {
750                                 /*
751                                  * Do not pass noauto option to nmount().
752                                  * or external mount program.  noauto is
753                                  * only used to prevent mounting a filesystem
754                                  * when 'mount -a' is specified, and is
755                                  * not a real mount option.
756                                  */
757                                 continue;
758                         } else if (strcmp(p, "late") == 0) {
759                                 /*
760                                  * "late" is used to prevent certain file
761                                  * systems from being mounted before late
762                                  * in the boot cycle; for instance,
763                                  * loopback NFS mounts can't be mounted
764                                  * before mountd starts.
765                                  */
766                                 continue;
767                         } else if (strcmp(p, "failok") == 0) {
768                                 /*
769                                  * "failok" is used to prevent certain file
770                                  * systems from being causing the system to
771                                  * drop into single user mode in the boot
772                                  * cycle, and is not a real mount option.
773                                  */
774                                 continue;
775                         } else if (strncmp(p, "mountprog", 9) == 0) {
776                                 /*
777                                  * "mountprog" is used to force the use of
778                                  * userland mount programs.
779                                  */
780                                 val = strchr(p, '=');
781                                 if (val != NULL) {
782                                         ++val;
783                                         if (*val != '\0')
784                                                 mountprog = strdup(val);
785                                 }
786
787                                 if (mountprog == NULL) {
788                                         errx(1, "Need value for -o mountprog");
789                                 }
790                                 continue;
791                         } else if (strcmp(p, "userquota") == 0) {
792                                 continue;
793                         } else if (strncmp(p, userquotaeq,
794                             sizeof(userquotaeq) - 1) == 0) {
795                                 continue;
796                         } else if (strcmp(p, "groupquota") == 0) {
797                                 continue;
798                         } else if (strncmp(p, groupquotaeq,
799                             sizeof(groupquotaeq) - 1) == 0) {
800                                 continue;
801                         } else if (*p == '-') {
802                                 append_arg(a, p);
803                                 p = strchr(p, '=');
804                                 if (p != NULL) {
805                                         *p = '\0';
806                                         append_arg(a, p + 1);
807                                 }
808                         } else {
809                                 append_arg(a, strdup("-o"));
810                                 append_arg(a, p);
811                         }
812                 }
813 }
814
815
816 char *
817 update_options(char *opts, char *fstab, int curflags)
818 {
819         char *o, *p;
820         char *cur;
821         char *expopt, *newopt, *tmpopt;
822
823         if (opts == NULL)
824                 return (strdup(""));
825
826         /* remove meta options from list */
827         remopt(fstab, MOUNT_META_OPTION_FSTAB);
828         remopt(fstab, MOUNT_META_OPTION_CURRENT);
829         cur = flags2opts(curflags);
830
831         /*
832          * Expand all meta-options passed to us first.
833          */
834         expopt = NULL;
835         for (p = opts; (o = strsep(&p, ",")) != NULL;) {
836                 if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
837                         expopt = catopt(expopt, fstab);
838                 else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
839                         expopt = catopt(expopt, cur);
840                 else
841                         expopt = catopt(expopt, o);
842         }
843         free(cur);
844         free(opts);
845
846         /*
847          * Remove previous contradictory arguments. Given option "foo" we
848          * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
849          * and "foo" - so we can deal with possible options like "notice".
850          */
851         newopt = NULL;
852         for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
853                 if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
854                         errx(1, "malloc failed");
855
856                 strcpy(tmpopt, "no");
857                 strcat(tmpopt, o);
858                 remopt(newopt, tmpopt);
859                 free(tmpopt);
860
861                 if (strncmp("no", o, 2) == 0)
862                         remopt(newopt, o+2);
863
864                 newopt = catopt(newopt, o);
865         }
866         free(expopt);
867
868         return (newopt);
869 }
870
871 void
872 remopt(char *string, const char *opt)
873 {
874         char *o, *p, *r;
875
876         if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
877                 return;
878
879         r = string;
880
881         for (p = string; (o = strsep(&p, ",")) != NULL;) {
882                 if (strcmp(opt, o) != 0) {
883                         if (*r == ',' && *o != '\0')
884                                 r++;
885                         while ((*r++ = *o++) != '\0')
886                             ;
887                         *--r = ',';
888                 }
889         }
890         *r = '\0';
891 }
892
893 void
894 usage(void)
895 {
896
897         (void)fprintf(stderr, "%s\n%s\n%s\n",
898 "usage: mount [-adflpruvw] [-F fstab] [-o options] [-t ufs | external_type]",
899 "       mount [-dfpruvw] special | node",
900 "       mount [-dfpruvw] [-o options] [-t ufs | external_type] special node");
901         exit(1);
902 }
903
904 void
905 putfsent(struct statfs *ent)
906 {
907         struct fstab *fst;
908         char *opts, *rw;
909         int l;
910
911         opts = NULL;
912         /* flags2opts() doesn't return the "rw" option. */
913         if ((ent->f_flags & MNT_RDONLY) != 0)
914                 rw = NULL;
915         else
916                 rw = catopt(NULL, "rw");
917
918         opts = flags2opts(ent->f_flags);
919         opts = catopt(rw, opts);
920
921         if (strncmp(ent->f_mntfromname, "<below>", 7) == 0 ||
922             strncmp(ent->f_mntfromname, "<above>", 7) == 0) {
923                 strlcpy(ent->f_mntfromname,
924                     (strnstr(ent->f_mntfromname, ":", 8) +1),
925                     sizeof(ent->f_mntfromname));
926         }
927
928         l = strlen(ent->f_mntfromname);
929         printf("%s%s%s%s", ent->f_mntfromname,
930             l < 8 ? "\t" : "",
931             l < 16 ? "\t" : "",
932             l < 24 ? "\t" : " ");
933         l = strlen(ent->f_mntonname);
934         printf("%s%s%s%s", ent->f_mntonname,
935             l < 8 ? "\t" : "",
936             l < 16 ? "\t" : "",
937             l < 24 ? "\t" : " ");
938         printf("%s\t", ent->f_fstypename);
939         l = strlen(opts);
940         printf("%s%s", opts,
941             l < 8 ? "\t" : " ");
942         free(opts);
943
944         if ((fst = getfsspec(ent->f_mntfromname)))
945                 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
946         else if ((fst = getfsfile(ent->f_mntonname)))
947                 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
948         else if (strcmp(ent->f_fstypename, "ufs") == 0) {
949                 if (strcmp(ent->f_mntonname, "/") == 0)
950                         printf("\t1 1\n");
951                 else
952                         printf("\t2 2\n");
953         } else
954                 printf("\t0 0\n");
955 }
956
957
958 char *
959 flags2opts(int flags)
960 {
961         char *res;
962
963         res = NULL;
964
965         if (flags & MNT_RDONLY)         res = catopt(res, "ro");
966         if (flags & MNT_SYNCHRONOUS)    res = catopt(res, "sync");
967         if (flags & MNT_NOEXEC)         res = catopt(res, "noexec");
968         if (flags & MNT_NOSUID)         res = catopt(res, "nosuid");
969         if (flags & MNT_UNION)          res = catopt(res, "union");
970         if (flags & MNT_ASYNC)          res = catopt(res, "async");
971         if (flags & MNT_NOATIME)        res = catopt(res, "noatime");
972         if (flags & MNT_NOCLUSTERR)     res = catopt(res, "noclusterr");
973         if (flags & MNT_NOCLUSTERW)     res = catopt(res, "noclusterw");
974         if (flags & MNT_NOSYMFOLLOW)    res = catopt(res, "nosymfollow");
975         if (flags & MNT_SUIDDIR)        res = catopt(res, "suiddir");
976         if (flags & MNT_MULTILABEL)     res = catopt(res, "multilabel");
977         if (flags & MNT_ACLS)           res = catopt(res, "acls");
978         if (flags & MNT_NFS4ACLS)       res = catopt(res, "nfsv4acls");
979         if (flags & MNT_UNTRUSTED)      res = catopt(res, "untrusted");
980         if (flags & MNT_NOCOVER)        res = catopt(res, "nocover");
981         if (flags & MNT_EMPTYDIR)       res = catopt(res, "emptydir");
982
983         return (res);
984 }