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>
64 #include "pathnames.h"
67 #define MOUNT_META_OPTION_FSTAB "fstab"
68 #define MOUNT_META_OPTION_CURRENT "current"
70 int debug, fstab_style, verbose;
72 char *catopt(char *, const char *);
73 struct statfs *getmntpt(const char *);
74 int hasopt(const char *, const char *);
75 int ismounted(struct fstab *, struct statfs *, int);
76 int isremountable(const char *);
77 void mangle(char *, int *, char **);
78 char *update_options(char *, char *, int);
79 int mountfs(const char *, const char *, const char *,
80 int, const char *, const char *);
81 void remopt(char *, const char *);
82 void prmount(struct statfs *);
83 void putfsent(const struct statfs *);
85 char *flags2opts(int);
87 /* Map from mount options to printable formats. */
92 { MNT_ASYNC, "asynchronous" },
93 { MNT_EXPORTED, "NFS exported" },
94 { MNT_LOCAL, "local" },
95 { MNT_NOATIME, "noatime" },
96 { MNT_NOEXEC, "noexec" },
97 { MNT_NOSUID, "nosuid" },
98 { MNT_NOSYMFOLLOW, "nosymfollow" },
99 { MNT_QUOTA, "with quotas" },
100 { MNT_RDONLY, "read-only" },
101 { MNT_SYNCHRONOUS, "synchronous" },
102 { MNT_UNION, "union" },
103 { MNT_NOCLUSTERR, "noclusterr" },
104 { MNT_NOCLUSTERW, "noclusterw" },
105 { MNT_SUIDDIR, "suiddir" },
106 { MNT_SOFTDEP, "soft-updates" },
107 { MNT_MULTILABEL, "multilabel" },
108 { MNT_ACLS, "acls" },
113 * List of VFS types that can be remounted without becoming mounted on top
115 * XXX Is this list correct?
118 remountable_fs_names[] = {
119 "ufs", "ffs", "ext2fs",
124 use_mountprog(const char *vfstype)
126 /* XXX: We need to get away from implementing external mount
127 * programs for every filesystem, and move towards having
128 * each filesystem properly implement the nmount() system call.
132 "cd9660", "mfs", "msdosfs", "nfs", "nfs4", "ntfs",
133 "nwfs", "nullfs", "portalfs", "smbfs", "udf", "umapfs",
138 for (i=0; fs[i] != NULL; ++i) {
139 if (strcmp(vfstype, fs[i]) == 0)
147 exec_mountprog(const char *name, const char *execname,
153 switch (pid = fork()) {
154 case -1: /* Error. */
158 /* Go find an executable. */
159 execvP(execname, _PATH_SYSPATH, argv);
160 if (errno == ENOENT) {
161 warn("exec %s not found in %s", execname,
165 default: /* Parent. */
166 if (waitpid(pid, &status, 0) < 0) {
171 if (WIFEXITED(status)) {
172 if (WEXITSTATUS(status) != 0)
173 return (WEXITSTATUS(status));
174 } else if (WIFSIGNALED(status)) {
175 warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
185 main(int argc, char *argv[])
187 const char *mntfromname, **vfslist, *vfstype;
189 struct statfs *mntbuf;
192 int all, ch, i, init_flags, mntsize, rval, have_fstab;
193 char *cp, *ep, *options;
195 all = init_flags = 0;
199 while ((ch = getopt(argc, argv, "adF:fo:prwt:uv")) != -1)
211 init_flags |= MNT_FORCE;
215 options = catopt(options, optarg);
222 options = catopt(options, "ro");
226 errx(1, "only one -t option may be specified");
227 vfslist = makevfslist(optarg);
231 init_flags |= MNT_UPDATE;
237 options = catopt(options, "noro");
247 #define BADTYPE(type) \
248 (strcmp(type, FSTAB_RO) && \
249 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
254 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
255 err(1, "getmntinfo");
257 while ((fs = getfsent()) != NULL) {
258 if (BADTYPE(fs->fs_type))
260 if (checkvfsname(fs->fs_vfstype, vfslist))
262 if (hasopt(fs->fs_mntops, "noauto"))
264 if (!(init_flags & MNT_UPDATE) &&
265 ismounted(fs, mntbuf, mntsize))
267 options = update_options(options, fs->fs_mntops,
269 if (mountfs(fs->fs_vfstype, fs->fs_spec,
270 fs->fs_file, init_flags, options,
274 } else if (fstab_style) {
275 for (i = 0; i < mntsize; i++) {
276 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
278 putfsent(&mntbuf[i]);
281 for (i = 0; i < mntsize; i++) {
282 if (checkvfsname(mntbuf[i].f_fstypename,
293 rmslashes(*argv, *argv);
294 if (init_flags & MNT_UPDATE) {
297 if ((mntbuf = getmntpt(*argv)) == NULL)
298 errx(1, "not currently mounted %s", *argv);
300 * Only get the mntflags from fstab if both mntpoint
301 * and mntspec are identical. Also handle the special
302 * case where just '/' is mounted and 'spec' is not
303 * identical with the one from fstab ('/dev' is missing
304 * in the spec-string at boot-time).
306 if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
307 if (strcmp(fs->fs_spec,
308 mntbuf->f_mntfromname) == 0 &&
310 mntbuf->f_mntonname) == 0) {
312 mntfromname = mntbuf->f_mntfromname;
313 } else if (argv[0][0] == '/' &&
314 argv[0][1] == '\0') {
317 mntfromname = fs->fs_spec;
321 options = update_options(options, fs->fs_mntops,
324 mntfromname = mntbuf->f_mntfromname;
325 options = update_options(options, NULL,
328 rval = mountfs(mntbuf->f_fstypename, mntfromname,
329 mntbuf->f_mntonname, init_flags, options, 0);
332 if ((fs = getfsfile(*argv)) == NULL &&
333 (fs = getfsspec(*argv)) == NULL)
334 errx(1, "%s: unknown special file or file system",
336 if (BADTYPE(fs->fs_type))
337 errx(1, "%s has unknown file system type",
339 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
340 init_flags, options, fs->fs_mntops);
344 * If -t flag has not been specified, the path cannot be
345 * found, spec contains either a ':' or a '@', then assume
346 * that an NFS file system is being specified ala Sun.
347 * Check if the hostname contains only allowed characters
348 * to reduce false positives. IPv6 addresses containing
349 * ':' will be correctly parsed only if the separator is '@'.
350 * The definition of a valid hostname is taken from RFC 1034.
352 if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL ||
353 (ep = strchr(argv[0], ':')) != NULL)) {
356 ep = cp + strlen(cp);
360 if (!isdigit(*cp) && !isalpha(*cp) &&
361 *cp != '.' && *cp != '-' && *cp != ':')
368 rval = mountfs(vfstype,
369 argv[0], argv[1], init_flags, options, NULL);
377 * If the mount was successfully, and done by root, tell mountd the
378 * good news. Pid checks are probably unnecessary, but don't hurt.
380 if (rval == 0 && getuid() == 0 &&
381 (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
382 if (fscanf(mountdfp, "%d", &pid) == 1 &&
383 pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
384 err(1, "signal mountd");
385 (void)fclose(mountdfp);
392 ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
394 char realfsfile[PATH_MAX];
397 if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
398 /* the root file system can always be remounted */
401 /* The user may have specified a symlink in fstab, resolve the path */
402 if (realpath(fs->fs_file, realfsfile) == NULL) {
403 /* Cannot resolve the path, use original one */
404 strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile));
407 for (i = mntsize - 1; i >= 0; --i)
408 if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 &&
409 (!isremountable(fs->fs_vfstype) ||
410 strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
416 isremountable(const char *vfsname)
420 for (cp = remountable_fs_names; *cp; cp++)
421 if (strcmp(*cp, vfsname) == 0)
427 hasopt(const char *mntopts, const char *option)
432 if (option[0] == 'n' && option[1] == 'o') {
437 optbuf = strdup(mntopts);
439 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
440 if (opt[0] == 'n' && opt[1] == 'o') {
441 if (!strcasecmp(opt + 2, option))
443 } else if (!strcasecmp(opt, option))
451 mountfs(const char *vfstype, const char *spec, const char *name, int flags,
452 const char *options, const char *mntopts)
457 char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX];
459 /* resolve the mountpoint with realpath(3) */
460 (void)checkpath(name, mntpath);
465 if (options == NULL) {
466 if (*mntopts == '\0') {
473 optbuf = catopt(strdup(mntopts), options);
475 if (strcmp(name, "/") == 0)
477 if (flags & MNT_FORCE)
478 optbuf = catopt(optbuf, "force");
479 if (flags & MNT_RDONLY)
480 optbuf = catopt(optbuf, "ro");
483 * The mount_mfs (newfs) command uses -o to select the
484 * optimization mode. We don't pass the default "-o rw"
487 if (flags & MNT_UPDATE)
488 optbuf = catopt(optbuf, "update");
490 /* Compatibility glue. */
491 if (strcmp(vfstype, "msdos") == 0)
494 /* Construct the name of the appropriate mount command */
495 (void)snprintf(execname, sizeof(execname), "mount_%s", vfstype);
498 argv[argc++] = execname;
499 mangle(optbuf, &argc, argv);
500 argv[argc++] = strdup(spec);
501 argv[argc++] = strdup(name);
505 (void)printf("exec: mount_%s", vfstype);
506 for (i = 1; i < argc; i++)
507 (void)printf(" %s", argv[i]);
512 if (use_mountprog(vfstype)) {
513 ret = exec_mountprog(name, execname, argv);
515 ret = mount_fs(vfstype, argc, argv);
521 if (statfs(name, &sf) < 0) {
522 warn("statfs %s", name);
535 prmount(struct statfs *sfp)
542 (void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
545 flags = sfp->f_flags & MNT_VISFLAGMASK;
546 for (o = optnames; flags && o->o_opt; o++)
547 if (flags & o->o_opt) {
548 (void)printf(", %s", o->o_name);
552 * Inform when file system is mounted by an unprivileged user
553 * or privileged non-root user.
555 if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) {
556 (void)printf(", mounted by ");
557 if ((pw = getpwuid(sfp->f_owner)) != NULL)
558 (void)printf("%s", pw->pw_name);
560 (void)printf("%d", sfp->f_owner);
563 if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
564 (void)printf(", writes: sync %ju async %ju",
565 (uintmax_t)sfp->f_syncwrites,
566 (uintmax_t)sfp->f_asyncwrites);
567 if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
568 (void)printf(", reads: sync %ju async %ju",
569 (uintmax_t)sfp->f_syncreads,
570 (uintmax_t)sfp->f_asyncreads);
571 if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) {
573 for (i = 0; i < sizeof(sfp->f_fsid); i++)
574 printf("%02x", ((u_char *)&sfp->f_fsid)[i]);
581 getmntpt(const char *name)
583 struct statfs *mntbuf;
586 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
587 for (i = mntsize - 1; i >= 0; i--) {
588 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
589 strcmp(mntbuf[i].f_mntonname, name) == 0)
596 catopt(char *s0, const char *s1)
601 if (s1 == NULL || *s1 == '\0')
605 i = strlen(s0) + strlen(s1) + 1 + 1;
606 if ((cp = malloc(i)) == NULL)
607 errx(1, "malloc failed");
608 (void)snprintf(cp, i, "%s,%s", s0, s1);
618 mangle(options, argcp, argv)
627 for (s = options; (p = strsep(&s, ",")) != NULL;)
629 if (strcmp(p, "noauto") == 0) {
631 * Do not pass noauto option to nmount().
632 * or external mount program. noauto is
633 * only used to prevent mounting a filesystem
634 * when 'mount -a' is specified, and is
635 * not a real mount option.
638 } else if (strcmp(p, "userquota") == 0) {
640 } else if (strcmp(p, "groupquota") == 0) {
642 } else if (*p == '-') {
650 argv[argc++] = strdup("-o");
660 update_options(opts, fstab, curflags)
667 char *expopt, *newopt, *tmpopt;
672 /* remove meta options from list */
673 remopt(fstab, MOUNT_META_OPTION_FSTAB);
674 remopt(fstab, MOUNT_META_OPTION_CURRENT);
675 cur = flags2opts(curflags);
678 * Expand all meta-options passed to us first.
681 for (p = opts; (o = strsep(&p, ",")) != NULL;) {
682 if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
683 expopt = catopt(expopt, fstab);
684 else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
685 expopt = catopt(expopt, cur);
687 expopt = catopt(expopt, o);
693 * Remove previous contradictory arguments. Given option "foo" we
694 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
695 * and "foo" - so we can deal with possible options like "notice".
698 for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
699 if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
700 errx(1, "malloc failed");
702 strcpy(tmpopt, "no");
704 remopt(newopt, tmpopt);
707 if (strncmp("no", o, 2) == 0)
710 newopt = catopt(newopt, o);
724 if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
729 for (p = string; (o = strsep(&p, ",")) != NULL;) {
730 if (strcmp(opt, o) != 0) {
731 if (*r == ',' && *o != '\0')
733 while ((*r++ = *o++) != '\0')
745 (void)fprintf(stderr, "%s\n%s\n%s\n",
746 "usage: mount [-adfpruvw] [-F fstab] [-o options] [-t ufs | external_type]",
747 " mount [-dfpruvw] special | node",
748 " mount [-dfpruvw] [-o options] [-t ufs | external_type] special node");
754 const struct statfs *ent;
759 opts = flags2opts(ent->f_flags);
760 printf("%s\t%s\t%s %s", ent->f_mntfromname, ent->f_mntonname,
761 ent->f_fstypename, opts);
764 if ((fst = getfsspec(ent->f_mntfromname)))
765 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
766 else if ((fst = getfsfile(ent->f_mntonname)))
767 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
768 else if (strcmp(ent->f_fstypename, "ufs") == 0) {
769 if (strcmp(ent->f_mntonname, "/") == 0)
786 res = catopt(res, (flags & MNT_RDONLY) ? "ro" : "rw");
788 if (flags & MNT_SYNCHRONOUS) res = catopt(res, "sync");
789 if (flags & MNT_NOEXEC) res = catopt(res, "noexec");
790 if (flags & MNT_NOSUID) res = catopt(res, "nosuid");
791 if (flags & MNT_UNION) res = catopt(res, "union");
792 if (flags & MNT_ASYNC) res = catopt(res, "async");
793 if (flags & MNT_NOATIME) res = catopt(res, "noatime");
794 if (flags & MNT_NOCLUSTERR) res = catopt(res, "noclusterr");
795 if (flags & MNT_NOCLUSTERW) res = catopt(res, "noclusterw");
796 if (flags & MNT_NOSYMFOLLOW) res = catopt(res, "nosymfollow");
797 if (flags & MNT_SUIDDIR) res = catopt(res, "suiddir");
798 if (flags & MNT_MULTILABEL) res = catopt(res, "multilabel");
799 if (flags & MNT_ACLS) res = catopt(res, "acls");