2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1989, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
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";
37 static char sccsid[] = "@(#)mount.c 8.25 (Berkeley) 5/8/95";
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/param.h>
45 #define _WANT_MNTOPTNAMES
46 #include <sys/mount.h>
66 #include "pathnames.h"
69 #define MOUNT_META_OPTION_FSTAB "fstab"
70 #define MOUNT_META_OPTION_CURRENT "current"
72 static int debug, fstab_style, verbose;
80 char *catopt(char *, const char *);
81 struct statfs *getmntpt(const char *);
82 int hasopt(const char *, const char *);
83 int ismounted(struct fstab *, struct statfs *, int);
84 int isremountable(const char *);
85 void mangle(char *, struct cpa *);
86 char *update_options(char *, char *, int);
87 int mountfs(const char *, const char *, const char *,
88 int, const char *, const char *);
89 void remopt(char *, const char *);
90 void prmount(struct statfs *);
91 void putfsent(struct statfs *);
93 char *flags2opts(int);
95 /* Map from mount options to printable formats. */
96 static struct mntoptnames optnames[] = {
101 * List of VFS types that can be remounted without becoming mounted on top
103 * XXX Is this list correct?
106 remountable_fs_names[] = {
107 "ufs", "ffs", "ext2fs",
111 static const char userquotaeq[] = "userquota=";
112 static const char groupquotaeq[] = "groupquota=";
114 static char *mountprog = NULL;
117 use_mountprog(const char *vfstype)
119 /* XXX: We need to get away from implementing external mount
120 * programs for every filesystem, and move towards having
121 * each filesystem properly implement the nmount() system call.
125 "cd9660", "mfs", "msdosfs", "nfs",
126 "nullfs", "smbfs", "udf", "unionfs",
130 if (mountprog != NULL)
133 for (i = 0; fs[i] != NULL; ++i) {
134 if (strcmp(vfstype, fs[i]) == 0)
142 exec_mountprog(const char *name, const char *execname, char *const argv[])
147 switch (pid = fork()) {
148 case -1: /* Error. */
152 /* Go find an executable. */
153 execvP(execname, _PATH_SYSPATH, argv);
154 if (errno == ENOENT) {
155 warn("exec %s not found", execname);
156 if (execname[0] != '/') {
157 warnx("in path: %s", _PATH_SYSPATH);
161 default: /* Parent. */
162 if (waitpid(pid, &status, 0) < 0) {
167 if (WIFEXITED(status)) {
168 if (WEXITSTATUS(status) != 0)
169 return (WEXITSTATUS(status));
170 } else if (WIFSIGNALED(status)) {
171 warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
181 specified_ro(const char *arg)
186 optbuf = strdup(arg);
190 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
191 if (strcmp(opt, "ro") == 0) {
207 pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid);
209 /* Mountd is not running. */
213 if (errno != EEXIST) {
214 /* Cannot open pidfile for some reason. */
219 * Refuse to send broadcast or group signals, this has
220 * happened due to the bugs in pidfile(3).
222 if (mountdpid <= 0) {
223 warnx("mountd pid %d, refusing to send SIGHUP", mountdpid);
227 /* We have mountd(8) PID in mountdpid varible, let's signal it. */
228 if (kill(mountdpid, SIGHUP) == -1)
229 err(1, "signal mountd");
233 main(int argc, char *argv[])
235 const char *mntfromname, **vfslist, *vfstype;
237 struct statfs *mntbuf;
238 int all, ch, i, init_flags, late, failok, mntsize, rval, have_fstab, ro;
240 char *cp, *ep, *options;
242 all = init_flags = late = onlylate = 0;
247 while ((ch = getopt(argc, argv, "adF:fLlno:prt:uvw")) != -1)
259 init_flags |= MNT_FORCE;
269 /* For compatibility with the Linux version of mount. */
273 options = catopt(options, optarg);
274 if (specified_ro(optarg))
283 options = catopt(options, "ro");
288 errx(1, "only one -t option may be specified");
289 vfslist = makevfslist(optarg);
293 init_flags |= MNT_UPDATE;
299 options = catopt(options, "noro");
309 #define BADTYPE(type) \
310 (strcmp(type, FSTAB_RO) && \
311 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
313 if ((init_flags & MNT_UPDATE) && (ro == 0))
314 options = catopt(options, "noro");
319 if ((mntsize = getmntinfo(&mntbuf,
320 verbose ? MNT_WAIT : MNT_NOWAIT)) == 0)
321 err(1, "getmntinfo");
323 while ((fs = getfsent()) != NULL) {
324 if (BADTYPE(fs->fs_type))
326 if (checkvfsname(fs->fs_vfstype, vfslist))
328 if (hasopt(fs->fs_mntops, "noauto"))
330 if (!hasopt(fs->fs_mntops, "late") && onlylate)
332 if (hasopt(fs->fs_mntops, "late") && !late)
334 if (hasopt(fs->fs_mntops, "failok"))
338 if (!(init_flags & MNT_UPDATE) &&
339 !hasopt(fs->fs_mntops, "update") &&
340 ismounted(fs, mntbuf, mntsize))
342 options = update_options(options, fs->fs_mntops,
344 if (mountfs(fs->fs_vfstype, fs->fs_spec,
345 fs->fs_file, init_flags, options,
346 fs->fs_mntops) && !failok)
349 } else if (fstab_style) {
350 for (i = 0; i < mntsize; i++) {
351 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
353 putfsent(&mntbuf[i]);
356 for (i = 0; i < mntsize; i++) {
357 if (checkvfsname(mntbuf[i].f_fstypename,
361 (mntbuf[i].f_flags & MNT_IGNORE) != 0)
371 rmslashes(*argv, *argv);
372 if (init_flags & MNT_UPDATE) {
375 if ((mntbuf = getmntpt(*argv)) == NULL)
376 errx(1, "not currently mounted %s", *argv);
378 * Only get the mntflags from fstab if both mntpoint
379 * and mntspec are identical. Also handle the special
380 * case where just '/' is mounted and 'spec' is not
381 * identical with the one from fstab ('/dev' is missing
382 * in the spec-string at boot-time).
384 if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
385 if (strcmp(fs->fs_spec,
386 mntbuf->f_mntfromname) == 0 &&
388 mntbuf->f_mntonname) == 0) {
390 mntfromname = mntbuf->f_mntfromname;
391 } else if (argv[0][0] == '/' &&
392 argv[0][1] == '\0' &&
393 strcmp(fs->fs_vfstype,
394 mntbuf->f_fstypename) == 0) {
397 mntfromname = fs->fs_spec;
401 options = update_options(options, fs->fs_mntops,
404 mntfromname = mntbuf->f_mntfromname;
405 options = update_options(options, NULL,
408 rval = mountfs(mntbuf->f_fstypename, mntfromname,
409 mntbuf->f_mntonname, init_flags, options, 0);
412 if ((fs = getfsfile(*argv)) == NULL &&
413 (fs = getfsspec(*argv)) == NULL)
414 errx(1, "%s: unknown special file or file system",
416 if (BADTYPE(fs->fs_type))
417 errx(1, "%s has unknown file system type",
419 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
420 init_flags, options, fs->fs_mntops);
424 * If -t flag has not been specified, the path cannot be
425 * found, spec contains either a ':' or a '@', then assume
426 * that an NFS file system is being specified ala Sun.
427 * Check if the hostname contains only allowed characters
428 * to reduce false positives. IPv6 addresses containing
429 * ':' will be correctly parsed only if the separator is '@'.
430 * The definition of a valid hostname is taken from RFC 1034.
432 if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL ||
433 (ep = strchr(argv[0], ':')) != NULL)) {
436 ep = cp + strlen(cp);
440 if (!isdigit(*cp) && !isalpha(*cp) &&
441 *cp != '.' && *cp != '-' && *cp != ':')
448 rval = mountfs(vfstype,
449 argv[0], argv[1], init_flags, options, NULL);
457 * If the mount was successfully, and done by root, tell mountd the
460 if (rval == 0 && getuid() == 0)
467 ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
469 char realfsfile[PATH_MAX];
472 if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
473 /* the root file system can always be remounted */
476 /* The user may have specified a symlink in fstab, resolve the path */
477 if (realpath(fs->fs_file, realfsfile) == NULL) {
478 /* Cannot resolve the path, use original one */
479 strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile));
483 * Consider the filesystem to be mounted if:
484 * It has the same mountpoint as a mounted filesytem, and
485 * It has the same type as that same mounted filesystem, and
486 * It has the same device name as that same mounted filesystem, OR
487 * It is a nonremountable filesystem
489 for (i = mntsize - 1; i >= 0; --i)
490 if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 &&
491 strcmp(fs->fs_vfstype, mntbuf[i].f_fstypename) == 0 &&
492 (!isremountable(fs->fs_vfstype) ||
493 (strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0)))
499 isremountable(const char *vfsname)
503 for (cp = remountable_fs_names; *cp; cp++)
504 if (strcmp(*cp, vfsname) == 0)
510 hasopt(const char *mntopts, const char *option)
515 if (option[0] == 'n' && option[1] == 'o') {
520 optbuf = strdup(mntopts);
522 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
523 if (opt[0] == 'n' && opt[1] == 'o') {
524 if (!strcasecmp(opt + 2, option))
526 } else if (!strcasecmp(opt, option))
534 append_arg(struct cpa *sa, char *arg)
536 if (sa->c + 1 == sa->sz) {
537 sa->sz = sa->sz == 0 ? 8 : sa->sz * 2;
538 sa->a = realloc(sa->a, sizeof(*sa->a) * sa->sz);
540 errx(1, "realloc failed");
542 sa->a[++sa->c] = arg;
546 mountfs(const char *vfstype, const char *spec, const char *name, int flags,
547 const char *options, const char *mntopts)
551 char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX];
552 static struct cpa mnt_argv;
554 /* resolve the mountpoint with realpath(3) */
555 if (checkpath(name, mntpath) != 0) {
563 optbuf = catopt(strdup(mntopts), options);
565 if (strcmp(name, "/") == 0)
567 if (flags & MNT_FORCE)
568 optbuf = catopt(optbuf, "force");
569 if (flags & MNT_RDONLY)
570 optbuf = catopt(optbuf, "ro");
573 * The mount_mfs (newfs) command uses -o to select the
574 * optimization mode. We don't pass the default "-o rw"
577 if (flags & MNT_UPDATE)
578 optbuf = catopt(optbuf, "update");
580 /* Compatibility glue. */
581 if (strcmp(vfstype, "msdos") == 0)
584 /* Construct the name of the appropriate mount command */
585 (void)snprintf(execname, sizeof(execname), "mount_%s", vfstype);
588 append_arg(&mnt_argv, execname);
589 mangle(optbuf, &mnt_argv);
590 if (mountprog != NULL)
591 strlcpy(execname, mountprog, sizeof(execname));
593 append_arg(&mnt_argv, strdup(spec));
594 append_arg(&mnt_argv, strdup(name));
595 append_arg(&mnt_argv, NULL);
598 if (use_mountprog(vfstype))
599 printf("exec: %s", execname);
601 printf("mount -t %s", vfstype);
602 for (i = 1; i < mnt_argv.c; i++)
603 (void)printf(" %s", mnt_argv.a[i]);
611 if (use_mountprog(vfstype)) {
612 ret = exec_mountprog(name, execname, mnt_argv.a);
614 ret = mount_fs(vfstype, mnt_argv.c, mnt_argv.a);
622 if (statfs(name, &sf) < 0) {
623 warn("statfs %s", name);
636 prmount(struct statfs *sfp)
640 struct mntoptnames *o;
643 (void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
646 flags = sfp->f_flags & MNT_VISFLAGMASK;
647 for (o = optnames; flags != 0 && o->o_opt != 0; o++)
648 if (flags & o->o_opt) {
649 (void)printf(", %s", o->o_name);
653 * Inform when file system is mounted by an unprivileged user
654 * or privileged non-root user.
656 if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) {
657 (void)printf(", mounted by ");
658 if ((pw = getpwuid(sfp->f_owner)) != NULL)
659 (void)printf("%s", pw->pw_name);
661 (void)printf("%d", sfp->f_owner);
664 if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
665 (void)printf(", writes: sync %ju async %ju",
666 (uintmax_t)sfp->f_syncwrites,
667 (uintmax_t)sfp->f_asyncwrites);
668 if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
669 (void)printf(", reads: sync %ju async %ju",
670 (uintmax_t)sfp->f_syncreads,
671 (uintmax_t)sfp->f_asyncreads);
672 if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) {
673 (void)printf(", fsid ");
674 for (i = 0; i < sizeof(sfp->f_fsid); i++)
675 (void)printf("%02x", ((u_char *)&sfp->f_fsid)[i]);
682 getmntpt(const char *name)
684 struct statfs *mntbuf;
687 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
688 for (i = mntsize - 1; i >= 0; i--) {
689 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
690 strcmp(mntbuf[i].f_mntonname, name) == 0)
697 catopt(char *s0, const char *s1)
701 if (s1 == NULL || *s1 == '\0')
705 if (asprintf(&cp, "%s,%s", s0, s1) == -1)
706 errx(1, "asprintf failed");
716 mangle(char *options, struct cpa *a)
720 for (s = options; (p = strsep(&s, ",")) != NULL;)
722 if (strcmp(p, "noauto") == 0) {
724 * Do not pass noauto option to nmount().
725 * or external mount program. noauto is
726 * only used to prevent mounting a filesystem
727 * when 'mount -a' is specified, and is
728 * not a real mount option.
731 } else if (strcmp(p, "late") == 0) {
733 * "late" is used to prevent certain file
734 * systems from being mounted before late
735 * in the boot cycle; for instance,
736 * loopback NFS mounts can't be mounted
737 * before mountd starts.
740 } else if (strcmp(p, "failok") == 0) {
742 * "failok" is used to prevent certain file
743 * systems from being causing the system to
744 * drop into single user mode in the boot
745 * cycle, and is not a real mount option.
748 } else if (strncmp(p, "mountprog", 9) == 0) {
750 * "mountprog" is used to force the use of
751 * userland mount programs.
753 val = strchr(p, '=');
757 mountprog = strdup(val);
760 if (mountprog == NULL) {
761 errx(1, "Need value for -o mountprog");
764 } else if (strcmp(p, "userquota") == 0) {
766 } else if (strncmp(p, userquotaeq,
767 sizeof(userquotaeq) - 1) == 0) {
769 } else if (strcmp(p, "groupquota") == 0) {
771 } else if (strncmp(p, groupquotaeq,
772 sizeof(groupquotaeq) - 1) == 0) {
774 } else if (*p == '-') {
779 append_arg(a, p + 1);
782 append_arg(a, strdup("-o"));
790 update_options(char *opts, char *fstab, int curflags)
794 char *expopt, *newopt, *tmpopt;
799 /* remove meta options from list */
800 remopt(fstab, MOUNT_META_OPTION_FSTAB);
801 remopt(fstab, MOUNT_META_OPTION_CURRENT);
802 cur = flags2opts(curflags);
805 * Expand all meta-options passed to us first.
808 for (p = opts; (o = strsep(&p, ",")) != NULL;) {
809 if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
810 expopt = catopt(expopt, fstab);
811 else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
812 expopt = catopt(expopt, cur);
814 expopt = catopt(expopt, o);
820 * Remove previous contradictory arguments. Given option "foo" we
821 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
822 * and "foo" - so we can deal with possible options like "notice".
825 for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
826 if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
827 errx(1, "malloc failed");
829 strcpy(tmpopt, "no");
831 remopt(newopt, tmpopt);
834 if (strncmp("no", o, 2) == 0)
837 newopt = catopt(newopt, o);
845 remopt(char *string, const char *opt)
849 if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
854 for (p = string; (o = strsep(&p, ",")) != NULL;) {
855 if (strcmp(opt, o) != 0) {
856 if (*r == ',' && *o != '\0')
858 while ((*r++ = *o++) != '\0')
870 (void)fprintf(stderr, "%s\n%s\n%s\n",
871 "usage: mount [-adflpruvw] [-F fstab] [-o options] [-t ufs | external_type]",
872 " mount [-dfpruvw] special | node",
873 " mount [-dfpruvw] [-o options] [-t ufs | external_type] special node");
878 putfsent(struct statfs *ent)
885 /* flags2opts() doesn't return the "rw" option. */
886 if ((ent->f_flags & MNT_RDONLY) != 0)
889 rw = catopt(NULL, "rw");
891 opts = flags2opts(ent->f_flags);
892 opts = catopt(rw, opts);
894 if (strncmp(ent->f_mntfromname, "<below>", 7) == 0 ||
895 strncmp(ent->f_mntfromname, "<above>", 7) == 0) {
896 strlcpy(ent->f_mntfromname,
897 (strnstr(ent->f_mntfromname, ":", 8) +1),
898 sizeof(ent->f_mntfromname));
901 l = strlen(ent->f_mntfromname);
902 printf("%s%s%s%s", ent->f_mntfromname,
905 l < 24 ? "\t" : " ");
906 l = strlen(ent->f_mntonname);
907 printf("%s%s%s%s", ent->f_mntonname,
910 l < 24 ? "\t" : " ");
911 printf("%s\t", ent->f_fstypename);
917 if ((fst = getfsspec(ent->f_mntfromname)))
918 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
919 else if ((fst = getfsfile(ent->f_mntonname)))
920 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
921 else if (strcmp(ent->f_fstypename, "ufs") == 0) {
922 if (strcmp(ent->f_mntonname, "/") == 0)
932 flags2opts(int flags)
938 if (flags & MNT_RDONLY) res = catopt(res, "ro");
939 if (flags & MNT_SYNCHRONOUS) res = catopt(res, "sync");
940 if (flags & MNT_NOEXEC) res = catopt(res, "noexec");
941 if (flags & MNT_NOSUID) res = catopt(res, "nosuid");
942 if (flags & MNT_UNION) res = catopt(res, "union");
943 if (flags & MNT_ASYNC) res = catopt(res, "async");
944 if (flags & MNT_NOATIME) res = catopt(res, "noatime");
945 if (flags & MNT_NOCLUSTERR) res = catopt(res, "noclusterr");
946 if (flags & MNT_NOCLUSTERW) res = catopt(res, "noclusterw");
947 if (flags & MNT_NOSYMFOLLOW) res = catopt(res, "nosymfollow");
948 if (flags & MNT_SUIDDIR) res = catopt(res, "suiddir");
949 if (flags & MNT_MULTILABEL) res = catopt(res, "multilabel");
950 if (flags & MNT_ACLS) res = catopt(res, "acls");
951 if (flags & MNT_NFS4ACLS) res = catopt(res, "nfsv4acls");
952 if (flags & MNT_UNTRUSTED) res = catopt(res, "untrusted");
953 if (flags & MNT_NOCOVER) res = catopt(res, "nocover");
954 if (flags & MNT_EMPTYDIR) res = catopt(res, "emptydir");