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>
66 #include "pathnames.h"
69 #define MOUNT_META_OPTION_FSTAB "fstab"
70 #define MOUNT_META_OPTION_CURRENT "current"
72 int debug, fstab_style, verbose;
74 char *catopt(char *, const char *);
75 struct statfs *getmntpt(const char *);
76 int hasopt(const char *, const char *);
77 int ismounted(struct fstab *, struct statfs *, int);
78 int isremountable(const char *);
79 void mangle(char *, int *, const char **);
80 char *update_options(char *, char *, int);
81 int mountfs(const char *, const char *, const char *,
82 int, const char *, const char *);
83 void remopt(char *, const char *);
84 void prmount(struct statfs *);
85 void putfsent(const struct statfs *);
87 char *flags2opts(int);
89 /* Map from mount options to printable formats. */
94 { MNT_ASYNC, "asynchronous" },
95 { MNT_EXPORTED, "NFS exported" },
96 { MNT_LOCAL, "local" },
97 { MNT_NOATIME, "noatime" },
98 { MNT_NODEV, "nodev" },
99 { MNT_NOEXEC, "noexec" },
100 { MNT_NOSUID, "nosuid" },
101 { MNT_NOSYMFOLLOW, "nosymfollow" },
102 { MNT_QUOTA, "with quotas" },
103 { MNT_RDONLY, "read-only" },
104 { MNT_SYNCHRONOUS, "synchronous" },
105 { MNT_UNION, "union" },
106 { MNT_NOCLUSTERR, "noclusterr" },
107 { MNT_NOCLUSTERW, "noclusterw" },
108 { MNT_SUIDDIR, "suiddir" },
109 { MNT_SOFTDEP, "soft-updates" },
114 * List of VFS types that can be remounted without becoming mounted on top
116 * XXX Is this list correct?
119 remountable_fs_names[] = {
120 "ufs", "ffs", "ext2fs",
129 const char *mntfromname, **vfslist, *vfstype;
131 struct statfs *mntbuf;
134 int all, ch, i, init_flags, mntsize, rval, have_fstab;
135 char *cp, *ep, *options;
137 all = init_flags = 0;
141 while ((ch = getopt(argc, argv, "adfo:prwt:uv")) != -1)
150 init_flags |= MNT_FORCE;
154 options = catopt(options, optarg);
161 options = catopt(options, "ro");
165 errx(1, "only one -t option may be specified");
166 vfslist = makevfslist(optarg);
170 init_flags |= MNT_UPDATE;
176 options = catopt(options, "noro");
186 #define BADTYPE(type) \
187 (strcmp(type, FSTAB_RO) && \
188 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
193 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
194 err(1, "getmntinfo");
196 while ((fs = getfsent()) != NULL) {
197 if (BADTYPE(fs->fs_type))
199 if (checkvfsname(fs->fs_vfstype, vfslist))
201 if (hasopt(fs->fs_mntops, "noauto"))
203 if (!(init_flags & MNT_UPDATE) &&
204 ismounted(fs, mntbuf, mntsize))
206 if (mountfs(fs->fs_vfstype, fs->fs_spec,
207 fs->fs_file, init_flags, options,
211 } else if (fstab_style) {
212 for (i = 0; i < mntsize; i++) {
213 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
215 putfsent(&mntbuf[i]);
218 for (i = 0; i < mntsize; i++) {
219 if (checkvfsname(mntbuf[i].f_fstypename,
230 if (init_flags & MNT_UPDATE) {
233 if ((mntbuf = getmntpt(*argv)) == NULL)
234 errx(1, "not currently mounted %s", *argv);
236 * Only get the mntflags from fstab if both mntpoint
237 * and mntspec are identical. Also handle the special
238 * case where just '/' is mounted and 'spec' is not
239 * identical with the one from fstab ('/dev' is missing
240 * in the spec-string at boot-time).
242 if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
243 if (strcmp(fs->fs_spec,
244 mntbuf->f_mntfromname) == 0 &&
246 mntbuf->f_mntonname) == 0) {
248 mntfromname = mntbuf->f_mntfromname;
249 } else if (argv[0][0] == '/' &&
250 argv[0][1] == '\0') {
253 mntfromname = fs->fs_spec;
257 options = update_options(options, fs->fs_mntops,
260 mntfromname = mntbuf->f_mntfromname;
261 options = update_options(options, NULL,
264 rval = mountfs(mntbuf->f_fstypename, mntfromname,
265 mntbuf->f_mntonname, init_flags, options, 0);
268 rmslashes(*argv, *argv);
269 if ((fs = getfsfile(*argv)) == NULL &&
270 (fs = getfsspec(*argv)) == NULL)
271 errx(1, "%s: unknown special file or filesystem",
273 if (BADTYPE(fs->fs_type))
274 errx(1, "%s has unknown filesystem type",
276 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
277 init_flags, options, fs->fs_mntops);
281 * If -t flag has not been specified, the path cannot be
282 * found, spec contains either a ':' or a '@', then assume
283 * that an NFS filesystem is being specified ala Sun.
284 * Check if the hostname contains only allowed characters
285 * to reduce false positives. IPv6 addresses containing
286 * ':' will be correctly parsed only if the separator is '@'.
287 * The definition of a valid hostname is taken from RFC 1034.
289 if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL) ||
290 ((ep = strchr(argv[0], ':')) != NULL)) {
293 ep = cp + strlen(cp);
297 if (!isdigit(*cp) && !isalpha(*cp) &&
298 *cp != '.' && *cp != '-' && *cp != ':')
305 rval = mountfs(vfstype,
306 argv[0], argv[1], init_flags, options, NULL);
314 * If the mount was successfully, and done by root, tell mountd the
315 * good news. Pid checks are probably unnecessary, but don't hurt.
317 if (rval == 0 && getuid() == 0 &&
318 (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
319 if (fscanf(mountdfp, "%d", &pid) == 1 &&
320 pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
321 err(1, "signal mountd");
322 (void)fclose(mountdfp);
329 ismounted(fs, mntbuf, mntsize)
331 struct statfs *mntbuf;
336 if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
337 /* the root filesystem can always be remounted */
340 for (i = mntsize - 1; i >= 0; --i)
341 if (strcmp(fs->fs_file, mntbuf[i].f_mntonname) == 0 &&
342 (!isremountable(fs->fs_vfstype) ||
343 strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
349 isremountable(vfsname)
354 for (cp = remountable_fs_names; *cp; cp++)
355 if (strcmp(*cp, vfsname) == 0)
361 hasopt(mntopts, option)
362 const char *mntopts, *option;
367 if (option[0] == 'n' && option[1] == 'o') {
372 optbuf = strdup(mntopts);
374 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
375 if (opt[0] == 'n' && opt[1] == 'o') {
376 if (!strcasecmp(opt + 2, option))
378 } else if (!strcasecmp(opt, option))
386 mountfs(vfstype, spec, name, flags, options, mntopts)
387 const char *vfstype, *spec, *name, *options, *mntopts;
390 /* List of directories containing mount_xxx subcommands. */
391 static const char *edirs[] = {
396 const char *argv[100], **edir;
400 char *optbuf, execname[MAXPATHLEN + 1], mntpath[MAXPATHLEN];
407 /* resolve the mountpoint with realpath(3) */
408 (void)checkpath(name, mntpath);
413 if (options == NULL) {
414 if (*mntopts == '\0') {
421 optbuf = catopt(strdup(mntopts), options);
423 if (strcmp(name, "/") == 0)
425 if (flags & MNT_FORCE)
426 optbuf = catopt(optbuf, "force");
427 if (flags & MNT_RDONLY)
428 optbuf = catopt(optbuf, "ro");
431 * The mount_mfs (newfs) command uses -o to select the
432 * optimization mode. We don't pass the default "-o rw"
435 if (flags & MNT_UPDATE)
436 optbuf = catopt(optbuf, "update");
438 /* Compatibility glue. */
439 if (strcmp(vfstype, "msdos") == 0)
443 argv[argc++] = vfstype;
444 mangle(optbuf, &argc, argv);
450 (void)printf("exec: mount_%s", vfstype);
451 for (i = 1; i < argc; i++)
452 (void)printf(" %s", argv[i]);
457 switch (pid = fork()) {
458 case -1: /* Error. */
463 if (strcmp(vfstype, "ufs") == 0)
464 exit(mount_ufs(argc, (char * const *) argv));
466 /* Go find an executable. */
467 for (edir = edirs; *edir; edir++) {
468 (void)snprintf(execname,
469 sizeof(execname), "%s/mount_%s", *edir, vfstype);
470 execv(execname, (char * const *)argv);
472 if (errno == ENOENT) {
475 for (edir = edirs; *edir; edir++)
476 len += strlen(*edir) + 2; /* ", " */
477 if ((cp = malloc(len)) == NULL)
478 errx(1, "malloc failed");
480 for (edir = edirs; *edir; edir++) {
485 warn("exec mount_%s not found in %s", vfstype, cp);
489 default: /* Parent. */
492 if (waitpid(pid, &status, 0) < 0) {
497 if (WIFEXITED(status)) {
498 if (WEXITSTATUS(status) != 0)
499 return (WEXITSTATUS(status));
500 } else if (WIFSIGNALED(status)) {
501 warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
506 if (statfs(name, &sf) < 0) {
507 warn("statfs %s", name);
529 (void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
532 flags = sfp->f_flags & MNT_VISFLAGMASK;
533 for (o = optnames; flags && o->o_opt; o++)
534 if (flags & o->o_opt) {
535 (void)printf(", %s", o->o_name);
539 (void)printf(", mounted by ");
540 if ((pw = getpwuid(sfp->f_owner)) != NULL)
541 (void)printf("%s", pw->pw_name);
543 (void)printf("%d", sfp->f_owner);
546 if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
547 (void)printf(", writes: sync %ld async %ld",
548 sfp->f_syncwrites, sfp->f_asyncwrites);
549 if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
550 (void)printf(", reads: sync %ld async %ld",
551 sfp->f_syncreads, sfp->f_asyncreads);
560 struct statfs *mntbuf;
563 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
564 for (i = mntsize - 1; i >= 0; i--) {
565 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
566 strcmp(mntbuf[i].f_mntonname, name) == 0)
580 if (s1 == NULL || *s1 == '\0')
584 i = strlen(s0) + strlen(s1) + 1 + 1;
585 if ((cp = malloc(i)) == NULL)
586 errx(1, "malloc failed");
587 (void)snprintf(cp, i, "%s,%s", s0, s1);
597 mangle(options, argcp, argv)
606 for (s = options; (p = strsep(&s, ",")) != NULL;)
615 } else if (strcmp(p, "rw") != 0) {
626 update_options(opts, fstab, curflags)
633 char *expopt, *newopt, *tmpopt;
638 /* remove meta options from list */
639 remopt(fstab, MOUNT_META_OPTION_FSTAB);
640 remopt(fstab, MOUNT_META_OPTION_CURRENT);
641 cur = flags2opts(curflags);
644 * Expand all meta-options passed to us first.
647 for (p = opts; (o = strsep(&p, ",")) != NULL;) {
648 if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
649 expopt = catopt(expopt, fstab);
650 else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
651 expopt = catopt(expopt, cur);
653 expopt = catopt(expopt, o);
659 * Remove previous contradictory arguments. Given option "foo" we
660 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
661 * and "foo" - so we can deal with possible options like "notice".
664 for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
665 if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
666 errx(1, "malloc failed");
668 strcpy(tmpopt, "no");
670 remopt(newopt, tmpopt);
673 if (strncmp("no", o, 2) == 0)
676 newopt = catopt(newopt, o);
690 if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
695 for (p = string; (o = strsep(&p, ",")) != NULL;) {
696 if (strcmp(opt, o) != 0) {
697 if (*r == ',' && *o != '\0')
699 while ((*r++ = *o++) != '\0')
711 (void)fprintf(stderr, "%s\n%s\n%s\n",
712 "usage: mount [-dfpruvw] [-o options] [-t ufs | external_type] special node",
713 " mount [-adfpruvw] [-t ufs | external_type]",
714 " mount [-dfpruvw] special | node");
720 const struct statfs *ent;
725 opts = flags2opts(ent->f_flags);
726 printf("%s\t%s\t%s %s", ent->f_mntfromname, ent->f_mntonname,
727 ent->f_fstypename, opts);
730 if ((fst = getfsspec(ent->f_mntfromname)))
731 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
732 else if ((fst = getfsfile(ent->f_mntonname)))
733 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
734 else if (strcmp(ent->f_fstypename, "ufs") == 0) {
735 if (strcmp(ent->f_mntonname, "/") == 0)
752 res = catopt(res, (flags & MNT_RDONLY) ? "ro" : "rw");
754 if (flags & MNT_SYNCHRONOUS) res = catopt(res, "sync");
755 if (flags & MNT_NOEXEC) res = catopt(res, "noexec");
756 if (flags & MNT_NOSUID) res = catopt(res, "nosuid");
757 if (flags & MNT_NODEV) res = catopt(res, "nodev");
758 if (flags & MNT_UNION) res = catopt(res, "union");
759 if (flags & MNT_ASYNC) res = catopt(res, "async");
760 if (flags & MNT_NOATIME) res = catopt(res, "noatime");
761 if (flags & MNT_NOCLUSTERR) res = catopt(res, "noclusterr");
762 if (flags & MNT_NOCLUSTERW) res = catopt(res, "noclusterw");
763 if (flags & MNT_NOSYMFOLLOW) res = catopt(res, "nosymfollow");
764 if (flags & MNT_SUIDDIR) res = catopt(res, "suiddir");