2 * Copyright (c) 1980, 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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 static const char copyright[] =
32 "@(#) Copyright (c) 1980, 1989, 1993, 1994\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)mount.c 8.25 (Berkeley) 5/8/95";
40 static const char rcsid[] =
44 #include <sys/param.h>
45 #include <sys/mount.h>
65 #include "pathnames.h"
68 #define MOUNT_META_OPTION_FSTAB "fstab"
69 #define MOUNT_META_OPTION_CURRENT "current"
71 int debug, fstab_style, verbose;
73 char *catopt(char *, const char *);
74 struct statfs *getmntpt(const char *);
75 int hasopt(const char *, const char *);
76 int ismounted(struct fstab *, struct statfs *, int);
77 int isremountable(const char *);
78 void mangle(char *, int *, char *[]);
79 char *update_options(char *, char *, int);
80 int mountfs(const char *, const char *, const char *,
81 int, const char *, const char *);
82 void remopt(char *, const char *);
83 void prmount(struct statfs *);
84 void putfsent(const struct statfs *);
86 char *flags2opts(int);
88 /* Map from mount options to printable formats. */
93 { MNT_ASYNC, "asynchronous" },
94 { MNT_EXPORTED, "NFS exported" },
95 { MNT_LOCAL, "local" },
96 { MNT_NOATIME, "noatime" },
97 { MNT_NOEXEC, "noexec" },
98 { MNT_NOSUID, "nosuid" },
99 { MNT_NOSYMFOLLOW, "nosymfollow" },
100 { MNT_QUOTA, "with quotas" },
101 { MNT_RDONLY, "read-only" },
102 { MNT_SYNCHRONOUS, "synchronous" },
103 { MNT_UNION, "union" },
104 { MNT_NOCLUSTERR, "noclusterr" },
105 { MNT_NOCLUSTERW, "noclusterw" },
106 { MNT_SUIDDIR, "suiddir" },
107 { MNT_SOFTDEP, "soft-updates" },
108 { MNT_MULTILABEL, "multilabel" },
109 { MNT_ACLS, "acls" },
110 { MNT_GJOURNAL, "gjournal" },
115 * List of VFS types that can be remounted without becoming mounted on top
117 * XXX Is this list correct?
120 remountable_fs_names[] = {
121 "ufs", "ffs", "ext2fs",
125 static const char userquotaeq[] = "userquota=";
126 static const char groupquotaeq[] = "groupquota=";
129 use_mountprog(const char *vfstype)
131 /* XXX: We need to get away from implementing external mount
132 * programs for every filesystem, and move towards having
133 * each filesystem properly implement the nmount() system call.
137 "cd9660", "mfs", "msdosfs", "nfs", "nfs4", "ntfs",
138 "nwfs", "nullfs", "portalfs", "smbfs", "udf", "unionfs",
142 for (i = 0; fs[i] != NULL; ++i) {
143 if (strcmp(vfstype, fs[i]) == 0)
151 exec_mountprog(const char *name, const char *execname, char *const argv[])
156 switch (pid = fork()) {
157 case -1: /* Error. */
161 /* Go find an executable. */
162 execvP(execname, _PATH_SYSPATH, argv);
163 if (errno == ENOENT) {
164 warn("exec %s not found in %s", execname,
168 default: /* Parent. */
169 if (waitpid(pid, &status, 0) < 0) {
174 if (WIFEXITED(status)) {
175 if (WEXITSTATUS(status) != 0)
176 return (WEXITSTATUS(status));
177 } else if (WIFSIGNALED(status)) {
178 warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
188 specified_ro(const char *arg)
193 optbuf = strdup(arg);
197 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
198 if (strcmp(opt, "ro") == 0) {
213 pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid);
215 /* Mountd is not running. */
219 if (errno != EEXIST) {
220 /* Cannot open pidfile for some reason. */
223 /* We have mountd(8) PID in mountdpid varible, let's signal it. */
224 if (kill(mountdpid, SIGHUP) == -1)
225 err(1, "signal mountd");
229 main(int argc, char *argv[])
231 const char *mntfromname, **vfslist, *vfstype;
233 struct statfs *mntbuf;
234 int all, ch, i, init_flags, late, mntsize, rval, have_fstab, ro;
235 char *cp, *ep, *options;
237 all = init_flags = late = 0;
242 while ((ch = getopt(argc, argv, "adF:flo:prt:uvw")) != -1)
254 init_flags |= MNT_FORCE;
261 options = catopt(options, optarg);
262 if (specified_ro(optarg))
271 options = catopt(options, "ro");
276 errx(1, "only one -t option may be specified");
277 vfslist = makevfslist(optarg);
281 init_flags |= MNT_UPDATE;
287 options = catopt(options, "noro");
297 #define BADTYPE(type) \
298 (strcmp(type, FSTAB_RO) && \
299 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
301 if ((init_flags & MNT_UPDATE) && (ro == 0))
302 options = catopt(options, "noro");
307 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
308 err(1, "getmntinfo");
310 while ((fs = getfsent()) != NULL) {
311 if (BADTYPE(fs->fs_type))
313 if (checkvfsname(fs->fs_vfstype, vfslist))
315 if (hasopt(fs->fs_mntops, "noauto"))
317 if (hasopt(fs->fs_mntops, "late") && !late)
319 if (!(init_flags & MNT_UPDATE) &&
320 ismounted(fs, mntbuf, mntsize))
322 options = update_options(options, fs->fs_mntops,
324 if (mountfs(fs->fs_vfstype, fs->fs_spec,
325 fs->fs_file, init_flags, options,
329 } else if (fstab_style) {
330 for (i = 0; i < mntsize; i++) {
331 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
333 putfsent(&mntbuf[i]);
336 for (i = 0; i < mntsize; i++) {
337 if (checkvfsname(mntbuf[i].f_fstypename,
348 rmslashes(*argv, *argv);
349 if (init_flags & MNT_UPDATE) {
352 if ((mntbuf = getmntpt(*argv)) == NULL)
353 errx(1, "not currently mounted %s", *argv);
355 * Only get the mntflags from fstab if both mntpoint
356 * and mntspec are identical. Also handle the special
357 * case where just '/' is mounted and 'spec' is not
358 * identical with the one from fstab ('/dev' is missing
359 * in the spec-string at boot-time).
361 if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
362 if (strcmp(fs->fs_spec,
363 mntbuf->f_mntfromname) == 0 &&
365 mntbuf->f_mntonname) == 0) {
367 mntfromname = mntbuf->f_mntfromname;
368 } else if (argv[0][0] == '/' &&
369 argv[0][1] == '\0') {
372 mntfromname = fs->fs_spec;
376 options = update_options(options, fs->fs_mntops,
379 mntfromname = mntbuf->f_mntfromname;
380 options = update_options(options, NULL,
383 rval = mountfs(mntbuf->f_fstypename, mntfromname,
384 mntbuf->f_mntonname, init_flags, options, 0);
387 if ((fs = getfsfile(*argv)) == NULL &&
388 (fs = getfsspec(*argv)) == NULL)
389 errx(1, "%s: unknown special file or file system",
391 if (BADTYPE(fs->fs_type))
392 errx(1, "%s has unknown file system type",
394 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
395 init_flags, options, fs->fs_mntops);
399 * If -t flag has not been specified, the path cannot be
400 * found, spec contains either a ':' or a '@', then assume
401 * that an NFS file system is being specified ala Sun.
402 * Check if the hostname contains only allowed characters
403 * to reduce false positives. IPv6 addresses containing
404 * ':' will be correctly parsed only if the separator is '@'.
405 * The definition of a valid hostname is taken from RFC 1034.
407 if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL ||
408 (ep = strchr(argv[0], ':')) != NULL)) {
411 ep = cp + strlen(cp);
415 if (!isdigit(*cp) && !isalpha(*cp) &&
416 *cp != '.' && *cp != '-' && *cp != ':')
423 rval = mountfs(vfstype,
424 argv[0], argv[1], init_flags, options, NULL);
432 * If the mount was successfully, and done by root, tell mountd the
435 if (rval == 0 && getuid() == 0)
442 ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
444 char realfsfile[PATH_MAX];
447 if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
448 /* the root file system can always be remounted */
451 /* The user may have specified a symlink in fstab, resolve the path */
452 if (realpath(fs->fs_file, realfsfile) == NULL) {
453 /* Cannot resolve the path, use original one */
454 strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile));
457 for (i = mntsize - 1; i >= 0; --i)
458 if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 &&
459 (!isremountable(fs->fs_vfstype) ||
460 strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
466 isremountable(const char *vfsname)
470 for (cp = remountable_fs_names; *cp; cp++)
471 if (strcmp(*cp, vfsname) == 0)
477 hasopt(const char *mntopts, const char *option)
482 if (option[0] == 'n' && option[1] == 'o') {
487 optbuf = strdup(mntopts);
489 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
490 if (opt[0] == 'n' && opt[1] == 'o') {
491 if (!strcasecmp(opt + 2, option))
493 } else if (!strcasecmp(opt, option))
501 mountfs(const char *vfstype, const char *spec, const char *name, int flags,
502 const char *options, const char *mntopts)
507 char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX];
509 /* resolve the mountpoint with realpath(3) */
510 (void)checkpath(name, mntpath);
515 optbuf = catopt(strdup(mntopts), options);
517 if (strcmp(name, "/") == 0)
519 if (flags & MNT_FORCE)
520 optbuf = catopt(optbuf, "force");
521 if (flags & MNT_RDONLY)
522 optbuf = catopt(optbuf, "ro");
525 * The mount_mfs (newfs) command uses -o to select the
526 * optimization mode. We don't pass the default "-o rw"
529 if (flags & MNT_UPDATE)
530 optbuf = catopt(optbuf, "update");
532 /* Compatibility glue. */
533 if (strcmp(vfstype, "msdos") == 0) {
535 "Using \"-t msdosfs\", since \"-t msdos\" is deprecated.");
539 /* Construct the name of the appropriate mount command */
540 (void)snprintf(execname, sizeof(execname), "mount_%s", vfstype);
543 argv[argc++] = execname;
544 mangle(optbuf, &argc, argv);
545 argv[argc++] = strdup(spec);
546 argv[argc++] = strdup(name);
550 if (use_mountprog(vfstype))
551 printf("exec: mount_%s", vfstype);
553 printf("mount -t %s", vfstype);
554 for (i = 1; i < argc; i++)
555 (void)printf(" %s", argv[i]);
560 if (use_mountprog(vfstype)) {
561 ret = exec_mountprog(name, execname, argv);
563 ret = mount_fs(vfstype, argc, argv);
569 if (statfs(name, &sf) < 0) {
570 warn("statfs %s", name);
583 prmount(struct statfs *sfp)
590 (void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
593 flags = sfp->f_flags & MNT_VISFLAGMASK;
594 for (o = optnames; flags && o->o_opt; o++)
595 if (flags & o->o_opt) {
596 (void)printf(", %s", o->o_name);
600 * Inform when file system is mounted by an unprivileged user
601 * or privileged non-root user.
603 if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) {
604 (void)printf(", mounted by ");
605 if ((pw = getpwuid(sfp->f_owner)) != NULL)
606 (void)printf("%s", pw->pw_name);
608 (void)printf("%d", sfp->f_owner);
611 if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
612 (void)printf(", writes: sync %ju async %ju",
613 (uintmax_t)sfp->f_syncwrites,
614 (uintmax_t)sfp->f_asyncwrites);
615 if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
616 (void)printf(", reads: sync %ju async %ju",
617 (uintmax_t)sfp->f_syncreads,
618 (uintmax_t)sfp->f_asyncreads);
619 if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) {
621 for (i = 0; i < sizeof(sfp->f_fsid); i++)
622 printf("%02x", ((u_char *)&sfp->f_fsid)[i]);
629 getmntpt(const char *name)
631 struct statfs *mntbuf;
634 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
635 for (i = mntsize - 1; i >= 0; i--) {
636 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
637 strcmp(mntbuf[i].f_mntonname, name) == 0)
644 catopt(char *s0, const char *s1)
649 if (s1 == NULL || *s1 == '\0')
653 i = strlen(s0) + strlen(s1) + 1 + 1;
654 if ((cp = malloc(i)) == NULL)
655 errx(1, "malloc failed");
656 (void)snprintf(cp, i, "%s,%s", s0, s1);
666 mangle(char *options, int *argcp, char *argv[])
672 for (s = options; (p = strsep(&s, ",")) != NULL;)
674 if (strcmp(p, "noauto") == 0) {
676 * Do not pass noauto option to nmount().
677 * or external mount program. noauto is
678 * only used to prevent mounting a filesystem
679 * when 'mount -a' is specified, and is
680 * not a real mount option.
683 } else if (strcmp(p, "late") == 0) {
685 * "late" is used to prevent certain file
686 * systems from being mounted before late
687 * in the boot cycle; for instance,
688 * loopback NFS mounts can't be mounted
689 * before mountd starts.
692 } else if (strcmp(p, "userquota") == 0) {
694 } else if (strncmp(p, userquotaeq,
695 sizeof(userquotaeq) - 1) == 0) {
697 } else if (strcmp(p, "groupquota") == 0) {
699 } else if (strncmp(p, groupquotaeq,
700 sizeof(groupquotaeq) - 1) == 0) {
702 } else if (*p == '-') {
710 argv[argc++] = strdup("-o");
720 update_options(char *opts, char *fstab, int curflags)
724 char *expopt, *newopt, *tmpopt;
729 /* remove meta options from list */
730 remopt(fstab, MOUNT_META_OPTION_FSTAB);
731 remopt(fstab, MOUNT_META_OPTION_CURRENT);
732 cur = flags2opts(curflags);
735 * Expand all meta-options passed to us first.
738 for (p = opts; (o = strsep(&p, ",")) != NULL;) {
739 if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
740 expopt = catopt(expopt, fstab);
741 else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
742 expopt = catopt(expopt, cur);
744 expopt = catopt(expopt, o);
750 * Remove previous contradictory arguments. Given option "foo" we
751 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
752 * and "foo" - so we can deal with possible options like "notice".
755 for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
756 if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
757 errx(1, "malloc failed");
759 strcpy(tmpopt, "no");
761 remopt(newopt, tmpopt);
764 if (strncmp("no", o, 2) == 0)
767 newopt = catopt(newopt, o);
775 remopt(char *string, const char *opt)
779 if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
784 for (p = string; (o = strsep(&p, ",")) != NULL;) {
785 if (strcmp(opt, o) != 0) {
786 if (*r == ',' && *o != '\0')
788 while ((*r++ = *o++) != '\0')
800 (void)fprintf(stderr, "%s\n%s\n%s\n",
801 "usage: mount [-adflpruvw] [-F fstab] [-o options] [-t ufs | external_type]",
802 " mount [-dfpruvw] special | node",
803 " mount [-dfpruvw] [-o options] [-t ufs | external_type] special node");
808 putfsent(const struct statfs *ent)
814 opts = flags2opts(ent->f_flags);
817 * "rw" is not a real mount option; this is why we print NULL as "rw"
818 * if opts is still NULL here.
820 l = strlen(ent->f_mntfromname);
821 printf("%s%s%s%s", ent->f_mntfromname,
824 l < 24 ? "\t" : " ");
825 l = strlen(ent->f_mntonname);
826 printf("%s%s%s%s", ent->f_mntonname,
829 l < 24 ? "\t" : " ");
830 printf("%s\t", ent->f_fstypename);
832 printf("%s\t", "rw");
840 if ((fst = getfsspec(ent->f_mntfromname)))
841 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
842 else if ((fst = getfsfile(ent->f_mntonname)))
843 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
844 else if (strcmp(ent->f_fstypename, "ufs") == 0) {
845 if (strcmp(ent->f_mntonname, "/") == 0)
855 flags2opts(int flags)
861 if (flags & MNT_RDONLY) res = catopt(res, "ro");
862 if (flags & MNT_SYNCHRONOUS) res = catopt(res, "sync");
863 if (flags & MNT_NOEXEC) res = catopt(res, "noexec");
864 if (flags & MNT_NOSUID) res = catopt(res, "nosuid");
865 if (flags & MNT_UNION) res = catopt(res, "union");
866 if (flags & MNT_ASYNC) res = catopt(res, "async");
867 if (flags & MNT_NOATIME) res = catopt(res, "noatime");
868 if (flags & MNT_NOCLUSTERR) res = catopt(res, "noclusterr");
869 if (flags & MNT_NOCLUSTERW) res = catopt(res, "noclusterw");
870 if (flags & MNT_NOSYMFOLLOW) res = catopt(res, "nosymfollow");
871 if (flags & MNT_SUIDDIR) res = catopt(res, "suiddir");
872 if (flags & MNT_MULTILABEL) res = catopt(res, "multilabel");
873 if (flags & MNT_ACLS) res = catopt(res, "acls");
874 if (flags & MNT_GJOURNAL) res = catopt(res, "gjournal");