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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1989, 1993, 1994\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)mount.c 8.25 (Berkeley) 5/8/95";
44 static const char rcsid[] =
48 #include <sys/param.h>
49 #include <sys/mount.h>
68 #include "pathnames.h"
71 #define MOUNT_META_OPTION_FSTAB "fstab"
72 #define MOUNT_META_OPTION_CURRENT "current"
74 int debug, fstab_style, verbose;
76 char *catopt(char *, const char *);
77 struct statfs *getmntpt(const char *);
78 int hasopt(const char *, const char *);
79 int ismounted(struct fstab *, struct statfs *, int);
80 int isremountable(const char *);
81 void mangle(char *, int *, const char **);
82 char *update_options(char *, char *, int);
83 int mountfs(const char *, const char *, const char *,
84 int, const char *, const char *);
85 void remopt(char *, const char *);
86 void prmount(struct statfs *);
87 void putfsent(const struct statfs *);
89 char *flags2opts(int);
91 /* Map from mount options to printable formats. */
96 { MNT_ASYNC, "asynchronous" },
97 { MNT_EXPORTED, "NFS exported" },
98 { MNT_LOCAL, "local" },
99 { MNT_NOATIME, "noatime" },
100 { MNT_NODEV, "nodev" },
101 { MNT_NOEXEC, "noexec" },
102 { MNT_NOSUID, "nosuid" },
103 { MNT_NOSYMFOLLOW, "nosymfollow" },
104 { MNT_QUOTA, "with quotas" },
105 { MNT_RDONLY, "read-only" },
106 { MNT_SYNCHRONOUS, "synchronous" },
107 { MNT_UNION, "union" },
108 { MNT_NOCLUSTERR, "noclusterr" },
109 { MNT_NOCLUSTERW, "noclusterw" },
110 { MNT_SUIDDIR, "suiddir" },
111 { MNT_SOFTDEP, "soft-updates" },
112 { MNT_MULTILABEL, "multilabel" },
113 { MNT_ACLS, "acls" },
118 * List of VFS types that can be remounted without becoming mounted on top
120 * XXX Is this list correct?
123 remountable_fs_names[] = {
124 "ufs", "ffs", "ext2fs",
133 const char *mntfromname, **vfslist, *vfstype;
135 struct statfs *mntbuf;
138 int all, ch, i, init_flags, mntsize, rval, have_fstab;
139 char *cp, *ep, *options;
141 all = init_flags = 0;
145 while ((ch = getopt(argc, argv, "adF:fo:prwt:uv")) != -1)
157 init_flags |= MNT_FORCE;
161 options = catopt(options, optarg);
168 options = catopt(options, "ro");
172 errx(1, "only one -t option may be specified");
173 vfslist = makevfslist(optarg);
177 init_flags |= MNT_UPDATE;
183 options = catopt(options, "noro");
193 #define BADTYPE(type) \
194 (strcmp(type, FSTAB_RO) && \
195 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
200 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
201 err(1, "getmntinfo");
203 while ((fs = getfsent()) != NULL) {
204 if (BADTYPE(fs->fs_type))
206 if (checkvfsname(fs->fs_vfstype, vfslist))
208 if (hasopt(fs->fs_mntops, "noauto"))
210 if (!(init_flags & MNT_UPDATE) &&
211 ismounted(fs, mntbuf, mntsize))
213 options = update_options(options, fs->fs_mntops,
215 if (mountfs(fs->fs_vfstype, fs->fs_spec,
216 fs->fs_file, init_flags, options,
220 } else if (fstab_style) {
221 for (i = 0; i < mntsize; i++) {
222 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
224 putfsent(&mntbuf[i]);
227 for (i = 0; i < mntsize; i++) {
228 if (checkvfsname(mntbuf[i].f_fstypename,
239 rmslashes(*argv, *argv);
240 if (init_flags & MNT_UPDATE) {
243 if ((mntbuf = getmntpt(*argv)) == NULL)
244 errx(1, "not currently mounted %s", *argv);
246 * Only get the mntflags from fstab if both mntpoint
247 * and mntspec are identical. Also handle the special
248 * case where just '/' is mounted and 'spec' is not
249 * identical with the one from fstab ('/dev' is missing
250 * in the spec-string at boot-time).
252 if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
253 if (strcmp(fs->fs_spec,
254 mntbuf->f_mntfromname) == 0 &&
256 mntbuf->f_mntonname) == 0) {
258 mntfromname = mntbuf->f_mntfromname;
259 } else if (argv[0][0] == '/' &&
260 argv[0][1] == '\0') {
263 mntfromname = fs->fs_spec;
267 options = update_options(options, fs->fs_mntops,
270 mntfromname = mntbuf->f_mntfromname;
271 options = update_options(options, NULL,
274 rval = mountfs(mntbuf->f_fstypename, mntfromname,
275 mntbuf->f_mntonname, init_flags, options, 0);
278 if ((fs = getfsfile(*argv)) == NULL &&
279 (fs = getfsspec(*argv)) == NULL)
280 errx(1, "%s: unknown special file or file system",
282 if (BADTYPE(fs->fs_type))
283 errx(1, "%s has unknown file system type",
285 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
286 init_flags, options, fs->fs_mntops);
290 * If -t flag has not been specified, the path cannot be
291 * found, spec contains either a ':' or a '@', then assume
292 * that an NFS file system is being specified ala Sun.
293 * Check if the hostname contains only allowed characters
294 * to reduce false positives. IPv6 addresses containing
295 * ':' will be correctly parsed only if the separator is '@'.
296 * The definition of a valid hostname is taken from RFC 1034.
298 if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL ||
299 (ep = strchr(argv[0], ':')) != NULL)) {
302 ep = cp + strlen(cp);
306 if (!isdigit(*cp) && !isalpha(*cp) &&
307 *cp != '.' && *cp != '-' && *cp != ':')
314 rval = mountfs(vfstype,
315 argv[0], argv[1], init_flags, options, NULL);
323 * If the mount was successfully, and done by root, tell mountd the
324 * good news. Pid checks are probably unnecessary, but don't hurt.
326 if (rval == 0 && getuid() == 0 &&
327 (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
328 if (fscanf(mountdfp, "%d", &pid) == 1 &&
329 pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
330 err(1, "signal mountd");
331 (void)fclose(mountdfp);
338 ismounted(fs, mntbuf, mntsize)
340 struct statfs *mntbuf;
345 if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
346 /* the root file system can always be remounted */
349 for (i = mntsize - 1; i >= 0; --i)
350 if (strcmp(fs->fs_file, mntbuf[i].f_mntonname) == 0 &&
351 (!isremountable(fs->fs_vfstype) ||
352 strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
358 isremountable(vfsname)
363 for (cp = remountable_fs_names; *cp; cp++)
364 if (strcmp(*cp, vfsname) == 0)
370 hasopt(mntopts, option)
371 const char *mntopts, *option;
376 if (option[0] == 'n' && option[1] == 'o') {
381 optbuf = strdup(mntopts);
383 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
384 if (opt[0] == 'n' && opt[1] == 'o') {
385 if (!strcasecmp(opt + 2, option))
387 } else if (!strcasecmp(opt, option))
395 mountfs(vfstype, spec, name, flags, options, mntopts)
396 const char *vfstype, *spec, *name, *options, *mntopts;
399 const char *argv[100];
403 char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX];
410 /* resolve the mountpoint with realpath(3) */
411 (void)checkpath(name, mntpath);
416 if (options == NULL) {
417 if (*mntopts == '\0') {
424 optbuf = catopt(strdup(mntopts), options);
426 if (strcmp(name, "/") == 0)
428 if (flags & MNT_FORCE)
429 optbuf = catopt(optbuf, "force");
430 if (flags & MNT_RDONLY)
431 optbuf = catopt(optbuf, "ro");
434 * The mount_mfs (newfs) command uses -o to select the
435 * optimization mode. We don't pass the default "-o rw"
438 if (flags & MNT_UPDATE)
439 optbuf = catopt(optbuf, "update");
441 /* Compatibility glue. */
442 if (strcmp(vfstype, "msdos") == 0)
446 argv[argc++] = vfstype;
447 mangle(optbuf, &argc, argv);
453 (void)printf("exec: mount_%s", vfstype);
454 for (i = 1; i < argc; i++)
455 (void)printf(" %s", argv[i]);
460 switch (pid = fork()) {
461 case -1: /* Error. */
466 if (strcmp(vfstype, "ufs") == 0)
467 exit(mount_ufs(argc, (char * const *) argv));
469 /* Go find an executable. */
470 (void)snprintf(execname, sizeof(execname), "mount_%s", vfstype);
471 execvP(execname, _PATH_SYSPATH, (char * const *)argv);
472 if (errno == ENOENT) {
473 warn("exec mount_%s not found in %s", vfstype,
478 default: /* Parent. */
481 if (waitpid(pid, &status, 0) < 0) {
486 if (WIFEXITED(status)) {
487 if (WEXITSTATUS(status) != 0)
488 return (WEXITSTATUS(status));
489 } else if (WIFSIGNALED(status)) {
490 warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
495 if (statfs(name, &sf) < 0) {
496 warn("statfs %s", name);
518 (void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
521 flags = sfp->f_flags & MNT_VISFLAGMASK;
522 for (o = optnames; flags && o->o_opt; o++)
523 if (flags & o->o_opt) {
524 (void)printf(", %s", o->o_name);
528 (void)printf(", mounted by ");
529 if ((pw = getpwuid(sfp->f_owner)) != NULL)
530 (void)printf("%s", pw->pw_name);
532 (void)printf("%d", sfp->f_owner);
535 if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
536 (void)printf(", writes: sync %ju async %ju",
537 (uintmax_t)sfp->f_syncwrites,
538 (uintmax_t)sfp->f_asyncwrites);
539 if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
540 (void)printf(", reads: sync %ju async %ju",
541 (uintmax_t)sfp->f_syncreads,
542 (uintmax_t)sfp->f_asyncreads);
543 if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) {
545 for (i = 0; i < sizeof(sfp->f_fsid); i++)
546 printf("%02x", ((u_char *)&sfp->f_fsid)[i]);
556 struct statfs *mntbuf;
559 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
560 for (i = mntsize - 1; i >= 0; i--) {
561 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
562 strcmp(mntbuf[i].f_mntonname, name) == 0)
576 if (s1 == NULL || *s1 == '\0')
580 i = strlen(s0) + strlen(s1) + 1 + 1;
581 if ((cp = malloc(i)) == NULL)
582 errx(1, "malloc failed");
583 (void)snprintf(cp, i, "%s,%s", s0, s1);
593 mangle(options, argcp, argv)
602 for (s = options; (p = strsep(&s, ",")) != NULL;)
611 } else if (strcmp(p, "rw") != 0) {
622 update_options(opts, fstab, curflags)
629 char *expopt, *newopt, *tmpopt;
634 /* remove meta options from list */
635 remopt(fstab, MOUNT_META_OPTION_FSTAB);
636 remopt(fstab, MOUNT_META_OPTION_CURRENT);
637 cur = flags2opts(curflags);
640 * Expand all meta-options passed to us first.
643 for (p = opts; (o = strsep(&p, ",")) != NULL;) {
644 if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
645 expopt = catopt(expopt, fstab);
646 else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
647 expopt = catopt(expopt, cur);
649 expopt = catopt(expopt, o);
655 * Remove previous contradictory arguments. Given option "foo" we
656 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
657 * and "foo" - so we can deal with possible options like "notice".
660 for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
661 if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
662 errx(1, "malloc failed");
664 strcpy(tmpopt, "no");
666 remopt(newopt, tmpopt);
669 if (strncmp("no", o, 2) == 0)
672 newopt = catopt(newopt, o);
686 if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
691 for (p = string; (o = strsep(&p, ",")) != NULL;) {
692 if (strcmp(opt, o) != 0) {
693 if (*r == ',' && *o != '\0')
695 while ((*r++ = *o++) != '\0')
707 (void)fprintf(stderr, "%s\n%s\n%s\n",
708 "usage: mount [-dfpruvw] [-o options] [-t ufs | external_type] special node",
709 " mount [-adfpruvw] [ -F fstab] [-o options] [-t ufs | external_type]",
710 " mount [-dfpruvw] special | node");
716 const struct statfs *ent;
721 opts = flags2opts(ent->f_flags);
722 printf("%s\t%s\t%s %s", ent->f_mntfromname, ent->f_mntonname,
723 ent->f_fstypename, opts);
726 if ((fst = getfsspec(ent->f_mntfromname)))
727 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
728 else if ((fst = getfsfile(ent->f_mntonname)))
729 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
730 else if (strcmp(ent->f_fstypename, "ufs") == 0) {
731 if (strcmp(ent->f_mntonname, "/") == 0)
748 res = catopt(res, (flags & MNT_RDONLY) ? "ro" : "rw");
750 if (flags & MNT_SYNCHRONOUS) res = catopt(res, "sync");
751 if (flags & MNT_NOEXEC) res = catopt(res, "noexec");
752 if (flags & MNT_NOSUID) res = catopt(res, "nosuid");
753 if (flags & MNT_NODEV) res = catopt(res, "nodev");
754 if (flags & MNT_UNION) res = catopt(res, "union");
755 if (flags & MNT_ASYNC) res = catopt(res, "async");
756 if (flags & MNT_NOATIME) res = catopt(res, "noatime");
757 if (flags & MNT_NOCLUSTERR) res = catopt(res, "noclusterr");
758 if (flags & MNT_NOCLUSTERW) res = catopt(res, "noclusterw");
759 if (flags & MNT_NOSYMFOLLOW) res = catopt(res, "nosymfollow");
760 if (flags & MNT_SUIDDIR) res = catopt(res, "suiddir");
761 if (flags & MNT_MULTILABEL) res = catopt(res, "multilabel");
762 if (flags & MNT_ACLS) res = catopt(res, "acls");